Skip to content
Official PYQ · 2021

GATE CS 2021 Set 1 Q10 — Binary Search Trees

Explain with AI

A binary search tree TT contains nn distinct elements. What is the time complexity of picking an element in TT that is smaller than the maximum element in TT?

Choose one option
View worked solution

Answer: D

To find any element in a Binary Search Tree (BST) TT that is strictly smaller than the maximum element, we can utilize the structural properties of a BST. Assuming n≥2n \ge 2 (so that such an element always exists):

Let the root of the BST be RR. We can determine a valid element in Θ(1)\Theta(1) time by checking the right child of RR:

  1. Case 1: RR has a right child (R.right≠NULLR.\text{right} \neq \text{NULL})
    The maximum element of the BST must reside in the right subtree of RR. Since all elements in the right subtree are strictly greater than RR, the root RR itself is guaranteed to be smaller than the maximum element. Thus, we can simply return RR.
  2. Case 2: RR does not have a right child (R.right==NULLR.\text{right} == \text{NULL})
    The root RR itself is the maximum element of the BST. Since n≥2n \ge 2, the left child of RR must exist. Any element in the left subtree is strictly smaller than RR (the maximum). Thus, we can simply return the left child of RR.

In both cases, we only need to inspect the root and its immediate children. This requires a constant number of operations, resulting in a time complexity of Θ(1)\Theta(1).

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Binary Search Trees questions