autocomplete-trie
This library uses a prefix trie data structure to efficiently generate autocomplete suggestions.
Usage
const AutoComplete = ;const trie = ;
API
.add( word: String )
Adds a word to the trie. Words inside the trie can be return as suggestions later.
Example:
// Create a new AutoComplete trieconst trie = ; // Add "hello" to the trietrie; // AutoComplete trie now contains "hello"
.contains( word: String )
Tests if the AutoComplete trie contains a given word. REturns a boolean.
Example:
// Create an empty AutoComplete trieconst trie = ; // Trie shouldn't contain anything yet. This returns falselet containsHello = trie trie; // This returns true nowcontainsHello = trie
.suggest( prefix: String )
Takes a word prefix and returns an array of all words in the tree which begin with the prefix.
Rather than searching the whole tree, .suggest()
will search the subtree
rooted at the last character in the prefix, eliminating most of the nodes
from the search.
Example:
// Create empty AutoComplete trieconst trie = ; // Add some wordstrie;trie;trie;trie;trie;trie; // Get suggestions for the prefix 'he'.const suggestions = trie; // ['hello', 'helium', 'help']