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
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.
Sp 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.
- Data Structures | Binary Search Trees | Question 3
- Data Structures | Binary Search Trees | Question 4
- Data Structures | Tree Traversals | Question 5
- Data Structures | Binary Trees | Question 6
- Data Structures | Binary Search Trees | Question 6
- Advantages of vector over array in C++
- Data Structures and Algorithms | Set 37
- Data Structures and Algorithms | Set 36
- Data Structures | Heap | Question 12
- Data Structures | Linked List | Question 17