Skip to content

Instantly share code, notes, and snippets.

@CodHeK
Last active June 11, 2020 12:40
Show Gist options
  • Save CodHeK/545bccd1fec49dfed8f9c7497de5cfe1 to your computer and use it in GitHub Desktop.
Save CodHeK/545bccd1fec49dfed8f9c7497de5cfe1 to your computer and use it in GitHub Desktop.
traverse(root, word) {
if(root.isLeaf) {
this.suggestions.push(word);
return;
}
for(const letter in root.children) {
this.traverse(root.children[letter], word + letter);
}
}
complete(word, CHILDREN=null) {
const root = this.find(word);
if(!root) return this.suggestions; // cannot suggest anything
const children = root.children;
let spread = 0;
for(const letter in children) {
this.traverse(children[letter], word + letter);
spread++;
if(CHILDREN && spread === CHILDREN) break;
}
return this.suggestions;
}
// trie.complete('pro') -> [ 'project', 'produce' ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment