3

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.

user2179293
  • 425
  • 9
  • 22
  • Where's a link to the existing question? And typically you should try to clarify an answer through a comment or in chat with the poster, rather than posting a new question. – Bernhard Barker Sep 13 '13 at 15:10
  • That question was 3yrs old – user2179293 Sep 13 '13 at 15:19
  • You mean [this one](http://stackoverflow.com/questions/8454771/how-can-i-prove-this-operation-over-binary-search-trees)? I admit that the second last point isn't clear and the 4th point requires elaboration. Since the answerer was online 4 hours ago, asking for a clarification shouldn't have been a problem. – Bernhard Barker Sep 13 '13 at 15:33
  • The problem with not providing a link is that we have no idea what you already know, so we may go to the effort of writing up an answer very similar to the one there, which you also won't understand. Where we could've just clarified the parts which you didn't understand. – Bernhard Barker Sep 13 '13 at 15:35
  • @user2179293, you might wanna have a look at my answer here: http://stackoverflow.com/questions/40561235/time-complexity-of-finding-k-successors-in-bst/40564321#40564321 – Shasha99 Nov 13 '16 at 06:03

1 Answers1

3

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • In order to show that the cost traversing height of tree is distributed equally do I need to show each edge is visited some constant number of times at the max. If so then how? – user2179293 Sep 23 '13 at 13:22
  • 1
    @user2179293 You can show that each node is visited at most three times in the process of the complete BST traversal. When the node is a leaf, it's visited only once. When the node has one child, it is visited twice - once on the way down, and once on the way up. When the node has both children, it is visited once on the way down, and twice on the way up - one time per child branch. Since there's a constant limit on the number each node is visited, the overall time is linear. – Sergey Kalinichenko Sep 23 '13 at 14:29
  • But that is linear in terms of number of nodes and not in terms of height – user2179293 Sep 23 '13 at 14:32
  • @user2179293 It's a single call that is linear in terms of height (rightmost leaf of the left subtree to the leftmost leaf of the right subtree). The complete walk is linear in terms of number of nodes, meaning getting a successor `n` times takes `O(n)`. From that it follows that getting a successor once takes amortized `O(1)`. – Sergey Kalinichenko Sep 23 '13 at 14:45
  • I was not talking about O(h) for complete algorithm, my, point was calling successor k times is taking O(k) time whereas it should be O(k+h). I wanted to know when h does dominate over k(I think when the right subtree of a node is empty) and we are just calling for one time and how can we argue that total running time in O(k+h) that is traversing of height is getting distributed equally – user2179293 Sep 23 '13 at 14:56
  • 1
    @user2179293 Please re-read the first sentence of my answer: `O(k+h)` does not mean that the traversal of height is distributed equally. All it means that it *may* be dominated either by `k` or by `h`, yet it is linear in either one of them. You prove each point separately (i.e. "linear in `k`", "linear in `h`"), then write `O(MAX(k,h))`, and finally replace by an equivalent `O(k+h)`, that's all. – Sergey Kalinichenko Sep 23 '13 at 15:19
  • I am just asking in which all cases h will dominate over k. – user2179293 Sep 23 '13 at 15:25
  • @user2179293 Since the initial node in the tree on which we call `Successor(node)` for the first time can be chosen arbitrarily, `h` will dominate over `k` when we start at the predecessor of the tree's root, and make `k`, the number of calls, smaller than `h`. – Sergey Kalinichenko Sep 23 '13 at 15:34
  • 1
    @dasblinkenlight Successor of rightmost node in left subtree of a rooted tree is root itself... right? – cbinder Jun 12 '14 at 04:34
  • I agree with @cbinder, the successor of the rightmost node in the left subtree of the root would be the root itself, so you would only have to traverse the height of the tree once in this scenario. – Andrew Schroeder Nov 21 '21 at 18:39