@kamilmielnik/trie
Version:
Trie data structure implementation in TypeScript. Highly performant. No dependencies. Built for a Scrabble Solver.
19 lines (18 loc) • 589 B
JavaScript
/**
* Finds {@link Node} representing given prefix in given {@link Node}.
*
* @param node - {@link Node} to look for prefix in.
* @param prefix - Prefix to be found.
* @returns {@link Node} representing a given prefix, undefined if there is no such node.
*/
export const find = (node, prefix) => {
let currentNode = node;
for (let index = 0; index < prefix.length; ++index) {
const character = prefix[index];
if (!currentNode[character]) {
return undefined;
}
currentNode = currentNode[character];
}
return currentNode;
};