fix bug in trie

This commit is contained in:
Joeri Exelmans 2025-05-10 20:53:36 +02:00
parent fe6e86e1a4
commit 3b8548e9af
2 changed files with 13 additions and 0 deletions

View file

@ -183,6 +183,9 @@ const __suggest = (trie, path, remaining, maxSuggestions) => {
} }
const [pos, prefix] = binarySearch(trie.children, remaining); const [pos, prefix] = 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];
return __suggest(haveChildNode, path+haveKey, remaining.slice(haveKey.length), maxSuggestions); return __suggest(haveChildNode, path+haveKey, remaining.slice(haveKey.length), maxSuggestions);

View file

@ -90,3 +90,13 @@ assert.equal(
true, true,
"trie should always be properly sorted!" "trie should always be properly sorted!"
); );
assert.equal(
growPrefix(bigTrie)("dict.le"),
"ngth"
);
assert.deepEqual(
suggest(bigTrie)("ooo")(2),
[]
);