Question
In a balanced binary search tree with elements, what is the worst case time complexity of reporting all elements in range ? Assume that the number of reported elements is .
Official answer and solution
- Official answer
- B
- From
- Official GATE 2020 answer key
Solution
Analyze the search phase to locate the lower bound and the subsequent inorder traversal phase.
Phase 1: Locating the start of the range:
- In a balanced binary search tree of elements (such as an AVL tree or Red-Black tree), tree height is .
- Searching for the smallest key takes time.
Phase 2: Reporting the elements in range :
- From the first key , an inorder traversal continues visiting nodes until encountering a key .
- In an inorder traversal of a binary search tree, the amortized cost to find each successor node is .
- Traversing and outputting the elements within the range, plus reaching the first element , takes time.
Total worst-case time complexity:
Final answer
B
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2020 Computer Science and Information Technology (CS)
- Question
- 51 of 65, printed as CS Q.41
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.