deno.land / x / masx200_leetcode_test@10.6.5 / utils / TrieNodeForEach.ts

TrieNodeForEach.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
39
40
41
import { TrieNode } from "../implement-trie-ii-prefix-tree/TrieNode.ts";
export function TrieNodeForEach( root: TrieNode, cb: (word: string) => void,): void { if (root.children.size === 0) { return; }
let result = traverse([{ root, prefix: "" }], (s) => cb(s)); while (typeof result == "function") { result = result(); } return;}function traverse( roots_and_prefixes: { root: TrieNode; prefix: string }[], output: (s: string) => void,) { if (roots_and_prefixes.length === 0) { return; } return () => { const rap: typeof roots_and_prefixes = []; for (const { root, prefix } of roots_and_prefixes) { if (root.children.size === 0) { continue; } for (const [key, child] of root.children) { for (let i = 0; i < child.wordCount; i++) { output(prefix + key); } rap.push({ root: child, prefix: prefix + key }); } }
return traverse(rap, output); };}
masx200_leetcode_test

Version Info

Tagged at
a year ago