-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrieClass.js
More file actions
97 lines (86 loc) · 2.49 KB
/
Copy pathTrieClass.js
File metadata and controls
97 lines (86 loc) · 2.49 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
/*
Implement TrieNode and Trie data structures using ES6 classes
Open to suggestions & comments!
*/
class TrieNode {
constructor() {
this.children = {};
this.endOfWord = false; // boolean that indicates whether this node is the end of a word
}
}
class Trie {
constructor() {
this.root = new TrieNode();
}
insertIterative(word) {
let current = this.root;
for (let i = 0; i < word.length; i++) {
let letter = word[i];
let child = current.children[letter];
if (!child) {
child = new TrieNode();
current.children[letter] = child;
}
current = child;
}
current.endOfWord = true;
}
insertRecursive(current, word, index) {
index = index || 0;
if (index === word.length) {
current.endOfWord = true;
}
let letter = word[index];
let child = current.children[letter];
if (!child) {
child = new TrieNode();
current.children[letter] = child;
}
return this.insertRecursive(child, word, index + 1);
}
searchIterative(word) {
let current = this.root;
for (let i = 0; i < word.length; i++) {
let letter = word[i];
let child = current.children[letter];
if (!child) {
return false;
}
current = child;
}
return current.endOfWord; // endOfWord is true only if word exists
}
searchRecursive(current, word, index) {
index = index || 0;
if (index === word.length) {
return current.endOfWord;
}
let letter = word[index];
let child = current.children[letter];
if (!child) {
return false;
}
return this.searchRecursive(child, word, index + 1);
}
delete(current, word, index) {
index = index || 0;
if (index === word.length) { // when you get to end of word
if (!current.endOfWord) { // if endOfWord is false, word does not exist
return false;
}
current.endOfWord = false; // if endOfWord is true, delete word by changing endOfWord to false
return Object.keys(current.children).length === 0; // check to see if node has children
}
let letter = word[index];
let child = current.children[letter];
if (!child) { // word doesn't exist
return false;
}
let shouldDeleteCurrentNode = this.delete(child, word, index + 1);
if (shouldDeleteCurrentNode) { // if node has no children, delete node reference from parent's children
delete current.children[letter];
return Object.keys(current.children).length === 0;
}
return false;
}
}