-
Notifications
You must be signed in to change notification settings - Fork 118
/
Binary-search-tree-recursive-version.js
474 lines (366 loc) · 15.2 KB
/
Binary-search-tree-recursive-version.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
// Binary Search Tree - recursive version
// Write a function on the BinarySearchTree class
// insert - accepts a value and inserts it into the BST in the correct position.
// The function should return the binary search tree.
// find
// This function should find a node in a binary tree. It should return the node
// if found, otherwise return `null`.
// remove
// This function should remove a node from a binary search tree.
// Your remove function should be able to handle removal of the root node,
// removal of a node with one child and removal of a node with two children.
// The function should return the node removed.
// findSecondLargest
// This function should find 2nd largest node.
// isBalanced
// This function should return true if the BST is balanced, otherwise false.
// A balanced tree is defined as a tree where the depth of all leaf nodes or
// nodes with single children differ by no more than one.
// breadthFirstSearch
// This function should search through each node in the binary search tree
// using breadth first search and return an array containing each node's value.
// depthFirstSearchPreOrder
// This function should search through each node in the binary search tree using
// pre-order depth first search and return an array containing each node's value.
// depthFirstSearchPostOrder
// This function should search through each node in the binary search tree using
// post-order depth first search and return an array containing each node's value.
// depthFirstSearchInOrder
// This function should search through each node in the binary search tree using
// in-order depth first search and return an array containing each node's value.
// getHeight
// This function should return the height of the tree
// The height is the number of nodes along the longest path
// from the root node down to the farthest leaf node.
// getMinHeight
// This function should return the min height of the tree
// The minimum height is the number of nodes along the shortest path
// from the root node down to the nearest leaf node.
// findMin
// This function should return min value in the binary tree
// findMax
// This function should return max value in the binary tree
// invert
// This function should invert the current tree structure
// (produce a tree that is equivalently the mirror image of the current tree)
// findLowestCommonAncestor
// This function should return the lowest common ancestor (LCA) node of two given nodes
// The lowest common ancestor is defined between two nodes p and q
// as the lowest node in T that has both p and q as descendants
// (where we allow a node to be a descendant of itself)
// getBalancedTree
// This function should return a balanced version of the binary search tree
// getTreeFormattedMatrix
// This function should return a matrix that represents a formatted layout of the tree
// printTree
// This function should print a tree formatted matrix
const BinarySearchTreeNode = require('./Binary-search-tree-node');
class BinarySearchTree {
constructor(root = null) {
this.root = root;
}
getHeight(node = this.root) {
if (!node) return 0;
return Math.max(this.getHeight(node.left), this.getHeight(node.right)) + 1;
}
getMinHeight(node = this.root) {
if (!node) return 0;
if (!node.left && !node.right) return 1;
if (!node.left) return this.getMinHeight(node.right) + 1;
if (!node.right) return this.getMinHeight(node.left) + 1;
return Math.min(this.getMinHeight(node.left), this.getMinHeight(node.right)) + 1;
}
isBalanced(node = this.root) {
if (!node) return true;
return Math.abs(this.getHeight(node.left) - this.getHeight(node.right)) <= 1 &&
this.isBalanced(node.left) &&
this.isBalanced(node.right);
}
insert(data, node = this.root) {
if (!node) {
this.root = new BinarySearchTreeNode(data);
return this;
}
if (data < node.data && node.left) return this.insert(data, node.left);
if (data < node.data) {
node.left = new BinarySearchTreeNode(data);
return this;
}
if (data > node.data && node.right) return this.insert(data, node.right);
if (data > node.data) {
node.right = new BinarySearchTreeNode(data);
return this;
}
return this;
}
find(data, node = this.root) {
if (!node) return null;
if (data < node.data) return this.find(data, node.left);
if (data > node.data) return this.find(data, node.right);
return node;
}
contains(data, node = this.root) {
return !!this.find(data, node);
}
findMin(node = this.root) {
if (!node || !node.left) return node;
return this.findMin(node.left);
}
findMax(node = this.root) {
if (!node || !node.right) return node;
return this.findMax(node.right);
}
findSecondLargest(node = this.root, parent = null) {
if (!node) return null;
if (!node.right) return node.left ? this.findMax(node.left) : parent;
return this.findSecondLargest(node.right, node);
}
invert(node = this.root) {
if (!node) return null;
if (node.left) this.invert(node.left);
if (node.right) this.invert(node.right);
[node.left, node.right] = [node.right, node.left];
return node;
}
findNodeWithParent(data, currentNode = this.root, parentNode = null) {
if (!currentNode) {
return {
parentNode: null,
currentNode: null
};
}
if (data < currentNode.data) {
return this.findNodeWithParent(data, currentNode.left, currentNode);
}
if (data > currentNode.data) {
return this.findNodeWithParent(data, currentNode.right, currentNode);
}
return {
parentNode,
currentNode
};
}
findNextBigNodeWithParent(nextBigNodeParent = this.root, nextBigNode = nextBigNodeParent.right) {
if (!nextBigNode || !nextBigNode.left) {
return {
nextBigNodeParent,
nextBigNode
};
}
return this.findNextBigNodeWithParent(nextBigNode, nextBigNode.left);
}
findLowestCommonAncestor(node1, node2, nodeToTraverse = this.root) {
function traverse(node) {
if (node && node.data > node1.data && node.data > node2.data) {
return traverse(node.left);
}
if (node && node.data < node1.data && node.data < node2.data) {
return traverse(node.right);
}
return node;
}
return traverse(nodeToTraverse);
}
_removeRecursively(data, node = this.root) {
if (!node) return null;
// Data is smaller than the value of the current node
// Look for data in the left subtree
if (data < node.data) {
node.left = this._removeRecursively(data, node.left);
// Data is greater than the value of the current node
// Look for data in the right subtree
} else if (data > node.data) {
node.right = this._removeRecursively(data, node.right);
// Found the node with data
} else {
// There is no left child, return the right child
// Or there are no children - return null as a right child
if (!node.left) {
return node.right;
// There is no right child, return the left child
} else if (!node.right) {
return node.left;
// There are two children
} else {
// Find the next biggest node (minimum node in the right branch)
// to replace current node with.
const nextBigNode = this.findMin(node.right);
// Replace the data
node.data = nextBigNode.data;
// Recursively remove the next biggest node
node.right = this._removeRecursively(nextBigNode.data, node.right);
}
}
return node;
}
removeRecursively(data) {
let removedNode = this.find(data);
if (!removedNode) return null;
removedNode = Object.assign({}, removedNode,
{
left: null,
right: null
});
this.root = this._removeRecursively(data);
return removedNode;
}
remove(data) {
const { parentNode, currentNode } = this.findNodeWithParent(data);
if (!currentNode) return null;
const removedNode = Object.assign({}, currentNode,
{ left: null, right: null });
// Node has no children.
if (!currentNode.left && !currentNode.right) {
// Node is the root and has no parent
if (!parentNode) {
this.root = null;
// Node is the left child
} else if (parentNode.left && parentNode.left.data === data) {
parentNode.left = null;
// Node is the right child
} else if (parentNode.right && parentNode.right.data === data) {
parentNode.right = null;
}
// Node has two children.
} else if (currentNode.left && currentNode.right) {
// Find the next biggest node (minimum node in the right branch)
// to replace current node with.
const { nextBigNode, nextBigNodeParent } = this.findNextBigNodeWithParent(currentNode);
currentNode.data = nextBigNode.data;
// Node is direct parent of the next biggest node
if (nextBigNodeParent === currentNode) nextBigNodeParent.right = nextBigNode.right;
// Node is not direct parent of the next biggest node
else nextBigNodeParent.left = nextBigNode.right;
// Node has only one child.
} else {
const nextNode = currentNode.left || currentNode.right;
currentNode.data = nextNode.data;
currentNode.left = nextNode.left;
currentNode.right = nextNode.right;
}
return removedNode;
}
breadthFirstSearch(nodeToTraverse = this.root) {
const data = [];
if (!nodeToTraverse) return data;
function traverse(node, depth) {
data[depth] = data[depth] || [];
data[depth].push(node.data);
if (node.left) traverse(node.left, depth + 1);
if (node.right) traverse(node.right, depth + 1);
}
traverse(nodeToTraverse, 0);
return [].concat(...data);
}
depthFirstSearchPreOrder(nodeToTraverse = this.root) {
const data = [];
if (!nodeToTraverse) return data;
function traverse(node) {
data.push(node.data);
if (node.left) traverse(node.left);
if (node.right) traverse(node.right);
}
traverse(nodeToTraverse);
return data;
}
depthFirstSearchPostOrder(nodeToTraverse = this.root) {
const data = [];
if (!nodeToTraverse) return data;
function traverse(node) {
if (node.left) traverse(node.left);
if (node.right) traverse(node.right);
data.push(node.data);
}
traverse(nodeToTraverse);
return data;
}
depthFirstSearchInOrder(nodeToTraverse = this.root) {
const data = [];
if (!nodeToTraverse) return data;
function traverse(node) {
if (node.left) traverse(node.left);
data.push(node.data);
if (node.right) traverse(node.right);
}
traverse(nodeToTraverse);
return data;
}
getBalancedTree(node = this.root) {
if (!node) return node;
const sortedData = this.depthFirstSearchInOrder(node);
function balance(start, end) {
if (start > end) return null;
const middle = Math.floor((start + end) / 2);
const node = new BinarySearchTreeNode(sortedData[middle]);
node.left = balance(start, middle - 1);
node.right = balance(middle + 1, end);
return node;
}
return new BinarySearchTree(balance(0, sortedData.length - 1));
}
getTreeFormattedMatrix(node = this.root) {
const height = this.getHeight(node);
const width = 2 ** height - 1;
const matrix = Array.from({ length: height },
() => Array.from({ length: width }, () => ''));
function fillMatrixHelper(node, row, left, right) {
const middle = Math.floor((left + right) / 2);
matrix[row][middle] = `${node.data}`;
if (node.left) fillMatrixHelper(node.left, row + 1, left, middle - 1);
if (node.right) fillMatrixHelper(node.right, row + 1, middle + 1, right);
}
fillMatrixHelper(node, 0, 0, width - 1);
return matrix;
}
printTree(node = this.root) {
const maxNumberWidth = Math.max(
`${this.findMax(node).data}`.length,
`${this.findMin(node).data}`.length
);
const matrix = this.getTreeFormattedMatrix(node)
.map(row => row.map(item => {
const lengthDiff = maxNumberWidth - item.length;
const prefixLength = Math.ceil(lengthDiff / 2);
return ' '.repeat(prefixLength) + item + ' '.repeat(lengthDiff - prefixLength);
}));
matrix.forEach(row => console.log(row.join(' ')));
}
}
const binarySearchTree1 = new BinarySearchTree();
binarySearchTree1.insert(15).insert(20).insert(10).insert(12).insert(8).insert(13);
console.log('---BinarySearchTree1---');
console.log('min:', binarySearchTree1.findMin().data); // 8
console.log('max:', binarySearchTree1.findMax().data); // 20
console.log('Contains 10:', binarySearchTree1.contains(10)); // true
console.log('Remove 10:', binarySearchTree1.remove(10)); // { data: 10, left: null, right: null }
console.log('root.data after the removal:', binarySearchTree1.root.data); // 15
console.log('root.left.data after the removal:', binarySearchTree1.root.left.data); // 12
console.log('root.left.right.data after the removal:', binarySearchTree1.root.left.right.data); // 13
console.log('root.left.left.data after the removal:', binarySearchTree1.root.left.left.data); // 8
console.log('height:', binarySearchTree1.getHeight()); // 3
console.log('minHeight:', binarySearchTree1.getMinHeight()); // 2
console.log('Is balanced:', binarySearchTree1.isBalanced()); // true
const binarySearchTree2 = new BinarySearchTree();
binarySearchTree2.insert(22).insert(49).insert(85).insert(66).insert(95).insert(90).insert(100).insert(88).insert(93).insert(89);
console.log('---BinarySearchTree2---');
console.log('Recursively remove 85:', binarySearchTree2.removeRecursively(85)); // { data: 85, left: null, right: null }
console.log('root.data after the removal:', binarySearchTree2.root.data); // 22
console.log('root.right.right.data after the removal:', binarySearchTree2.root.right.right.data); // 88
console.log('root.right.right.right.left.left.data after the removal:', binarySearchTree2.root.right.right.right.left.left.data); // 89
console.log('Find second largest:', binarySearchTree2.findSecondLargest().data); // 95
console.log('Find lowest common ancestor:', binarySearchTree2.findLowestCommonAncestor(
binarySearchTree2.root.right.right.right.left.left,
binarySearchTree2.root.right.right
).data); // 88
console.log('BreadthFirst:', binarySearchTree2.breadthFirstSearch()); // [ 22, 49, 88, 66, 95, 90, 100, 89, 93 ]
console.log('PreOrder:', binarySearchTree2.depthFirstSearchPreOrder()); // [ 22, 49, 88, 66, 95, 90, 89, 93, 100 ]
console.log('PostOrder:', binarySearchTree2.depthFirstSearchPostOrder()); // [ 66, 89, 93, 90, 100, 95, 88, 49, 22 ]
console.log('InOrder:', binarySearchTree2.depthFirstSearchInOrder()); // [ 22, 49, 66, 88, 89, 90, 93, 95, 100 ]
console.log('height:', binarySearchTree2.getHeight()); // 6
console.log('minHeight:', binarySearchTree2.getMinHeight()); // 4
console.log('Is balanced:', binarySearchTree2.isBalanced()); // false
const balancedTree = binarySearchTree2.getBalancedTree();
console.log('Is balanced:', balancedTree.isBalanced()); // true
console.log('BreadthFirst after balancing:', balancedTree.breadthFirstSearch()); // [ 89, 49, 93, 22, 66, 90, 95, 88, 100 ]
binarySearchTree2.invert();
console.log('InOrder after the inversion:', binarySearchTree2.depthFirstSearchInOrder()); // [ 100, 95, 93, 90, 89, 88, 66, 49, 22 ]