Question
Suppose we have a balanced binary search tree holding numbers. We are given two numbers and and wish to sum up all the numbers in that lie between and . Suppose there are such numbers in . If the tightest upper bound on the time to compute the sum is , the value of is _____.
Official answer and solution
- Official answer
- 110
- From
- Official GATE 2014 answer key
Solution
Analyze the time complexity of a range query on a balanced binary search tree and match powers to find the required combination.
-
Range Query on a Balanced BST:
To sum all elements within the range in a balanced BST containing elements:- Step 1: Search for the smallest key in the tree. Because the tree is balanced (height ), this search takes time.
- Step 2: From this starting node, perform an in-order traversal to retrieve each of the keys in range, stopping when a key is encountered.
- The in-order traversal visits each of the reported nodes plus at most boundary nodes.
- Thus, the total time required is:
-
Matching Coefficients:
We are given the tightest upper bound form:Matching this with :
-
Compute the Required Expression:
110
Assuming the range search takes O(m log n) time by searching for each element individually from the root instead of continuing via tree traversal.
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2014 Computer Science and Information Technology (CS) Session 3
- Question
- 49 of 65, printed as CS Q.39
- 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.