Last active
June 11, 2020 12:40
-
-
Save CodHeK/545bccd1fec49dfed8f9c7497de5cfe1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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