deno.land / x / masx200_leetcode_test@10.6.5 / design-add-and-search-words-data-structure / PrefixTreeInsert.ts

PrefixTreeInsert.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { PrefixTree } from "../implement-trie-prefix-tree/PrefixTree.ts";
export function PrefixTreeInsert<T extends PrefixTree>( root: T, word: string, { stop, end, each, create = () => PrefixTree() as T, }: { stop?: (node: T) => boolean; each?: (node: T) => void; create?: () => T; end?: (node: T) => void; } = {},): void { if (word.length === 0) return;
each?.(root); if (stop?.(root)) return; let node = root; for (const ch of word) { const next: T = (node.children.get(ch) ?? (() => { const next = create(); node.children.set(ch, next); return next; })()) as T;
each?.(next); if (stop?.(next)) return; node = next; } node.isEnd = true; end?.(node);}
masx200_leetcode_test

Version Info

Tagged at
a year ago