-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trie.ts
172 lines (137 loc) · 3.73 KB
/
Trie.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { Node } from "./Node.js";
import { LinkedList } from "../linked-list/LinkedList.js";
export class Trie {
#root: Node;
#cache: Node;
#prefix: string;
constructor() {
this.#root = new Node("");
this.#cache = this.#root;
this.#prefix = "";
}
/**
* Adds a new string to the trie
* @param word - string to add
*/
add(word: string, weight = Infinity): void {
const chars = word.split("");
if (this.#has([...chars], this.#root)) {
throw new Error(`"${word}" already exists`);
}
return this.#add(chars, weight, this.#root);
}
#add(chars: string[], weight: number, node: Node): void {
if (chars.length === 0) {
node.complete.set(true);
node.weight = weight;
return;
}
const char = chars.shift() as string;
node.count.increment();
if (node.children[char]) {
this.#add(chars, weight, node.children[char]);
} else {
const newNode = new Node(char);
node.children[char] = newNode;
this.#add(chars, weight, newNode);
}
}
/**
* Removes a string from the trie
* @param word - string to remove
*/
remove(word: string): void {
const chars = word.split("");
if (!this.#has([...chars], this.#root)) {
throw new Error(`${word} does not exist`);
}
return this.#remove(chars, this.#root);
}
#remove(chars: string[], node: Node): void {
if (chars.length === 0) {
node.complete.set(false);
return;
}
const char = chars.shift() as string;
node.count.decrement();
if (node.count.get() === 0) {
delete node.children[char];
return;
}
this.#remove(chars, node.children[char]);
}
/**
* Checks if a string exists in the trie
* @param word - string to look for
*/
has(word: string): boolean {
const chars = word.split("");
return this.#has(chars, this.#root);
}
#has(chars: string[], node: Node): boolean {
if (chars.length === 0) {
return node.complete.get();
}
const char = chars.shift() as string;
if (node.children[char]) {
return this.#has(chars, node.children[char]);
}
return false;
}
/**
* Traverses to the last available node, and preforms a breadth-first-search to
* return all descendant strings. Results can be limited for brevity.
* @param word - string to determine the starting node for search
*/
search(word: string): string[] {
const chars = word.split("");
let node: Node;
try{
node = this.#cursor([...chars], this.#cache);
} catch {
try {
node = this.#cursor([...chars], this.#root);
} catch {
return [];
}
}
const start = node.element;
this.#cache = node;
const result: [string, number][] = []; // TODO: replace with heap
const queue = new LinkedList<[Node, string]>();
queue.push([node, this.#prefix]);
while(queue.size > 0) {
const [node, word] = queue.pop() || [];
if (!node) {
continue;
}
if (node.complete.get() && word) {
result.push([word, node.weight]);
}
node.children && Object.values(node.children).forEach((node) => {
queue.push([node, word + node.element]);
});
}
result.sort((a, b) => a[1] - b[1]);
return result.map(([word]) => word);
}
#cursor(chars: string[], node: Node): Node {
if (chars.length === 0) {
return node;
}
const char = chars.shift() as string;
this.#prefix = this.#prefix + char;
if (node.children[char]) {
return this.#cursor(chars, node.children[char]);
}
this.#cache = this.#root;
this.#prefix = "";
throw new Error("word not found");
}
__debug() {
return {
root: this.#root,
cache: this.#cache,
};
}
}