-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrie.js
More file actions
137 lines (109 loc) · 3.15 KB
/
Trie.js
File metadata and controls
137 lines (109 loc) · 3.15 KB
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
const Node = function (key) {
this.key = key;
this.parent = null;
this.children = {};
this.end = false;
this.getWord = function() {
let output = [];
let node = this;
while (node !== null) {
output.unshift(node.key);
node = node.parent;
}
return output.join('');
};
}
const Trie = function() {
this.root = new Node(null);
this.add = function(word) {
let node = this.root;
for(let i = 0; i < word.length; i++) {
if (!node.children[word[i]]) {
node.children[word[i]] = new Node(word[i]);
node.children[word[i]].parent = node;
}
node = node.children[word[i]];
if (i == word.length-1) {
node.end = true;
}
}
};
this.has = function(word) {
let node = this.root;
for(let i = 0; i < word.length; i++) {
if (node.children[word[i]]) {
node = node.children[word[i]];
} else {
return false;
}
}
return node.end;
};
this.search = function(prefix) {
let node = this.root;
let output = [];
for(let i = 0; i < prefix.length; i++) {
if (node.children[prefix[i]]) {
node = node.children[prefix[i]];
} else {
return output;
}
}
this.findAllWords(node, output);
return output;
};
// recursive function to search all words in the given node.
this.findAllWords = (node, arr) => {
// base case, if node is at a word, push to output
if (node.end) {
arr.unshift(node.getWord());
}
// iterate through each children, call recursive findAllWords
for (let child in node.children) {
this.findAllWords(node.children[child], arr);
}
}
// removes a word from the trie.
this.remove = function (word) {
let root = this.root;
if(!word) return;
// recursively finds and removes a word
const removeWord = (node, word) => {
// check if current node has the word
if (node.end && node.getWord() === word) {
// check and see if node has children
let hasChildren = Object.keys(node.children).length > 0;
// if has children we only want to un-flag the end node that marks the end of a word.
// this way we do not remove words that contain/include supplied word
if (hasChildren) {
node.end = false;
} else {
// remove word by getting parent and setting children to empty dictionary
node.parent.children = {};
}
return true;
}
// recursively remove word from all children
for (let key in node.children) {
removeWord(node.children[key], word)
}
return false
};
// call remove word on root node
removeWord(root, word);
};
}
const trie = new Trie();
// add few values
trie.add("peter");
trie.add("piper");
trie.add("picked");
trie.add("pickled");
trie.add("pepper");
// check has method
console.log(trie.has("picked"));
console.log(trie.has("pepper"));
trie.remove("pepper");
// check search method
console.log(trie.search("pi"));
console.log(trie.search("pe"));