Skip to content
Official PYQ · 2020

GATE CS 2020 Q41 — Binary Search Trees

Explain with AI

In a balanced binary search tree with nn elements, what is the worst case time complexity of reporting all elements in range [a,b][a,b]? Assume that the number of reported elements is kk.

Choose one option
View worked solution

Answer: B

To find and report all elements in the range [a,b][a, b] within a balanced Binary Search Tree (BST) containing nn elements, we can use a standard range-search algorithm:

  1. Locate the boundaries: We search for the keys aa and bb in the BST. Since the tree is balanced, searching for any key takes Θ(log⁡n)\Theta(\log n) time. This identifies the split node where the paths to aa and bb diverge.
  2. Traverse the range: From the split node, we traverse down towards aa and bb. For any node xx visited:
    • If xx is within the range [a,b][a, b], we report xx and recursively explore its subtrees.
    • If a subtree lies entirely within the range [a,b][a, b], we can report all its elements using a standard in-order traversal. The time taken to traverse a subtree containing mm elements is Θ(m)\Theta(m).

Complexity Analysis:

  • The search paths from the root to aa and bb have a maximum depth of O(log⁡n)O(\log n). Thus, the number of nodes visited on these boundary paths is O(log⁡n)O(\log n).
  • All other visited nodes belong to subtrees that lie entirely within the range [a,b][a, b]. The time spent traversing these subtrees is directly proportional to the number of elements reported, which is Θ(k)\Theta(k).

Combining these two parts, the total worst-case time complexity is:

Θ(log⁡n+k)\Theta(\log n + k)

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

Published
Report a correction
Keep exploringMore Binary Search Trees questions