Skip to content
Official PYQ · 2014

GATE CS 2014 Set 3 Q39 — Binary Search Trees

Explain with AI
Suppose we have a balanced binary search tree TT holding nn numbers. We are given two numbers LL and HH and wish to sum up all the numbers in TT that lie between LL and HH. Suppose there are mm such numbers in TT. If the tightest upper bound on the time to compute the sum is O(nalog⁡bn+mclog⁡dn)O(n^a\log^bn+m^c\log^dn), the value of a+10b+100c+1000da+10b+100c+1000d is ______.
View worked solution

Answer: 110

To find the sum of all elements in a balanced binary search tree (BST) TT of size nn that fall within the range [L,H][L, H] (where there are mm such elements), we can perform a standard range search:

  1. Search for the boundaries: We search for LL and HH in the BST. Since the tree is balanced, the height of the tree is O(log⁡n)O(\log n). Finding the split node and traversing down to the boundaries takes O(log⁡n)O(\log n) time.
  2. Traverse and sum the elements: The elements lying strictly between the search paths of LL and HH belong to subtrees that are completely inside the range [L,H][L, H]. We can traverse these subtrees to sum their elements. Since there are exactly mm such elements, traversing them takes O(m)O(m) time.

Thus, the tightest upper bound on the time complexity to compute the sum is:

T(n,m)=O(log⁡n+m)T(n, m) = O(\log n + m)

We are given the upper bound in the form:

O(nalog⁡bn+mclog⁡dn)O(n^a \log^b n + m^c \log^d n)

Comparing O(log⁡n+m)O(\log n + m) with O(nalog⁡bn+mclog⁡dn)O(n^a \log^b n + m^c \log^d n):

  • For the first term nalog⁡bn=log⁡nn^a \log^b n = \log n, we get:
    a=0a = 0 and b=1b = 1
  • For the second term mclog⁡dn=mm^c \log^d n = m, we get:
    c=1c = 1 and d=0d = 0

Now, we calculate the required value:

Value=a+10b+100c+1000d=0+10(1)+100(1)+1000(0)=110\begin{aligned} \text{Value} &= a + 10b + 100c + 1000d \\ &= 0 + 10(1) + 100(1) + 1000(0) \\ &= 110 \end{aligned}

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

Published
Report a correction
Keep exploringMore Binary Search Trees questions