-
Notifications
You must be signed in to change notification settings - Fork 314
/
Copy pathTrie.ts
252 lines (212 loc) · 6.22 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
const TERMINATING_CHAR = '$$LagoTrieTerminatingCharacter';
interface TrieNode {
[key: string]: TrieNode | number;
}
interface CountResult {
[key: string]: number;
}
class Trie {
private _tree: TrieNode;
private _size: number;
constructor() {
this._tree = Object.create(null);
// Number of different strings in the Trie.
// It is not the summation of count.
this._size = 0;
}
get size(): number {
return this._size;
}
/**
* Modifies the count of string in the Trie by a value.
* The string's new count will be returned.
* 0 is returned if the modification was unsuccessful.
* @param {string} string
* @param {number} difference
* @return {number}
*/
private _modifyCountBy(str: string, difference: number): number {
// TODO: Check that difference is a number;
let curr = this._tree;
for (let i = 0; i < str.length; i++) {
const char = str[i];
if (!Object.prototype.hasOwnProperty.call(curr, char)) {
curr[char] = {};
}
curr = curr[char] as TrieNode;
}
if (!Object.prototype.hasOwnProperty.call(curr, TERMINATING_CHAR)) {
this._size++;
curr[TERMINATING_CHAR] = 0;
}
curr[TERMINATING_CHAR] = difference + (curr[TERMINATING_CHAR] as number);
return curr[TERMINATING_CHAR] as number;
}
/**
* Inserts a string into the Trie, optionally specifying its count.
* The string's new count will be returned.
* 0 is returned if the insertion was unsuccessful.
* @param {string} string
* @param {number} [count=1]
* @return {number}
*/
insert(str: string, count = 1): number {
if (count < 1) {
return 0;
}
return this._modifyCountBy(str, count);
}
/**
* Increment the count of string in the Trie by 1.
* If the string does not exist in the Trie, it is inserted.
* The new count is returned.
* @param {string} string
* @return {number}
*/
increment(str: string): number {
return this._modifyCountBy(str, 1);
}
/**
* Deletes a string from the Trie.
* @param {string} string
* @return {boolean} Whether the deletion was successful.
*/
delete(str: string): boolean {
if (!str) {
return false;
}
let curr = this._tree;
const stack: Array<[TrieNode, string]> = [];
for (let i = 0; i < str.length; i++) {
const char = str[i];
if (!Object.prototype.hasOwnProperty.call(curr, char)) {
return false;
}
stack.push([curr, char]);
curr = curr[char] as TrieNode;
}
if (!Object.prototype.hasOwnProperty.call(curr, TERMINATING_CHAR)) {
return false;
}
stack.push([curr, TERMINATING_CHAR]);
this._size--;
// Clean up nodes up the parent chain until
// a non-childless node is encountered.
for (let i = 0; i < stack.length; i++) {
const [node, char] = stack.pop() as [TrieNode, string];
delete node[char];
if (Object.keys(node).length > 0) {
break;
}
}
return true;
}
/**
* Counts the number of times a string appears in the Trie.
* @param {string} string
* @return {boolean} The number of times a string appears in the Trie.
*/
count(str: string): number {
if (!str) {
return 0;
}
let curr = this._tree;
for (let i = 0; i < str.length; i++) {
const char = str[i];
if (!Object.prototype.hasOwnProperty.call(curr, char)) {
return 0;
}
curr = curr[char] as TrieNode;
}
return curr[TERMINATING_CHAR] as number;
}
/**
* Determine if a string is in the Trie.
* @param {string} string
* @return {boolean} Whether the string is in the Trie.
*/
contains(str: string): boolean {
return this.count(str) !== 0;
}
/**
* Returns a mapping of strings to its count in the Trie
* that start with the given prefix.
* @param {string} prefix
* @return {Object}
*/
stringsStartingWith(prefix: string): CountResult {
const results: CountResult = {};
if (!prefix) {
return results;
}
let curr = this._tree;
for (let i = 0; i < prefix.length; i++) {
const char = prefix[i];
if (!Object.prototype.hasOwnProperty.call(curr, char)) {
return results;
}
curr = curr[char] as TrieNode;
}
function traverse(node: TrieNode, chars: string) {
Object.keys(node).forEach((char) => {
if (char === TERMINATING_CHAR) {
results[chars] = node[char] as number;
return;
}
traverse(node[char] as TrieNode, chars + char);
});
}
traverse(curr, prefix);
return results;
}
/**
* Counts the total number of strings in the Trie
* that start with the given prefix.
* @param {string} prefix
* @return {number} The total number of strings in the Trie that start with the prefix.
*/
countStringsStartingWith(prefix: string): number {
const results = this.stringsStartingWith(prefix);
const total = Object.keys(results)
.map((key) => results[key])
.reduce((a, b) => a + b, 0);
return total;
}
/**
* Counts the different number of strings in the Trie
* that start with the given prefix.
* @param {string} prefix
* @return {number} The different number of strings in the Trie that start with the prefix.
*/
differentStringsStartingWith(prefix: string): number {
const results = this.stringsStartingWith(prefix);
return Object.keys(results).length;
}
/**
* Finds the shortest prefix of a string from words within the Trie.
* Returns null if there is no prefix of the string within the Trie.
* @param {string} string
* @return {string|null}
*/
shortestPrefix(string: string): string | null {
let curr = this._tree;
const prefixChars = [];
for (let i = 0; i < string.length; i++) {
const char = string[i];
if (Object.prototype.hasOwnProperty.call(curr, TERMINATING_CHAR)) {
return prefixChars.join('');
}
if (!Object.prototype.hasOwnProperty.call(curr, char)) {
break;
}
prefixChars.push(char);
curr = curr[char] as TrieNode;
}
// If the whole string is a prefix.
if (Object.prototype.hasOwnProperty.call(curr, TERMINATING_CHAR)) {
return prefixChars.join('');
}
return null;
}
}
export default Trie;