node-ternary-search-trie
v5.5.0
Published
A simple class for ternary search trie in Node.js™.
Downloads
77
Maintainers
Readme
A simple class for ternary search trie implemented in JavaScript.
It is unstable and not for production use. ;)
You can install it via npm install node-ternary-search-trie, or just
include the script lib/trie.js in your web pages.
var Trie = Trie || require('node-ternary-search-trie');
var trie = new Trie();Public methods (with simple Unicode support):
set(key, value) -> thisInsert one key-value pair into the trie. This will overwrite the existed key-value pair.
valueshould not benullorundefined.get(key, toSplay = false) -> value:Fetch the stored value of the given key.
getandsetmethods may be greatly affected after each splaying operation. Splay with caution.del(key) -> thisDelete the key-value pair for the given key.
size() -> sizeReturn the total number of nodes in the trie.
keys() -> [keys...]Sort and return all keys stored in the trie.
keysWithPrefix(prefix) -> [keys...]Sort and return all keys started with the given prefix.
keysWithCommonPrefix(key) -> [keys...]Sort and return all keys that are prefixes of the given key.
keysWithinHammingDistance(key, distance) -> [keys...]Sort and return all keys within a Hamming distance of the given key.
keysWithinLevenshteinDistance(key, distance) -> [keys...]Sort and return all keys within a Levenshtein distance of the given key.
keysWithinDamerauLevenshteinDistance(key, distance) -> [keys...]Sort and return all keys within a Damerau-Levenshtein distance of the given key.
searchWithPrefix(prefix, callback: (key, value) -> void) -> thisJust like
keysWithPrefix.searchWithCommonPrefix(key, callback: (key, value) -> void) -> thisJust like
keysWithCommonPrefix.searchWithinHammingDistance(key, distance, callback: (key, value, distance) -> void) -> thisJust like
keysWithinHammingDistance.searchWithinLevenshteinDistance(key, distance, callback: (key, value, distance) -> void) -> thisJust like
keysWithinLevenshteinDistance.searchWithinDamerauLevenshteinDistance(key, distance, callback: (key, value, distance) -> void) -> thisJust like
keysWithinDamerauLevenshteinDistance.traverse(callback: (key, value) -> void) -> thisTraverse in in-order. (sorted)
traversal() -> iteratorReturn an iterator for in-order traversal.
iterator.next()will return{value: undefined | {key, value}, done: Boolean}.
