Open In App

GATE | GATE CS 2020 | Question 51

Like Article
Like
Save
Share
Report

In a balanced binary search tree with n elements, what is the worst-case time complexity of reporting all elements in the 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:

For the range [a, b], we first need to check if a and b are present in BST.

  1. Time complexity to check if element ‘a’ is present in BST = O(log n)
  2. Time complexity to check if element ‘b’ is present in BST = O(log n)

For finding all the elements in the range [a, b], we have to apply in-order sorting from a to b.

In this way, we will lay every successor of reaching to the predecessor of b and all the elements between a and b will be reported.

For k elements, the in-order sorting will take O(k) time.

Hence, O(log n) + O(log n) + O(k) = O(2logn + k) = O(log(n) + k)


Quiz of this Question
Please comment below if you find anything wrong in the above post


Last Updated : 17 Sep, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads