Open In App

Data Structures | Binary Search Trees | Question 2

Like Article
Like
Save Article
Save
Share
Report issue
Report

In delete operation of BST, we need inorder successor (or predecessor) of a node when the node to be deleted has both left and right child as non-empty. Which of the following is true about inorder successor needed in delete operation?

(A)

Inorder Successor is always a leaf node

(B)

Inorder successor is always either a leaf node or a node with empty left child

(C)

Inorder successor may be an ancestor of the node

(D)

Inorder successor is always either a leaf node or a node with empty right child


Answer: (B)

Explanation:

Let X be the node to be deleted in a tree with root as \’root\’. There are three cases for deletion 1) X is a leaf node: We change left or right pointer of parent to NULL (depending upon whether X is left or right child of its parent) and we delete X 2) One child of X is empty: We copy values of non-empty child to X and delete the non-empty child 3) Both children of X are non-empty: In this case, we find inorder successor of X. Let the inorder successor be Y. We copy the contents of Y to X, and delete Y. So, we need inorder successor only when both left and right child of X are not empty. In this case, the inorder successor Y can never be an ancestor of X. In this case, the inorder successor is the leftmost node in right subtree of X. Since it is leftmost node, the left child of Y must be empty.


Quiz of this Question
Please comment below if you find anything wrong in the above post


Last Updated : 02 Feb, 2013
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads