-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateTrie.js
206 lines (163 loc) · 4.61 KB
/
createTrie.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
module.exports = createTrie;
function createTrie() {
const publicApi = {
insert,
count,
suggest,
populate,
delete: remove,
getRootNode,
toHierarchy
};
const rootNode = createTrieNode("__root__");
let numberOfWords = 0;
function insert(word) {
const normalizedWord = word.trim();
numberOfWords++;
normalizedWord.split("").reduce((parentNode, letter) => {
const children = parentNode.getChildren();
const trieNode = children[letter] || createTrieNode(letter);
// set the child if it does not exist
if (!children[letter]) {
children[letter] = trieNode;
}
// mark the last letter of the word
if (letter === normalizedWord[normalizedWord.length - 1]) {
trieNode.setIsCompleteString(true);
}
return trieNode;
}, rootNode);
}
function count() {
return numberOfWords;
}
function suggest(query) {
const normalizedQuery = query.trim();
const nodePath = getNodePath(normalizedQuery);
const endOfQueryNode = nodePath[nodePath.length - 1];
const startingSuggestions = [];
// the trie contains no path match for the query
if (nodePath.length === 0) return [];
// the query itself matches a word in the trie
if (endOfQueryNode.getIsCompleteString()) {
startingSuggestions.push(normalizedQuery);
}
return getSuggestions(endOfQueryNode, normalizedQuery, startingSuggestions);
}
function populate(wordList) {
wordList.forEach(word => insert(word));
}
function getRootNode() {
return rootNode;
}
function remove(word) {
const nodePath = getNodePath(word);
const lastNodeIndex = nodePath.length - 1;
// the word to delete is not a word that exists in the trie
if (
nodePath.length === 0 ||
!nodePath[lastNodeIndex].getIsCompleteString()
) {
throw Error(`${word} is not a word, nothing deleted`);
}
for (let index = lastNodeIndex; index > 0; index--) {
const node = nodePath[index];
const parentNode = nodePath[index - 1];
// we've found a full word
if (node.getIsCompleteString()) {
if (index === lastNodeIndex) {
// the word found is the word requested to be deleted
node.setIsCompleteString(false);
} else {
// the word found is a prefix of the word requested to be deleted;
// return from loop since rest nodes belong to this prefix word
return;
}
}
if (isEmpty(node.getChildren())) {
delete parentNode.getChildren()[node.getValue()];
}
}
}
function toHierarchy() {
return {
name: rootNode.getValue(),
children: rootNode.getChildren({ arrayForm: true })
};
}
// public api
return publicApi;
// private methods
function getNodePath(query) {
let nodePath = [];
let parentNode = rootNode;
for (const letter of query.split("")) {
const childNode = parentNode.getChildren()[letter];
// a node does not exist for some letter in the query
if (!childNode) return [];
nodePath.push(childNode);
parentNode = childNode;
}
return nodePath;
}
function getSuggestions(node, substring, suggestions = []) {
const children = node.getChildren();
for (const key in children) {
const childNode = children[key];
const nextSubstring = substring + childNode.getValue();
if (!childNode) return substring;
if (childNode.getIsCompleteString()) {
suggestions.push(nextSubstring);
}
getSuggestions(childNode, nextSubstring, suggestions);
}
return suggestions;
}
}
function createTrieNode(letter) {
const publicApi = {
getValue,
getChildren,
setIsCompleteString,
getIsCompleteString
};
const value = letter;
const children = {};
let isCompleteString = false;
function getValue() {
return value;
}
function getChildren(options = {}) {
if (options.arrayForm) {
return Object.keys(children).map(key => {
const node = children[key];
return {
name: node.getValue(),
children: node.getChildren({ arrayForm: true })
};
});
}
return children;
}
function getIsCompleteString() {
return isCompleteString;
}
function setIsCompleteString(bool) {
isCompleteString = Boolean(bool);
return isCompleteString;
}
// public api
return publicApi;
}
/**
* Check if value is an empty object
* @param {Object} value - the value to check
*/
function isEmpty(value) {
for (const key in value) {
if (hasOwnProperty.call(value, key)) {
return false;
}
}
return true;
}