fix bug in radix trie

This commit is contained in:
Joeri Exelmans 2025-05-19 17:02:20 +02:00
parent 70fb80a9fc
commit 4fcfea409a
2 changed files with 32 additions and 14 deletions

View file

@ -19,7 +19,7 @@ export const isProperlySorted = trie => {
}
}
return true;
}
};
// find maximal common prefix, and whether string A is smaller than B
const commonPrefix = (strA, strB) => {
@ -34,12 +34,12 @@ const commonPrefix = (strA, strB) => {
}
}
return [strA.slice(0, i), false];
}
};
// a funny kind of binary search, that assumes 'ls' is a sorted list of strings, and none of the strings have a common prefix
const binarySearch = (ls, key) => {
return __binarySearch(ls, key, 0, ls.length);
}
};
const __binarySearch = (ls, key, min, max) => {
if (min === max) {
return [max, ""]; // otherwise we go out of bounds
@ -54,7 +54,7 @@ const __binarySearch = (ls, key, min, max) => {
return __binarySearch(ls, key, min, middle);
}
return __binarySearch(ls, key, middle+1, max);
}
};
const check = trie => {
// uncomment if you think shit is broken:
@ -62,7 +62,7 @@ const check = trie => {
// throw new Error('not properly sorted!')
// }
return trie;
}
};
// insert (key,value) into trie.
export const insert = trie => key => value => {
@ -118,15 +118,20 @@ export const insert = trie => key => value => {
children: trie.children.with(
insertPos, // position to update
[prefix, {
value: (postFix.length === 0) ? value : undefined,
children: (havePostFix < postFix)
? [
[havePostFix, haveChildNode],
[postFix, {value, children: []}],
]
: [
: ((postFix.length > 0)
? [
[postFix, {value, children: []}],
[havePostFix, haveChildNode],
],
]
: [
[havePostFix, haveChildNode],
]),
}],
),
});
@ -153,12 +158,12 @@ export const growPrefix = trie => key => {
}
}
return "";
}
};
// get array of (key, value) entries whose keys are prefixed by 'key'.
export const suggest = trie => key => maxSuggestions => {
return __suggest(trie, "", key, maxSuggestions);
}
};
export const get = trie => key => {
if (key === '') {
@ -171,7 +176,7 @@ export const get = trie => key => {
return get(haveChildNode)(key.slice(haveKey.length));
}
}
}
};
const __suggest = (trie, path, remaining, maxSuggestions) => {
if (maxSuggestions === 0) {
@ -205,4 +210,4 @@ const __suggest = (trie, path, remaining, maxSuggestions) => {
}
return [];
}
};

View file

@ -27,6 +27,8 @@ const with7Items = insert(with6Items)('ab')('yup');
console.log(pretty(with7Items));
const with8Items = insert(with7Items)('')('hi!');
console.log(pretty(with8Items));
const with9Items = insert(with8Items)('fo')(42);
console.log(pretty(with9Items));
///////////////////
// Setup TRIE 2 ...
@ -82,11 +84,22 @@ assert.deepEqual(
]);
assert.equal(
isProperlySorted(with8Items),
isProperlySorted(with9Items),
true,
"trie should always be properly sorted!"
);
assert.equal(
get(with9Items)(''),
"hi!"
)
assert.equal(
get(with9Items)('fo'),
42
)
assert.equal(
isProperlySorted(bigTrie),
true,