Theory-of-Computation
November 9, 2023
Big-Data
November 9, 2023
Theory-of-Computation
November 9, 2023
Big-Data
November 9, 2023

GATE 2020

Question 48

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

A
θ(n log k)
B
θ(log n + k)
C
θ(k log n)
D
θ(log n)
Question 48 Explanation: 
The idea is to traverse the given binary search tree starting from root. For every node being visited, check if this node lies in range, if yes, then add 1 to result and recur for both of its children. If the current node is smaller than the low value of range, then recur for right child, else recur for left child.
Time complexity of the above program is O(h + k) where h is the height of BST and k is the number of nodes in a given range.
Here h is log n, hence O(log n+k).
Correct Answer: B
Question 48 Explanation: 
The idea is to traverse the given binary search tree starting from root. For every node being visited, check if this node lies in range, if yes, then add 1 to result and recur for both of its children. If the current node is smaller than the low value of range, then recur for right child, else recur for left child.
Time complexity of the above program is O(h + k) where h is the height of BST and k is the number of nodes in a given range.
Here h is log n, hence O(log n+k).

Leave a Reply

Your email address will not be published. Required fields are marked *