fix bug in trie
This commit is contained in:
parent
fe6e86e1a4
commit
3b8548e9af
2 changed files with 13 additions and 0 deletions
|
|
@ -183,6 +183,9 @@ const __suggest = (trie, path, remaining, maxSuggestions) => {
|
|||
}
|
||||
|
||||
const [pos, prefix] = binarySearch(trie.children, remaining);
|
||||
if (prefix.length === 0) {
|
||||
return []; // nothing in common
|
||||
}
|
||||
if (pos < trie.children.length) {
|
||||
const [haveKey, haveChildNode] = trie.children[pos];
|
||||
return __suggest(haveChildNode, path+haveKey, remaining.slice(haveKey.length), maxSuggestions);
|
||||
|
|
|
|||
|
|
@ -90,3 +90,13 @@ assert.equal(
|
|||
true,
|
||||
"trie should always be properly sorted!"
|
||||
);
|
||||
|
||||
assert.equal(
|
||||
growPrefix(bigTrie)("dict.le"),
|
||||
"ngth"
|
||||
);
|
||||
|
||||
assert.deepEqual(
|
||||
suggest(bigTrie)("ooo")(2),
|
||||
[]
|
||||
);
|
||||
Loading…
Add table
Add a link
Reference in a new issue