-
Notifications
You must be signed in to change notification settings - Fork 3
/
trie.js
59 lines (55 loc) · 1.36 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
function Node(value) {
this.value = value;
this.children = {};
this.isLeafNode = false;
}
function Trie() {
this.root = new Node(null);
}
Trie.prototype.add = function(word, node) {
if (!word) return;
node = node || this.root;
var char = word.charAt(0);
var child = node.children[char];
if (!child) {
child = new Node(char);
node.children[char] = child;
}
var remainder = word.substring(1);
if (remainder.length === 0) {
child.isLeafNode = true;
} else {
this.add(remainder, child);
}
};
Trie.prototype.autocomplete = function(prefix, node, stem, words) {
node = node || this.root;
stem = stem || "";
words = words || [];
prefix = prefix.toUpperCase();
var char = prefix.charAt(0);
var child = node.children[char];
if (child) {
var remainder = prefix.substring(1);
if (remainder.length > 0) {
this.autocomplete(remainder, child, stem.concat(char), words);
} else {
this.dfs(child, stem.concat(char), words);
}
}
return words;
};
// depth-first search traversal
Trie.prototype.dfs = function(node, chars = "", words = []) {
if (node && node.children) {
if (node.isLeafNode) {
words.push(chars);
} else {
Object.keys(node.children).forEach(
function(char) {
this.dfs(node.children[char], chars.concat(char), words);
}.bind(this)
);
}
}
};