Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Deleting desired node from a Binary Search Tree in JavaScrip
Deleting a node from a Binary Search Tree is more complex than insertion because we need to handle three different cases depending on the node's children. This operation maintains the BST property where left children are smaller and right children are larger.
Problem
We have a Binary Search Tree implementation with insertion functionality. We need to add a deleteNode() function that removes a node with a specific value while maintaining the BST structure.
class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
}
class BinarySearchTree {
constructor() {
this.root = null;
}
insert(data) {
var newNode = new Node(data);
if (this.root === null) {
this.root = newNode;
} else {
this.insertNode(this.root, newNode);
}
}
insertNode(node, newNode) {
if (newNode.data < node.data) {
if (node.left === null) {
node.left = newNode;
} else {
this.insertNode(node.left, newNode);
}
} else {
if (node.right === null) {
node.right = newNode;
} else {
this.insertNode(node.right, newNode);
}
}
}
}
// Create BST and insert nodes
const BST = new BinarySearchTree();
BST.insert(5);
BST.insert(3);
BST.insert(6);
BST.insert(2);
BST.insert(4);
BST.insert(7);
console.log("BST structure:");
console.log(" 5");
console.log(" / ");
console.log(" 3 6");
console.log(" / \ ");
console.log("2 4 7");
BST structure:
5
/ \
3 6
/ \ \
2 4 7
Three Cases for Node Deletion
When deleting a node from a BST, we encounter three scenarios:
- Leaf node: No children - simply remove it
- One child: Replace the node with its child
- Two children: Replace with inorder successor (smallest node in right subtree)
Complete Implementation
class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
}
class BinarySearchTree {
constructor() {
this.root = null;
}
insert(data) {
var newNode = new Node(data);
if (this.root === null) {
this.root = newNode;
} else {
this.insertNode(this.root, newNode);
}
}
insertNode(node, newNode) {
if (newNode.data < node.data) {
if (node.left === null) {
node.left = newNode;
} else {
this.insertNode(node.left, newNode);
}
} else {
if (node.right === null) {
node.right = newNode;
} else {
this.insertNode(node.right, newNode);
}
}
}
deleteNode(root, key) {
if (!root) {
return null;
}
if (key < root.data) {
root.left = this.deleteNode(root.left, key);
} else if (key > root.data) {
root.right = this.deleteNode(root.right, key);
} else {
// Node to delete found
// Case 1: Leaf node or node with one child
if (!root.left) {
return root.right;
} else if (!root.right) {
return root.left;
}
// Case 2: Node with two children
// Find inorder successor (smallest in right subtree)
let successor = root.right;
while (successor.left) {
successor = successor.left;
}
// Replace root's data with successor's data
root.data = successor.data;
// Delete the successor
root.right = this.deleteNode(root.right, successor.data);
}
return root;
}
}
// Helper function to print tree in order
const printInOrder = (node) => {
if (node !== null) {
printInOrder(node.left);
console.log(node.data);
printInOrder(node.right);
}
}
// Test the deletion
const BST = new BinarySearchTree();
BST.insert(5);
BST.insert(3);
BST.insert(6);
BST.insert(2);
BST.insert(4);
BST.insert(7);
console.log("Before deleting node 4:");
printInOrder(BST.root);
console.log("\nAfter deleting node 4:");
BST.root = BST.deleteNode(BST.root, 4);
printInOrder(BST.root);
Before deleting node 4: 2 3 4 5 6 7 After deleting node 4: 2 3 5 6 7
How the Algorithm Works
The deletion algorithm follows these steps:
- Search for the node: Traverse the tree to find the node to delete
-
Handle three cases:
- If node has no children: return
null - If node has one child: return that child
- If node has two children: find inorder successor, replace data, and recursively delete successor
- If node has no children: return
- Return the modified tree: The function returns the updated root
Conclusion
BST node deletion requires handling three distinct cases to maintain the tree structure. The key insight is using the inorder successor for nodes with two children, ensuring the BST property remains intact after deletion.
