-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBST.js
64 lines (59 loc) · 1.37 KB
/
BST.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
class Node {
constructor(val) {
this.val = val;
this.left = null;
this.right = null;
}
insert(root, val) {
if (!root) {
return new Node(val);
}
if (root.val > val) {
root.left = this.insert(root.left, val);
} else {
root.right = this.insert(root.right, val);
}
return root;
}
min(root) {
let node = root;
while (node && node.left) {
node = node.left;
}
return node;
}
/**
* case 1 : 1 or 0 child return left or right (42, 44)
* case 2 : min of right sub tree + replace the root val + remove the min of right sub tree
*/
delete(root, val) {
if (!root) {
return null;
}
if (root.val > val) {
root.left = this.delete(root.left, val);
} else if (root.val < val) {
root.right = this.delete(root.right, val);
} else {
if (!root.left) {
return root.right;
} else if (!root.right) {
return root.left;
} else {
const min = this.min(root.right);
root.val = min.val;
root.right = this.delete(root.right, root.val);
}
}
return root;
}
}
const root = new Node(5);
root.insert(root, 2);
root.insert(root, 3);
root.insert(root, 1);
root.insert(root, 7);
root.insert(root, 8);
root.insert(root, 6);
root.delete(root, 5);
console.log(root);