Given a binary search tree and a node of the binary search tree, the task is to delete the node from the Binary Search tree Iteratiely.
Here are the three cases that arise while performing a delete operation on a BST:
Case 1: Node to be deleted is a leaf node. Directly delete the node from the tree.
10 10 / \ delete(5) / \ 7 15 ---------> 7 15 / \ / \ \ / \ 5 8 11 18 8 11 18
Case 2: Node to be deleted is an internal node with two children. Copy the contents of the inorder successor of the node to be deleted and delete the inorder successor. The inorder successor can be found by finding the minimum element in the right subtree of the node.
inorderSuccessor(10) = 11.
10 11 / \ delete(10) / \ 7 15 ---------> 7 15 / \ / \ / \ \ 5 8 11 18 5 8 18
Case 3: Node to be deleted is an internal node with one child. For this case, delete the node and move its child up to take its place.
10 10 / \ delete(15) / \ 7 15 ---------> 7 11 / \ / / \ 5 8 11 5 8
The intuition behind deleting the inorder successor in Case 2 is that the inorder successor of a node with two children will always be greater than all elements in the left sub-tree of the of the node since it is the smallest node in the right sub-tree of the node and the inorder successor of the node will always be smaller than all other nodes in the right sub-tree of the node.
This preserves the BST property of all nodes in the left sub-tree of a given node are smaller than the given node and all nodes in the right sub-tree of the given node are greater than the given node.
Below is the implementation of the above approach:
Inorder traversal of original BST: 5 7 8 10 11 15 18 Deletion of 11 Inorder traversal post deletion: 5 7 8 10 15 18 Deletion of 15 Inorder traversal post deletion: 5 7 8 10 18 Deletion of 10 Inorder traversal post deletion: 5 7 8 18
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Binary Search Tree | Set 2 (Delete)
- Iterative searching in Binary Search Tree
- Binary Tree to Binary Search Tree Conversion using STL set
- Binary Search Tree | Set 1 (Search and Insertion)
- Binary Tree to Binary Search Tree Conversion
- Difference between Binary Tree and Binary Search Tree
- Convert a Binary Search Tree into a Skewed tree in increasing or decreasing order
- Count the Number of Binary Search Trees present in a Binary Tree
- Deleting a binary tree using the delete keyword
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Find the node with minimum value in a Binary Search Tree
- Lowest Common Ancestor in a Binary Search Tree.
- Inorder Successor in Binary Search Tree
- Optimal Binary Search Tree | DP-24
- How to handle duplicates in Binary Search Tree?
- Check if a given array can represent Preorder Traversal of Binary Search Tree
- Construct a Binary Search Tree from given postorder
- Check if given sorted sub-sequence exists in binary search tree
- Find the closest element in Binary Search Tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.