@kamilmielnik/trie
Version:
Trie data structure implementation in TypeScript. Highly performant. No dependencies. Built for a Scrabble Solver.
20 lines (19 loc) • 585 B
JavaScript
/**
* Inserts given `word` into given `node`.
*
* @param node - {@link Node} to insert the `word` to.
* @param word - Word to be inserted into `node`.
* @returns {@link Node} representing the end of the added word.
*/
export const add = (node, word) => {
let currentNode = node;
for (let index = 0; index < word.length; ++index) {
const character = word[index];
if (!currentNode[character]) {
currentNode[character] = {};
}
currentNode = currentNode[character];
}
currentNode.wordEnd = true;
return currentNode;
};