IAE Academy

ISRO CS 2023 Q.26: solution and answer

ISRO 2023 · CSQ.26 · 1 mark · MCQ

What is the Inorder predecessor of 15 in the Binary Search Tree shown below

ISRO CS 2023 question 26: A binary search tree with root 15.Tap to open full size
Figure from the ISRO CS 2023 paper. It is part of the question.
Figure description

A binary search tree with root 15. Left child of 15 is 6; right child of 15 is 18. Node 6 has left child 3 and right child 7. Node 3 has left child 2 and right child 4. Node 7 has only a right child 13. Node 13 has only a left child 9. Node 18 has left child 17 and right child 20.

  1. (A)

    18

  2. (B)

    7

  3. (C)

    9

  4. (D)

    13

Answer and solution

ANSWER

The inorder predecessor of 15 is 13, option (D).

Official answer: DSource: ISRO's revised and final answer key of 16 February 2024

  1. The inorder predecessor of a node is the key visited just before it in an inorder traversal (left subtree, node, right subtree). When the node has a left subtree, that key is the largest key in the left subtree.

  2. Node has a left subtree rooted at , so the predecessor is the maximum of that subtree.

  3. The maximum of a binary search tree is found by following right children until none remains: .

  4. Node has only a left child () and no right child, so the walk stops at . Every key in 's subtree is at most , so is the maximum of the left subtree of .

  5. Check with the full inorder sequence: . The key immediately before is .

  6. The other options fail: lies in the right subtree of , so it comes after in inorder; and are in the left subtree but are smaller than , so they are not the last key visited before .

KEY IDEA

If a node has a left subtree, its inorder predecessor is the rightmost (largest) node of that left subtree: go left once, then right as far as possible.

WATCH OUT FOR

Stopping at the left child (6) or at the right child of 6 (7), or picking the deepest node (9), instead of continuing along right children until there is none. Following the last leaf (9) goes one step too far, because 13 has no right child.

From the ISRO CS 2023 paper. Every question of this paper · The full paper (PDF, ISRO) · All 3 ISRO Trees & heaps questions, with notes

More ISRO Trees & heaps questions