-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tree.js
231 lines (183 loc) · 6.15 KB
/
Tree.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
class Node {
constructor(value){
this.value = value;
this.left = null;
this.right = null;
}
}
class Tree {
constructor(){
this.root = null;
}
insert(value){
const newNode = new Node(value);
if(this.root === null){
this.root = newNode;
return this;
} else {
let currentNode = this.root;
while(true){
if(currentNode.value > value){
if(!currentNode.left){
currentNode.left = newNode;
return this;
}
currentNode = currentNode.left;
} else {
if(!currentNode.right){
currentNode.right = newNode;
return this;
}
currentNode = currentNode.right;
}
}
}
}
lookup(value){
let currentNode = this.root;
while(currentNode !== null){
if(value === currentNode.value){
return currentNode;
} else {
if(value < currentNode.value){
if(!currentNode.left){
return null;
}
currentNode = currentNode.left;
} else {
if(!currentNode.right){
return null;
}
currentNode = currentNode.right;
}
}
}
return null;
}
remove(value){
//if there is no node, return false;
if(!this.root){
return false;
}
let currentNode = this.root;
let parentNode = null;
while(currentNode){
if(value < currentNode.value){
parentNode = currentNode;
currentNode = currentNode.left;
} else if (value > currentNode.value){
parentNode = currentNode;
currentNode = currentNode.right;
} else if (value === currentNode.value){
//Case 1: Node to be deleted has no child i.e. leaf node
if(currentNode.left === null && currentNode.right === null){
// if tree has only one node
if(parentNode === null){
this.root = null;
return;
} else if (currentNode === parentNode.right){
parentNode.right = null;
return;
} else if(currentNode === parentNode.left){
parentNode.left = null;
return;
}
}
// Case 2: When node to be deleted has one child
else if(currentNode.left === null && currentNode.right !== null){
parentNode.right = currentNode.right;
return;
}
else if(currentNode.right === null && currentNode.left !== null){
parentNode.left = currentNode.left;
return;
}
//Case 3: When node to be deleted has two child, and its right child is the successor.
else if(currentNode.right.left === null){
currentNode.right.left = currentNode.left;
parentNode.right = currentNode.right;
return;
}
//Case 4: When node to be deleted has two child, and right child's left descendents is the successor
else if(currentNode.right.left !== null){
let successor = currentNode.right.left;
let successorParent = currentNode.right;
while(successor.left !== null){
successorParent = successor;
successor = successor.left;
}
successorParent.left = successor.right;
successor.left = currentNode.left;
successor.right = currentNode.right;
parentNode.right = successor;
return;
}
} else {
return false;
}
}
}
inOrder(node, list){
if(node.left){
this.inOrder(node.left, list);
}
list.push(node.value);
if(node.right){
this.inOrder(node.right, list);
}
return list;
}
breadthFirstSearch() {
let list = [];
let queue = [];
let currentNode = this.root;
queue.push( currentNode);
while( queue.length > 0 ) {
currentNode = queue.shift();
list.push(currentNode.value);
if (currentNode.left) {
queue.push(currentNode.left);
}
if(currentNode.right) {
queue.push(currentNode.right);
}
}
return list;
}
depthFirstSearchInOrderIterative(){
let current = this.root;
let list = [];
let stack = [];
while( stack.length || current ){
if(current){
stack.push(current);
current = current.left;
} else {
current = stack.pop();
list.push(current.value);
current = current.right;
}
}
return list;
}
depthFirstSearchInOrderRecursive(){
return this.inOrder(this.root, []);
}
}
const tree = new Tree();
tree.insert(50);
tree.insert(73);
tree.insert(62);
tree.insert(87);
tree.insert(84);
tree.insert(93);
tree.insert(85);
tree.insert(79);
tree.insert(77);
tree.insert(78);
// tree.inOrder(tree.root);
// tree.remove(73);
// console.log(tree);
//console.log(tree.breadthFirstSearch());
//console.log(tree.depthFirstSearchInOrderIterative());
console.log(tree.depthFirstSearchInOrderRecursive());