forked from Yomguithereal/mnemonist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrie.js
269 lines (209 loc) · 4.69 KB
/
trie.js
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/**
* Mnemonist Trie
* ===============
*
* Very simple Trie implementation.
*/
var iterateOver = require('./utils/iterate.js');
/**
* Constants.
*/
var SENTINEL = '\uE000';
/**
* Trie.
*
* @constructor
*/
function Trie() {
this.clear();
this.end = SENTINEL;
}
/**
* Method used to clear the trie.
*
* @return {undefined}
*/
Trie.prototype.clear = function() {
// Properties
this.root = {};
this.size = 0;
};
/**
* Method used to add an item to the trie.
*
* @param {string|array} item - Item to add.
* @return {Trie}
*/
Trie.prototype.add = function(item) {
if (typeof item === 'string')
item = item.split('');
if (!item || !item.length)
return this;
var node = this.root,
token;
for (var i = 0, l = item.length; i < l; i++) {
token = item[i];
if (!node.hasOwnProperty(token))
node[token] = {};
node = node[token];
}
// The item already exists in the trie, we don't need to increase size
if (node[this.end])
return this;
node[this.end] = true;
this.size++;
return this;
};
/**
* Method used to delete an item from the trie.
*
* @param {string|array} item - Item to delete.
* @return {boolean}
*/
Trie.prototype.delete = function(item) {
if (typeof item === 'string')
item = item.split('');
if (!item || !item.length)
return false;
var node = this.root,
prefix = [],
token,
i,
l;
for (i = 0, l = item.length; i < l; i++) {
token = item[i];
if (!node.hasOwnProperty(token))
return false;
node = node[token];
prefix.push([token, node]);
}
if (!node[this.end])
return false;
this.size--;
delete node[this.end];
if (Object.keys(node).length >= 1)
return true;
for (i = prefix.length - 1; i >= 1; i--) {
if (Object.keys(prefix[i][1]).length < 2)
delete prefix[i - 1][1][prefix[i][0]];
else
break;
}
if (Object.keys(this.root[prefix[0][0]]).length < 2)
delete this.root[prefix[0][0]];
return true;
};
/**
* Method used to assert whether the given item is in the Trie.
*
* @param {string|array} item - Item to check.
* @return {boolean}
*/
Trie.prototype.has = function(item) {
if (typeof item === 'string')
item = item.split('');
if (!item || !item.length)
return false;
var node = this.root,
token;
for (var i = 0, l = item.length; i < l; i++) {
token = item[i];
if (!node.hasOwnProperty(token))
return false;
node = node[token];
}
return this.end in node;
};
/**
* Method used to retrieve every item in the trie with the given prefix.
*
* @param {string|array} prefix - Prefix to query.
* @return {array<string|array>}
*/
Trie.prototype.get = function(prefix) {
var string = typeof prefix === 'string',
item = prefix,
matches = [];
if (!item || !item.length)
return matches;
var node = this.root,
token;
for (var i = 0, l = item.length; i < l; i++) {
token = item[i];
node = node[token];
if (!node)
return matches;
}
var stack = [node, string ? '' : []],
tokens,
k;
while (stack.length) {
tokens = stack.pop();
node = stack.pop();
if (node[this.end])
matches.push(string ? prefix + tokens : prefix.concat(tokens));
for (k in node) {
if (k === this.end)
continue;
stack.push(node[k]);
stack.push(string ? tokens + k : tokens.concat(k));
}
}
return matches;
};
/**
* Method used to get the longest matching prefix for the given item.
*
* @param {string|array} item - Item to query.
* @return {string|array}
*/
Trie.prototype.longestPrefix = function(item) {
var string = typeof item === 'string';
if (string)
item = item.split('');
if (!item || !item.length)
return string ? '' : [];
var prefix = [],
node = this.root,
token;
for (var i = 0, l = item.length; i < l; i++) {
token = item[i];
if (!node.hasOwnProperty(token))
break;
prefix.push(token);
node = node[token];
}
return string ? prefix.join('') : prefix;
};
/**
* Convenience known methods.
*/
Trie.prototype.inspect = function() {
var proxy = {
size: this.size
};
// Trick so that node displays the name of the constructor
Object.defineProperty(proxy, 'constructor', {
value: Trie,
enumerable: false
});
return proxy;
};
Trie.prototype.toJSON = function() {
return this.root;
};
/**
* Static @.from function taking an abitrary iterable & converting it into
* a trie.
*
* @param {Iterable} iterable - Target iterable.
* @return {Heap}
*/
Trie.from = function(iterable) {
var trie = new Trie();
iterateOver(iterable, function(value) {
trie.add(value);
});
return trie;
};
module.exports = Trie;