All Projects → lyndseybrowning → trie-prefix-tree

lyndseybrowning / trie-prefix-tree

Licence: other
Create and modify trie prefix structures and extract word lists including prefixes, anagrams and sub-anagrams

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to trie-prefix-tree

HArray
Fastest Trie structure (Linux & Windows)
Stars: ✭ 89 (+111.9%)
Mutual labels:  trie
Traverser
Traverser is a Java library that helps software engineers implement advanced iteration of a data structure.
Stars: ✭ 45 (+7.14%)
Mutual labels:  depth-first-search
lucilla
Fast, efficient, in-memory Full Text Search for Kotlin
Stars: ✭ 102 (+142.86%)
Mutual labels:  trie
prefixtree
A prefix tree (trie) implementation in go
Stars: ✭ 21 (-50%)
Mutual labels:  trie
pathfinding-visualizer
🔍 A friendly visualizer for some search algorithms, like DFS, BDS, Greedy and A*
Stars: ✭ 21 (-50%)
Mutual labels:  depth-first-search
AlgoDaily
just for fun
Stars: ✭ 118 (+180.95%)
Mutual labels:  trie
go-erasure
Erasure coding (Reed–Solomon coding) in Go
Stars: ✭ 44 (+4.76%)
Mutual labels:  trie
poplar-trie
C++17 implementation of memory-efficient dynamic tries
Stars: ✭ 47 (+11.9%)
Mutual labels:  trie
Data-Structures
Algorithmic Problems Solutions -- hash table code featured in geeksforgeeks
Stars: ✭ 44 (+4.76%)
Mutual labels:  trie
aho-corasick-node
A Node implementation of the Aho-Corasick string matching algorithm based on DoubleArray Trie.
Stars: ✭ 16 (-61.9%)
Mutual labels:  trie
Algorithms
Data Structures & Algorithms. Includes solutions for Cracking the Coding Interview 6th Edition
Stars: ✭ 89 (+111.9%)
Mutual labels:  trie
minesweeper
API for setting up a Minesweeper game environment in its simplicity and robustness
Stars: ✭ 15 (-64.29%)
Mutual labels:  depth-first-search
radix
Golang radix tree implementation
Stars: ✭ 30 (-28.57%)
Mutual labels:  trie
Competitive Programming
Contains solutions and codes to various online competitive programming challenges and some good problems. The links to the problem sets are specified at the beginning of each code.
Stars: ✭ 65 (+54.76%)
Mutual labels:  trie
fixie-trie
Compact tries for fixed-width keys
Stars: ✭ 23 (-45.24%)
Mutual labels:  trie
treebitmap
Fast IP lookup table for IPv4/IPv6 prefixes
Stars: ✭ 81 (+92.86%)
Mutual labels:  trie
triebeard
Radix trees in Rcpp and R
Stars: ✭ 29 (-30.95%)
Mutual labels:  trie
maze-generator
A real-time JavaScript maze generator using the depth-first search algorithm
Stars: ✭ 13 (-69.05%)
Mutual labels:  depth-first-search
Data-Structures-and-Algorithms
Implementation of various Data Structures and algorithms - Linked List, Stacks, Queues, Binary Search Tree, AVL tree,Red Black Trees, Trie, Graph Algorithms, Sorting Algorithms, Greedy Algorithms, Dynamic Programming, Segment Trees etc.
Stars: ✭ 144 (+242.86%)
Mutual labels:  trie
retrie
Efficient Trie-based regex unions for blacklist/whitelist filtering and one-pass mapping-based string replacing
Stars: ✭ 35 (-16.67%)
Mutual labels:  trie

Trie Prefix Tree

Travis Build codecov coverage version downloads MIT License semantic-release

This is a Trie implementation written in JavaScript, with insert and remove capability. It can be used to search a predefined dictionary for prefixes, check a prefix exists and retrieve a list of anagrams and sub-anagrams based on given letters.

What is a Trie?

A Trie (also known as a prefix-tree) is a data structure for storing strings in a tree. Each branch in the tree represents a single character which allows for fast and efficient depth-first searching. Let's say we have a dictionary with the words: CAR, CAT and CURL. We can visualise the trie like this:

trie data structure

Installation

Pull down dependencies:

npm install

This project uses Jest for unit testing and ESLint for linting.

To run combined linting & unit tests:

npm test

To run linting:

npm run lint

Run tests in watch mode:

npm run test-watch

Get code coverage report:

npm run test-coverage

How to Use

To use the Trie, install and save it to your package dependencies:

npm install trie-prefix-tree --save

To create a new Trie:

var trie = require('trie-prefix-tree');

// using ES2015 Modules
import trie from 'trie-prefix-tree';

Instantiate the Trie:

var myTrie = trie(['cat', 'cats', 'dogs', 'elephant', 'tiger']);

Trie functionality:

// retrieve a stringified dump of the Trie object
myTrie.dump(); // { c: { a: { t: $: 1 }, s: 1 ... }}

// optionally pass in spacer parameter to format the output string
myTrie.dump(2); // equivalent of JSON.stringify(obj, null, 2);
// retrieve the Trie object instance
myTrie.tree();
// add a new word to the Trie
myTrie.addWord('lion');
// remove an existing word from the Trie
myTrie.removeWord('dogs');

Adding and removing words can be chained:

myTrie.addWord('hello').removeWord('hello');

Prefix searching:

// check if a prefix exists:
myTrie.isPrefix('do'); // true
myTrie.isPrefix('z'); // false
// count prefixes
myTrie.countPrefix('c'); // 2
// get an array of words with the passed in prefix
myTrie.getPrefix('c'); // ['cat', 'cats']

// Pass false as the second parameter to disable 
// output being sorted alphabetically
// this is useful when your dictionary is already sorted
// and will therefore save performance
myTrie.getPrefix('c', false); // ['cat', 'cats']
// get a random word at a prefix
myTrie.getRandomWordWithPrefix('c'); // 'cat'
myTrie.getRandomWordWithPrefix('c'); // 'cats'

Other:

// retrieve a full list of words in the Trie
// the output array is automatically sorted
myTrie.getWords(); // ['cat', 'cats', 'elephant', 'lion', 'tiger']

// pass false to disable the output being sorted
// this is useful when your dictionary is already sorted
// and will therefore save performance
myTrie.getWords(false); // ['cat', 'cats', 'elephant', 'tiger', 'lion']
// check if a word exists in the Trie
myTrie.hasWord('elephant'); // true
myTrie.hasWord('zoo'); // false
// generate a list of valid anagrams from the given letters
myTrie.getAnagrams('act'); // ['cat'];
// generate a list of valid sub-anagrams from the given letters
myTrie.getSubAnagrams('ctalion'); ['cat', 'cats', 'lion'];

Credits

Credit goes to Kent C. Dodds for providing the awesome 'How to Create an Open Source JavaScript Library' course, available on egghead.io.

License

This project is referenced under the MIT license and is free to use and distribute.

MIT @ Lyndsey Browning

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].