@kamilmielnik/trie
Version:
Trie data structure implementation in TypeScript. Highly performant. No dependencies. Built for a Scrabble Solver.
24 lines (23 loc) • 696 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.add = void 0;
/**
* 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.
*/
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;
};
exports.add = add;