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 .
View worked solution
Answer: B
To find and report all elements in the range within a balanced Binary Search Tree (BST) containing elements, we can use a standard range-search algorithm:
- Locate the boundaries: We search for the keys and in the BST. Since the tree is balanced, searching for any key takes time. This identifies the split node where the paths to and diverge.
- Traverse the range: From the split node, we traverse down towards and . For any node visited:
- If is within the range , we report and recursively explore its subtrees.
- If a subtree lies entirely within the range , we can report all its elements using a standard in-order traversal. The time taken to traverse a subtree containing elements is .
Complexity Analysis:
- The search paths from the root to and have a maximum depth of . Thus, the number of nodes visited on these boundary paths is .
- All other visited nodes belong to subtrees that lie entirely within the range . The time spent traversing these subtrees is directly proportional to the number of elements reported, which is .
Combining these two parts, the total worst-case time complexity is:
Free practice · Your answers on this page are not saved.
