GATE | GATE CS 2020 | Question 51

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) Θ(log n)
(B) Θ(log(n)+k)
(C) Θ(k log n)
(D) Θ(n log k)


Answer: (B)

Explanation:

  1. Time complexity to check if element ‘a’ in given balanced binary search tree = O(log n)
  2. Time complexity to check if element ‘a’ in given balanced binary search tree = O(log n)
  3. Now, time complexity to traverse all element in range [a, b], those elements will be inoder sorted = θ(k)
  4. Therefore, total time complexity will be,

    = Θ(log(n)) + Θ(log(n)) + Θ(k)
    = Θ(2(log(n))+k)
    = Θ(log(n)+k) 

    Option (B) is correct.


    Quiz of this Question

    My Personal Notes arrow_drop_up
    Article Tags :

    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.