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$.
it is pic of answer given by https://gateoverflow.in/user/Shikhar.
Can you please confirm, if the logic of my solution is correct?
@Yashdeep2000 your time complexity for statement 1 is wrong because you are traversing the balanced BST which takes O(n) time .
We need to first find the indices of a & b, which takes time of log n + log n (Since it is Balanced BST). Once we find the indices, we can directly print the k elements.
As k can be significantly larger than log n, time complexity is O(log n + k)
@mkagenius
Then , is TIFR question ans wrong? It is telling only logn comparison and logn addition?
logn addition means T.C. also log n ,right?
For checking the Reporting element is in range you will do checking of 2 nodes at each height .
And Between the you can have “k” nodes which will encounter in the counting range .
So total element ( node ) for reporting all element in range [a,b] will be : ( k + 2*h) where k: nodes in the range , 2*h : Total Number of nodes which you will check at each height .
Height ( h) = logn ( BST )
So, worst overall complexity Ɵ(k+2*logn)
64.3k questions
77.9k answers
244k comments
80.0k users