mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 01:18:23 +08:00
* removeValue method of BST implemented #687 * code has formatted for the file BinarySearchTree.js
This commit is contained in:
@ -11,7 +11,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
// class Node
|
// class Node
|
||||||
const Node = (function () {
|
const Node = (function Node () {
|
||||||
// Node in the tree
|
// Node in the tree
|
||||||
function Node (val) {
|
function Node (val) {
|
||||||
this.value = val
|
this.value = val
|
||||||
@ -23,9 +23,9 @@ const Node = (function () {
|
|||||||
Node.prototype.search = function (val) {
|
Node.prototype.search = function (val) {
|
||||||
if (this.value === val) {
|
if (this.value === val) {
|
||||||
return this
|
return this
|
||||||
} else if (val < this.value && this.left != null) {
|
} else if (val < this.value && this.left !== null) {
|
||||||
return this.left.search(val)
|
return this.left.search(val)
|
||||||
} else if (val > this.value && this.right != null) {
|
} else if (val > this.value && this.right !== null) {
|
||||||
return this.right.search(val)
|
return this.right.search(val)
|
||||||
}
|
}
|
||||||
return null
|
return null
|
||||||
@ -34,13 +34,13 @@ const Node = (function () {
|
|||||||
// Visit a node
|
// Visit a node
|
||||||
Node.prototype.visit = function () {
|
Node.prototype.visit = function () {
|
||||||
// Recursively go left
|
// Recursively go left
|
||||||
if (this.left != null) {
|
if (this.left !== null) {
|
||||||
this.left.visit()
|
this.left.visit()
|
||||||
}
|
}
|
||||||
// Print out value
|
// Print out value
|
||||||
console.log(this.value)
|
console.log(this.value)
|
||||||
// Recursively go right
|
// Recursively go right
|
||||||
if (this.right != null) {
|
if (this.right !== null) {
|
||||||
this.right.visit()
|
this.right.visit()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -48,13 +48,13 @@ const Node = (function () {
|
|||||||
// Add a node
|
// Add a node
|
||||||
Node.prototype.addNode = function (n) {
|
Node.prototype.addNode = function (n) {
|
||||||
if (n.value < this.value) {
|
if (n.value < this.value) {
|
||||||
if (this.left == null) {
|
if (this.left === null) {
|
||||||
this.left = n
|
this.left = n
|
||||||
} else {
|
} else {
|
||||||
this.left.addNode(n)
|
this.left.addNode(n)
|
||||||
}
|
}
|
||||||
} else if (n.value > this.value) {
|
} else if (n.value > this.value) {
|
||||||
if (this.right == null) {
|
if (this.right === null) {
|
||||||
this.right = n
|
this.right = n
|
||||||
} else {
|
} else {
|
||||||
this.right.addNode(n)
|
this.right.addNode(n)
|
||||||
@ -62,6 +62,45 @@ const Node = (function () {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// remove a node
|
||||||
|
Node.prototype.removeNode = function (val) {
|
||||||
|
if (val === this.value) {
|
||||||
|
if (!this.left && !this.right) {
|
||||||
|
return null
|
||||||
|
} else {
|
||||||
|
if (this.left) {
|
||||||
|
const leftMax = maxVal(this.left)
|
||||||
|
this.value = leftMax
|
||||||
|
this.left = this.left.removeNode(leftMax)
|
||||||
|
} else {
|
||||||
|
const rightMin = minVal(this.right)
|
||||||
|
this.value = rightMin
|
||||||
|
this.right = this.right.removeNode(rightMin)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (val < this.value) {
|
||||||
|
this.left = this.left && this.left.removeNode(val)
|
||||||
|
} else if (val > this.value) {
|
||||||
|
this.right = this.right && this.right.removeNode(val)
|
||||||
|
}
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
|
// find maximum value in the tree
|
||||||
|
const maxVal = function (node) {
|
||||||
|
if (!node.right) {
|
||||||
|
return node.value
|
||||||
|
}
|
||||||
|
return maxVal(node.right)
|
||||||
|
}
|
||||||
|
|
||||||
|
// find minimum value in the tree
|
||||||
|
const minVal = function (node) {
|
||||||
|
if (!node.left) {
|
||||||
|
return node.value
|
||||||
|
}
|
||||||
|
return minVal(node.left)
|
||||||
|
}
|
||||||
// returns the constructor
|
// returns the constructor
|
||||||
return Node
|
return Node
|
||||||
}())
|
}())
|
||||||
@ -75,6 +114,10 @@ const Tree = (function () {
|
|||||||
|
|
||||||
// Inorder traversal
|
// Inorder traversal
|
||||||
Tree.prototype.traverse = function () {
|
Tree.prototype.traverse = function () {
|
||||||
|
if (!this.root) {
|
||||||
|
console.log('No nodes are there in the tree till now')
|
||||||
|
return
|
||||||
|
}
|
||||||
this.root.visit()
|
this.root.visit()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,13 +134,19 @@ const Tree = (function () {
|
|||||||
// Add a new value to the tree
|
// Add a new value to the tree
|
||||||
Tree.prototype.addValue = function (val) {
|
Tree.prototype.addValue = function (val) {
|
||||||
const n = new Node(val)
|
const n = new Node(val)
|
||||||
if (this.root == null) {
|
if (this.root === null) {
|
||||||
this.root = n
|
this.root = n
|
||||||
} else {
|
} else {
|
||||||
this.root.addNode(n)
|
this.root.addNode(n)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// remove a value from the tree
|
||||||
|
Tree.prototype.removeValue = function (val) {
|
||||||
|
// remove something if root exists
|
||||||
|
this.root = this.root && this.root.removeNode(val)
|
||||||
|
}
|
||||||
|
|
||||||
// returns the constructor
|
// returns the constructor
|
||||||
return Tree
|
return Tree
|
||||||
}())
|
}())
|
||||||
@ -112,3 +161,6 @@ bst.addValue(8)
|
|||||||
bst.addValue(4)
|
bst.addValue(4)
|
||||||
bst.traverse()
|
bst.traverse()
|
||||||
bst.search(8)
|
bst.search(8)
|
||||||
|
bst.removeValue(3)
|
||||||
|
bst.removeValue(8)
|
||||||
|
bst.traverse()
|
||||||
|
12433
package-lock.json
generated
12433
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user