-
Notifications
You must be signed in to change notification settings - Fork 11
/
binarySearchTree.js
349 lines (255 loc) · 8.82 KB
/
binarySearchTree.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
const Node = require('../Node/nodeTree.js');
class BianrySearchTree
{
constructor(data)
{
this.root = new Node(data);
this.count = 1;
}
size()
{
console.log(`Total Nodes: ${this.count}`);
return this.count;
}
insert(data) // Time Complexity O(n)
{
this.count++;
let newNode = new Node(data);
const searchTree = node =>
{
if(data <= node.data) // Go left
{
if(!node.left)
{
node.left = newNode; // If no left child, append node
console.log(`${data} inserted to Node ${node.data} as left child`);
}
else searchTree(node.left); // If left child exists, look left again
}
else if(data > node.data) // Go right
{
if(!node.right)
{
node.right = newNode; // If no right child, append node
console.log(`${data} inserted to Node ${node.data} as right child`);
}
else searchTree(node.right); // If right child exists, look right again
}
}
searchTree(this.root);
}
remove(data) // Time Complexity O(n)
{
console.log(`${data} removed from Tree`);
const removeNode = (node,data) =>
{
if(node === null) return null;
if(data === node.data)
{
// Case 1: If node has no children
if(node.left === null && node.right === null) return null;
// Case 2: If node has no left child
if(node.left === null) return node.right;
// Case 3: If node has no right child
if(node.right === null) return node.left;
// Case 4: If node has 2 children
let tempNode = node.right;
while(tempNode.left !== null) tempNode = tempNode.left;
node.data = tempNode.data;
node.right = removeNode(node.right, tempNode.data)
return node;
}
else if(data < node.data)
{
node.left = removeNode(node.left, data);
return node;
}
else
{
node.right = removeNode(node.right, data);
return node;
}
}
this.root = removeNode(this.root, data);
}
min() // Finds the minimum value in the tree
{
let currentNode = this.root;
while(currentNode.left) // Continue traversing through left until no more children
{
currentNode = currentNode.left;
}
console.log(`Minimum data: ${currentNode.data}`);
return currentNode.data;
}
max() // Finds the maximum value in the tree
{
let currentNode = this.root;
while(currentNode.right) // Continue traversing through right until no more children
{
currentNode = currentNode.right;
}
console.log(`Maximum data: ${currentNode.data}`);
return currentNode.data;
}
contains(data) // Time Complexity O(n)
{
let currentNode = this.root;
while(currentNode)
{
if(data === currentNode.data)
{
console.log(`Data: '${data}' exists`);
return true;
}
if(data <= currentNode.data) currentNode = currentNode.left;
else currentNode = currentNode.right;
}
console.log(`Data: '${data}' doesn't exist`);
return false;
}
printTree()
{
let nodes = [];
let queue = [];
queue.push(this.root);
while(queue.length)
{
let currentNode = queue.shift();
nodes.push(currentNode);
if(currentNode.left) queue.push(currentNode.left);
if(currentNode.right) queue.push(currentNode.right);
}
nodes.forEach(node => console.log(node));
return nodes;
}
findMinHeight(node = this.root) // Finds minimum height in the tree
{
if(node === null) return -1;
let left = this.findMinHeight(node.left);
let right = this.findMinHeight(node.right);
if(left < right) return left + 1;
else return right + 1;
}
findMaxHeight(node = this.root) // Finds maximum height in the tree
{
if(node === null) return -1;
let left = this.findMaxHeight(node.left);
let right = this.findMaxHeight(node.right);
if(left > right) return left + 1;
else return right + 1;
}
isBalanced() // Checks if the tree is balanced or not
{
this.findMinHeight() >= this.findMaxHeight() - 1 ?
console.log(`Balanced ? Yes`) :
console.log(`Balanced ? No`);
return (this.findMinHeight() >= this.findMaxHeight() - 1);
}
// Depth first search - Looking branch by branch
preOrder() // Root, Left, Right
{
let nodes = [];
const traverse = node =>
{
nodes.push(node.data); // Push root data
if(node.left) traverse(node.left); // If left child exists, go left again
if(node.right) traverse(node.right); // If right child exists, go right again
}
traverse(this.root);
console.log(`Pre-order Traversal: ${nodes.join(' ')}`);
return nodes;
}
inOrder() // Left, Root, Right
{
let nodes = [];
const traverse = node =>
{
if(node.left) traverse(node.left); // If left child exists, go left again
nodes.push(node.data); // Push root data
if(node.right) traverse(node.right); // If right child exists, go right again
}
traverse(this.root);
console.log(`In-order Traversal: ${nodes.join(' ')}`);
return nodes;
}
postOrder() // Left, Right, Root
{
let nodes = [];
const traverse = node =>
{
if(node.left) traverse(node.left); // If left child exists, go left again
if(node.right) traverse(node.right); // If right child exists, go right again
nodes.push(node.data); // Push root data
}
traverse(this.root);
console.log(`Post-order Traversal: ${nodes.join(' ')}`);
return nodes;
}
// Breadth first search - Looking level by level
BFS()
{
let nodes = [];
let queue = [];
queue.push(this.root);
while(queue.length)
{
let currentNode = queue.shift();
nodes.push(currentNode.data);
if(currentNode.left) queue.push(currentNode.left);
if(currentNode.right) queue.push(currentNode.right);
}
console.log(`BFS Traversal: ${nodes.join(' ')}`);
return nodes;
}
}
// Space Complexity O(n)
// BST is a type of Binary Tree where, Left (Subtree) < Root < Right (Subtree)
// Exception, a child can be equal to parent and in that case it's
// either, Left (Subtree) <= Root < Right (Subtree) or Left (Subtree) < Root <= Right (Subtree)
// It's an ordered tree
/*
50
/ \
40 70
/ \ / \
40 45 65 90
----------------------| |----------------------
/ \ / \
42 48 63 67
*/
/*
const bst = new BianrySearchTree(50); // Root / Level 0
// Level 1 nodes
bst.insert(40);
bst.insert(70);
// Level 2 nodes
bst.insert(40);
bst.insert(45);
bst.insert(65);
bst.insert(90);
// Level 3 nodes
bst.insert(42);
bst.insert(48);
bst.insert(63);
bst.insert(67);
bst.size();
bst.min();
bst.max();
bst.printTree();
console.log('Minimum height: ' + bst.findMinHeight());
console.log('Maximum height: ' + bst.findMaxHeight());
bst.isBalanced();
bst.contains(40);
bst.contains(98);
bst.preOrder();
bst.inOrder();
bst.postOrder();
bst.BFS();
bst.remove(45);
bst.contains(45);
bst.printTree();
console.log('Minimum height: ' + bst.findMinHeight());
console.log('Maximum height: ' + bst.findMaxHeight());
bst.isBalanced();
*/