Package Exports
- @datastructures-js/trie
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@datastructures-js/trie) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@datastructures-js/trie
node's data type: string.

Usage
const trieFn = require('@datastructures-js/trie');
const trie = trieFn();
// by default, the trie has 1 node, the root node.
API
.node(char)
creates a trie node that contain a one or more language charachter.
- .getChar() gets the node's charachter.
- .setParent(parent) sets the node's parent.
- .getParent() gets the node's parent.
- .setEndOfWord(isEndOfWord) sets the node as the end char of a word.
- .isEndOfWord() checks if a node's char is the end of a word.
- .addChild(child) adds a child node to the node.
- .removeChild(child) removes a child of the node.
- .getChild(char) gets a node's child by its character.
- .getChildren() gets all the children of a node.
- .countChildren() gets the count of node' children.
const n = trie.node('T');
console.log(n.getChar()); // T
console.log(n.isEndOfWord()); // false
console.log(n.countChildren()); // 0
.insert(word)
inserts a word into the trie.
try {
trie.insert('hi');
trie.insert('hit');
trie.insert('hide');
trie.insert('hello');
trie.insert('sand');
trie.insert('safe');
trie.insert('noun');
trie.insert('name');
trie.insert(123); // throws an error
} catch(e) {
console.log(e) // 123 is not a word
}
.search(word)
finds a word in the trie and returns the last char's node or null if word is not found.
try {
const n1 = trie.search('hi');
console.log(n1.getChar()); // i
console.log(n1.getParent().getChar()); // h
console.log(trie.search('abc')); // null
trie.search(); // throws an error
} catch(e) {
console.log(e); // undefined is not a word
}
.traverse(cb)
traverse the trie and calls cb for each word including the empty word
trie.traverse(console.log);
// hi
// hit
// hide
// hello
// sand
// safe
// noun
// name
.remove(word)
removes a word from the trie
try {
trie.remove('hit');
console.log(trie.search('hit')); // null
trie.remove(null);
} catch(e) {
console.log(e); // null is not a word
}
.countNodes()
gets the count of characters nodes in the trie
console.log(trie.countNodes()); // 22
.countWords()
gets the count of the words in the trie
console.log(trie.countWords()); // 7
.clear()
clears the trie
trie.clear();
console.log(trie.countNodes()); // 1 (root default node)
console.log(trie.countWords()); // 0
Build
grunt build
License
The MIT License. Full License is here