Prove that K-successive calls to tree successor takes O(k+h) time. Since each node is visited atmost twice the maximum bound on number of nodes visited must be 2k. The time complexity must be O(k). I dont get where is the factor of O(h) coming. Is it because of nodes which are visited but are not the successor. I am not exactly able to explain myself how is the factor O(h) is involved in the whole process
PS:I know this question already exists but I was not able to understand the solution.
Plus in the O(k+h)
notation is an alternative form of writing O(MAX(k, h))
.
Finding a successor once could take up to O(h)
time. To see why this is true, consider a situation when you are looking for a successor of the rightmost node of the left subtree of the root: its successor is at the bottom of the right subtree, so you must traverse the height of the tree twice. That's why you need to include h
in the calculation: if k
is small compared to h
, then h
would dominate the timing of the algorithm.
The point of the exercise is to prove that the time of calling the successor k
times in a row is not O(k*h)
, as one could imagine after observing that a single call could take up to O(h)
. You prove it by showing that the cost of traversing the height of the tree is distributed among the k
calls, as you did by noting that each node is visited at most twice.