-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtrie.js
187 lines (163 loc) · 4.96 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
const TrieNode = require('../Trie/trieNode');
class Trie {
constructor() {
this.root = new TrieNode(true, '*');
this.length = 0;
}
/**
* Action to insert a string.
* @param {number} key The string to be inserted.
*/
insert(key){
var currentValue = this.root;
for (let index = 0; index < key.length; index++) {
const element = key[index];
if (currentValue.children[element]) {
currentValue = currentValue.children[element];
} else {
this.length++;
const newNode = new TrieNode(false, element);
currentValue.children[element] = newNode;
currentValue = newNode;
}
}
currentValue.isEndOfWord = true;
}
/**
* Action to insert a string.
* @param {number} key The string to be inserted.
*/
insertRecursive(key){
this.length++;
this._addNode(this.root, key);
}
_addNode(node, word) {
if(!node || !word) return null;
var child = node.children[word[0]];
if(!child) {
child = new TrieNode(false, word[0]);
node.children[word[0]] = child;
}
var remainder = word.substring(1);
if(!remainder) {
child.isEndOfWord = true;
}
this._addNode(child, remainder);
}
/**
* Returns the node, if it exists on the trie
* @param {string} key The string to be searched in the structure.
* @return {TrieNode} Node found if it exists
*/
search(key){
var currentValue = this.root;
for (let index = 0; index < key.length; index++) {
const element = key[index];
if (currentValue.children[element]) {
currentValue = currentValue.children[element];
} else{
return false;
}
}
return (currentValue != null && currentValue.isEndOfWord);
}
/**
* Checks whether the list is empty or not
* @return {boolean}
*/
isEmpty(){
return this.length > 0 ? false : true;
}
/**
* Returns the size of the tree
* @return {number}
*/
getLength(){
return this.length;
}
/**
* Clear the structure
*/
clear(){
this.root = new TrieNode(true, '*');
}
/**
* Action to remove string with a specific prefix.
* @param {Boolean}
*/
remove(key) {
if(this.search(key)){
this._removeNode(this.root ,key, key, 0);
return true;
}else{
return false;
}
}
_removeNode(node, keySlice ,key, index) {
var letter = key[index];
var current = node.children[letter];
if(current){
keySlice = key.slice(index + 1, key.length);
var shouldRemove = this._removeNode(current, keySlice, key, index + 1 );
if(shouldRemove && !this._hasChild(node.children[letter].children)){
this.length--;
delete node.children[letter];
key = keySlice;
return true;
}else{
return false;
}
}
return true;
}
_hasChild(obj) {
var count = 0;
for(var key in obj) {
count = count + 1;
}
return count > 0 ? true : false;
}
/**
* Returns a list of words, if it exists on the trie
* @param {string} key The string to be searched in the structure.
* @return [strings] Array of strings if if exists on the
*/
suggestionWord(key) {
var word = this.searchWord(key);
if(word){
var suggestions = [];
if(word.isEndOfWord){
suggestions.push(key);
}
return this._suggestionWord(word, key, suggestions);
}
return [];
}
searchWord(key){
var currentValue = this.root;
for (let index = 0; index < key.length; index++) {
const element = key[index];
if (currentValue.children[element]) {
currentValue = currentValue.children[element];
} else{
return null;
}
}
return currentValue;
}
_suggestionWord(node, lastWord, suggestions){
var letters = Object.keys(node.children);
for (let index = 0; index < letters.length; index++) {
const element = letters[index];
if(node.children[element].isEndOfWord){
suggestions.push(lastWord + node.children[element].character);
this._suggestionWord(node.children[element], lastWord + node.children[element].character, suggestions);
}else{
var rest = lastWord + node.children[element].character;
this._suggestionWord(node.children[element], rest, suggestions);
}
}
return suggestions;
}
}
module.exports = Trie;