-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrie.js
57 lines (57 loc) · 1.64 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
var Trie = /** @class */ (function () {
function Trie(value) {
this.terminal = false;
this.value = value;
this.children = new Map();
}
Trie.prototype.add = function (c) {
var val;
if (this.value == null) {
val = c;
}
else {
val = this.value + c;
}
this.children.set(c, new Trie(val));
};
Trie.prototype.insert = function (word) {
var node = this;
for (var i = 0; i < word.length; i++) {
var c = word.charAt(i);
if (!node.children.has(c)) {
node.add(c);
}
node = node.children.get(c);
}
node.terminal = true;
};
Trie.prototype.autoComplete = function (prefix) {
var node = this;
var ret = [];
for (var i = 0; i < prefix.length; i++) {
var c = prefix.charAt(i);
if (!node.children.has(c)) {
return [];
}
node = node.children.get(c);
}
return this.flatten(node.allPrefixes());
};
Trie.prototype.allPrefixes = function () {
var results = [];
if (this.terminal) {
results.push(this.value);
}
this.children.forEach(function (value, key) {
var child = value;
var childPrefixes = child.allPrefixes();
results = results.concat([childPrefixes]);
});
return results;
};
Trie.prototype.flatten = function (arr) {
var flat = [].concat.apply([], arr);
return flat.some(Array.isArray) ? this.flatten(flat) : flat;
};
return Trie;
}());