advanced
Insert and search in a trie
Complete the character tree's two operations — the child map each node holds, the write that links a new branch, and the flag that says a word ends here.
interface TrieNode {
children: Map<string, TrieNode>
isEndOfWord: boolean
}
export class Trie {
#root: TrieNode = { children: new Map(), isEndOfWord: false }
insert(word: string): void {
let node = this.#root
for (const ch of word) {
let next = node.________.get(ch)
if (next === undefined) {
next = { children: new Map(), isEndOfWord: false }
node.children.___(ch, next)
}
node = next
}
node.__________... = true
}
search(word: string): boolean {
let node: TrieNode | undefined = this.#root
for (const ch of word) {
node = node.children.get(ch)
if (node === undefined) return false
}
return node.__________...
}
}0/4 filled tab moves · Ctrl+↵ submits · Ctrl+Shift+↵ runsstarter code