Repository navigation
Expand file tree
/
Copy pathtrie.ts
More file actions
105 lines (96 loc) · 2.92 KB
/
Copy pathtrie.ts
File metadata and controls
105 lines (96 loc) · 2.92 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* Trie (prefix tree)
*
* Idea: store words character by character along a tree. Each edge is a
* character and each node remembers whether a word ends there. Words that
* share a prefix share the same path, so "car", "card" and "care" reuse the
* nodes for "c", "a", "r".
*
* Used for: autocomplete, spell checkers, IP routing tables (on bits),
* word games and fast prefix counting.
*
* Complexity (L = word length, k = number of matches):
* - insert / has / startsWith / delete: O(L)
* - withPrefix: O(L + size of the subtree below the prefix)
* - space: O(total characters stored)
*/
class TrieNode {
children = new Map<string, TrieNode>();
isWord = false;
}
export class Trie {
private root = new TrieNode();
private count = 0;
constructor(words: Iterable<string> = []) {
for (const word of words) this.insert(word);
}
/** Number of distinct words stored. */
get size(): number {
return this.count;
}
insert(word: string): this {
let node = this.root;
for (const ch of word) {
let next = node.children.get(ch);
if (next === undefined) {
next = new TrieNode();
node.children.set(ch, next);
}
node = next;
}
if (!node.isWord) {
node.isWord = true;
this.count++;
}
return this;
}
has(word: string): boolean {
return this.walk(word)?.isWord ?? false;
}
startsWith(prefix: string): boolean {
return this.walk(prefix) !== undefined;
}
/** Every stored word that begins with `prefix`, in sorted order. */
withPrefix(prefix: string, limit = Infinity): string[] {
const start = this.walk(prefix);
const out: string[] = [];
if (start === undefined) return out;
const visit = (node: TrieNode, path: string): void => {
if (out.length >= limit) return;
if (node.isWord) out.push(path);
const keys = [...node.children.keys()].sort();
for (const key of keys) visit(node.children.get(key) as TrieNode, path + key);
};
visit(start, prefix);
return out;
}
/** Removes a word and prunes nodes that no longer lead to any word. */
delete(word: string): boolean {
const chars = [...word];
const path: TrieNode[] = [this.root];
let node = this.root;
for (const ch of chars) {
const next = node.children.get(ch);
if (next === undefined) return false;
node = next;
path.push(node);
}
if (!node.isWord) return false;
node.isWord = false;
this.count--;
for (let i = chars.length; i > 0; i--) {
const current = path[i] as TrieNode;
if (current.isWord || current.children.size > 0) break;
(path[i - 1] as TrieNode).children.delete(chars[i - 1] as string);
}
return true;
}
private walk(text: string): TrieNode | undefined {
let node: TrieNode | undefined = this.root;
for (const ch of text) {
node = node.children.get(ch);
if (node === undefined) return undefined;
}
return node;
}
}