If we want to delete a node with two children in a BST, which node can replace it?Its immediate left childIts immediate right childIts inorder predecessor or inorder successorThe root node
Question
If we want to delete a node with two children in a BST, which node can replace it?Its immediate left childIts immediate right childIts inorder predecessor or inorder successorThe root node
Solution 1
When we want to delete a node with two children in a Binary Search Tree (BST), the node that can replace it is its inorder predecessor or its inorder successor.
Here are the steps:
-
First, find the node to be deleted. If the node is found, there are three cases to consider:
-
Node to be deleted is a leaf: Simply remove from the tree.
-
Node to be deleted has only one child: Copy the child to the node and delete the child.
-
Node to be deleted has two children: Find inorder predecessor or inorder successor of the node. Copy contents of the inorder predecessor or inorder successor to the node and delete the inorder predecessor or inorder successor.
-
-
If the node has two children, find its inorder predecessor or inorder successor.
-
Inorder Predecessor: Inorder predecessor of a node is the next lower node in BST. To find inorder predecessor, we simply need to find the maximum value in the right sub-tree of the node.
-
Inorder Successor: Inorder successor of a node is the next higher node in BST. To find inorder successor, we simply need to find the minimum value in the left sub-tree of the node.
-
-
Replace the node with its inorder predecessor or inorder successor. This is because both of them would still preserve the BST property.
-
Delete the inorder predecessor or inorder successor.
Note: The root node, immediate left child, or immediate right child cannot always replace the deleted node as it may not preserve the BST property.
Solution 2
In a Binary Search Tree (BST), if we want to delete a node with two children, we can replace it with its inorder predecessor or its inorder successor.
Here are the steps:
- First, find the node to delete in the BST.
- If the node is found, there are three cases to consider:
- Node to be deleted has no children.
- Node to be deleted has two children.
- Node to be deleted has only one child.
- Since the node has two children, find the inorder predecessor or the inorder successor.
- The inorder predecessor is the node with the maximum value in the left subtree of the node. The inorder successor is the node with the minimum value in the right subtree of the node.
- Replace the node with its inorder predecessor or inorder successor.
- If the inorder predecessor or successor has a child, adjust the links to remove the replaced node completely from the tree.
- Finally, delete the node.
Please note that the immediate left child or the immediate right child or the root node cannot replace the node with two children in a BST.
Solution 3
Its inorder predecessor or inorder successor can replace the node with two children in a BST (Binary Search Tree).
Here are the steps:
-
If the node to be deleted has two children, find its inorder predecessor or inorder successor. The inorder predecessor is the node with the maximum value in the left subtree, and the inorder successor is the node with the minimum value in the right subtree.
-
Replace the value of the node to be deleted with the value of the inorder predecessor or inorder successor. This means the node to be deleted now contains the value of its inorder predecessor or inorder successor.
-
Finally, delete the inorder predecessor or inorder successor. Since the inorder predecessor or inorder successor will always be a node with at most one child (it is either a leaf node or a node with only one child), deleting it can be done as per the cases with 0 or 1 child.
This method ensures that the property of the Binary Search Tree (BST) is maintained after deletion.
Similar Questions
Which of the following is a correct method to delete a node with one child in a BST?Replace the node with its childReplace the node with the rootReplace the node with its inorder predecessorDelete the subtree containing the node
Consider the following statements:P: If the root node of a BST is deleted, it can be replaced by inorder predecessor. Q: If the root node of a BST is deleted, it can be replaced by preorder successor. Which of the following is/are CORRECT?
Which node is promoted when a node with more then one child is removed from a BST.AAll the optionsBTravel to the right or left sub tree and choose smallest or biggest respectively.CTravel to the right or left sub tree and choose the biggest or smallest respectively.DAny node can be promoted
In a BST, if a node has no children, it is known as a ______.root nodeleaf nodeinternal nodesubnode
Which type of tree node's in-order successor of a node with two children is the left-most child?Note: This kind of question will be helpful in clearing Infosys recruitment.Marks : 1Negative Marks : 0Answer hereRight subtreeLeft subtreeBoth right and left subtreesNone of the mentioned options
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.