autocomplete-trie
This library uses a prefix trie data structure to efficiently generate
autocomplete suggestions.
Usage
const AutoComplete = require('trie-autocomplete');
const trie = new AutoComplete();
API
.add( word: String )
Adds a word to the trie. Words inside the trie can be return as suggestions
later.
Example:
const trie = new AutoComplete();
trie.add('hello');
.contains( word: String )
Tests if the AutoComplete trie contains a given word. REturns a boolean.
Example:
const trie = new AutoComplete();
let containsHello = trie.contains('hello')
trie.add('hello');
containsHello = trie.contains('hello')
.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:
const trie = new AutoComplete();
trie.add('hello');
trie.add('helium');
trie.add('help');
trie.add('happy');
trie.add('cat');
trie.add('catastrophe');
const suggestions = trie.suggest('he');