Skip to content

Commit 8c7d592

Browse files
authored
chore: merge "removeValue method implemented to remove a particular node from the BST TheAlgorithms#687" (TheAlgorithms#696)
* removeValue method of BST implemented TheAlgorithms#687 * code has formatted for the file BinarySearchTree.js
1 parent 19970c3 commit 8c7d592

File tree

2 files changed

+67
-12434
lines changed

2 files changed

+67
-12434
lines changed

Data-Structures/Tree/BinarySearchTree.js

+60-8
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212

1313
// class Node
14-
const Node = (function () {
14+
const Node = (function Node () {
1515
// Node in the tree
1616
function Node (val) {
1717
this.value = val
@@ -23,9 +23,9 @@ const Node = (function () {
2323
Node.prototype.search = function (val) {
2424
if (this.value === val) {
2525
return this
26-
} else if (val < this.value && this.left != null) {
26+
} else if (val < this.value && this.left !== null) {
2727
return this.left.search(val)
28-
} else if (val > this.value && this.right != null) {
28+
} else if (val > this.value && this.right !== null) {
2929
return this.right.search(val)
3030
}
3131
return null
@@ -34,34 +34,73 @@ const Node = (function () {
3434
// Visit a node
3535
Node.prototype.visit = function () {
3636
// Recursively go left
37-
if (this.left != null) {
37+
if (this.left !== null) {
3838
this.left.visit()
3939
}
4040
// Print out value
4141
console.log(this.value)
4242
// Recursively go right
43-
if (this.right != null) {
43+
if (this.right !== null) {
4444
this.right.visit()
4545
}
4646
}
4747

4848
// Add a node
4949
Node.prototype.addNode = function (n) {
5050
if (n.value < this.value) {
51-
if (this.left == null) {
51+
if (this.left === null) {
5252
this.left = n
5353
} else {
5454
this.left.addNode(n)
5555
}
5656
} else if (n.value > this.value) {
57-
if (this.right == null) {
57+
if (this.right === null) {
5858
this.right = n
5959
} else {
6060
this.right.addNode(n)
6161
}
6262
}
6363
}
6464

65+
// remove a node
66+
Node.prototype.removeNode = function (val) {
67+
if (val === this.value) {
68+
if (!this.left && !this.right) {
69+
return null
70+
} else {
71+
if (this.left) {
72+
const leftMax = maxVal(this.left)
73+
this.value = leftMax
74+
this.left = this.left.removeNode(leftMax)
75+
} else {
76+
const rightMin = minVal(this.right)
77+
this.value = rightMin
78+
this.right = this.right.removeNode(rightMin)
79+
}
80+
}
81+
} else if (val < this.value) {
82+
this.left = this.left && this.left.removeNode(val)
83+
} else if (val > this.value) {
84+
this.right = this.right && this.right.removeNode(val)
85+
}
86+
return this
87+
}
88+
89+
// find maximum value in the tree
90+
const maxVal = function (node) {
91+
if (!node.right) {
92+
return node.value
93+
}
94+
return maxVal(node.right)
95+
}
96+
97+
// find minimum value in the tree
98+
const minVal = function (node) {
99+
if (!node.left) {
100+
return node.value
101+
}
102+
return minVal(node.left)
103+
}
65104
// returns the constructor
66105
return Node
67106
}())
@@ -75,6 +114,10 @@ const Tree = (function () {
75114

76115
// Inorder traversal
77116
Tree.prototype.traverse = function () {
117+
if (!this.root) {
118+
console.log('No nodes are there in the tree till now')
119+
return
120+
}
78121
this.root.visit()
79122
}
80123

@@ -91,13 +134,19 @@ const Tree = (function () {
91134
// Add a new value to the tree
92135
Tree.prototype.addValue = function (val) {
93136
const n = new Node(val)
94-
if (this.root == null) {
137+
if (this.root === null) {
95138
this.root = n
96139
} else {
97140
this.root.addNode(n)
98141
}
99142
}
100143

144+
// remove a value from the tree
145+
Tree.prototype.removeValue = function (val) {
146+
// remove something if root exists
147+
this.root = this.root && this.root.removeNode(val)
148+
}
149+
101150
// returns the constructor
102151
return Tree
103152
}())
@@ -112,3 +161,6 @@ bst.addValue(8)
112161
bst.addValue(4)
113162
bst.traverse()
114163
bst.search(8)
164+
bst.removeValue(3)
165+
bst.removeValue(8)
166+
bst.traverse()

0 commit comments

Comments
 (0)