fix bug in trie
This commit is contained in:
parent
3b8548e9af
commit
29c7b6af61
2 changed files with 9 additions and 4 deletions
|
|
@ -182,12 +182,12 @@ const __suggest = (trie, path, remaining, maxSuggestions) => {
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
const [pos, prefix] = binarySearch(trie.children, remaining);
|
const [pos] = binarySearch(trie.children, remaining);
|
||||||
if (prefix.length === 0) {
|
|
||||||
return []; // nothing in common
|
|
||||||
}
|
|
||||||
if (pos < trie.children.length) {
|
if (pos < trie.children.length) {
|
||||||
const [haveKey, haveChildNode] = trie.children[pos];
|
const [haveKey, haveChildNode] = trie.children[pos];
|
||||||
|
if (!remaining.startsWith(haveKey) && !haveKey.startsWith(remaining)) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
return __suggest(haveChildNode, path+haveKey, remaining.slice(haveKey.length), maxSuggestions);
|
return __suggest(haveChildNode, path+haveKey, remaining.slice(haveKey.length), maxSuggestions);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -99,4 +99,9 @@ assert.equal(
|
||||||
assert.deepEqual(
|
assert.deepEqual(
|
||||||
suggest(bigTrie)("ooo")(2),
|
suggest(bigTrie)("ooo")(2),
|
||||||
[]
|
[]
|
||||||
|
);
|
||||||
|
|
||||||
|
assert.deepEqual(
|
||||||
|
suggest(bigTrie)("df")(2),
|
||||||
|
[]
|
||||||
);
|
);
|
||||||
Loading…
Add table
Add a link
Reference in a new issue