MCS-208 · Unit 7

Advanced Trees

Section 2 of 9

Block 3 · Unit 7Advanced Trees10 min · Section 2 of 9

7.2Deleting a Node from a Binary Search Tree

Covers IGNOU MCS-208 §7.2.3

Deletion is the hardest of the basic BST operations, because removing a node must leave a valid BST behind. There are three cases, depending on how many children the node has.

The three cases

  1. No children (leaf): remove it; its parent’s link becomes NULL.
  2. One child: the child takes the node’s place: the parent’s link now points to the child.
  3. Two children: find the inorder successor ss, the smallest key in the node’s right subtree (go right once, then left as far as possible). Copy ss‘s key into the node, then delete ss from the right subtree. Because ss has no left child, deleting it is case 1 or 2.

The inorder predecessor (the largest key in the left subtree) works equally well in case 3.

Deletion follows one path to find the node and possibly one more path to find the successor: O(h)O(h).

Key takeaways

  • Case 1: leaf — just remove. Case 2: one child — child moves up.
  • Case 3: two children — copy the inorder successor (min of right subtree), then delete it.
  • The successor never has a left child, so its own deletion is easy.
  • Deletion costs O(h)O(h).

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

In a BST, which node replaces a deleted node that has two children (using the successor method)?

Show answer

Answer: C. The smallest node in its right subtree

The smallest key larger than the deleted key keeps the order intact. The right child is the successor only if it has no left child.

Question 2 · multiple choice

In the starting tree of the example, what is the inorder successor of 30?

Show answer

Answer: B. 35

Go right to 40, then left as far as possible: 35. 40 would be the answer only if it had no left child.

Question 3 · multiple choice

The node 60 has only a right child 65. After deleting 60, what is 65’s new parent?

Show answer

Answer: A. 70, the old parent of 60

In the one-child case the child is linked directly to the deleted node’s parent.

Question 4 · multiple choice

Why does the inorder successor of a node with two children never have a left child?

Show answer

Answer: C. Because if it had a left child, that child would be smaller and would be the successor instead

The successor is the minimum of the right subtree, found by going left until no left child exists. It may have a right child, so it is not always a leaf.

Question 5 · multiple choice

What does this program print?

C · q7-delete.c
#include <stdio.h>
#include <stddef.h>

typedef struct n { struct n *l; int k; struct n *r; } N;

int main(void)
{
    N e = {NULL, 55, NULL}, d = {&e, 60, NULL}, c = {NULL, 20, NULL};
    N root = {&c, 40, &d};
    /* delete 40 (two children): find the successor */
    N *s = root.r;
    while (s->l != NULL)
        s = s->l;
    printf("successor of %d is %d\n", root.k, s->k);
    return 0;
}
Show answer

Answer: D. successor of 40 is 55

Right to 60, then left to 55, which has no left child. 55 is the smallest key larger than 40.

Question 6 · short answer

Delete 30 from the BST 50(30(20, 40(35, 45)), 70) and draw the result, explaining each step.

Show worked answer

30 has two children, so find its inorder successor: go right to 40, then left to 35, which has no left child: the successor is 35. Copy 35 into the node that held 30, then delete 35 from the right subtree of that node; 35 is a leaf, so it is simply removed. Result: 50(35(20, 40(-, 45)), 70). Inorder check: 20 35 40 45 50 70 (sorted).

↑ ↓ move↵ openTab switch type