7

I need a hint for this exercise from the CLRS Algorithms book:

Prove that no matter what node we start at in a height-h binary search tree, k successive calls to Tree-Successor take O(k+h) time.

Luke Miles
  • 941
  • 9
  • 19
ssierral
  • 8,537
  • 6
  • 26
  • 44

2 Answers2

9
  • Let x be the starting node and z be the ending node after k successive calls to TREE-SUCCESSOR.
  • Let p be the simple path between x and z inclusive.
  • Let y be the common ancestor of x and z that p visits.
  • The length of p is at most 2h, which is O(h).
  • Let output be the elements that their values are between x.key and z.key inclusive.
  • The size of output is O(k).
  • In the execution of k successive calls to TREE-SUCCESSOR, the nodes that are in p are visited, and besides the nodes x, y and z, if a sub tree of a node in p is visited then all its elements are in output.
  • So the running time is O(h+k).
Avi Cohen
  • 3,102
  • 2
  • 25
  • 26
  • 2
    `In the execution of k successive calls to TREE-SUCCESSOR, the nodes that are in p are visited, and besides the nodes x, y and z` Can you please explain what is `y` here? – Rafi Kamal Dec 11 '12 at 13:24
  • I added `y` to the answer. – Avi Cohen Dec 11 '12 at 15:41
  • @AviCohen. Thank you very much. So, I though `k` successive calls would mean to call the function of finding successor `k` times meaning that we would have `k` different functions each with its own recursive calls. I think, since finding successor for each would take `O(h)`, where `h` is the height, then we need `O(h)+...+O(h)` `k` times? P – Avv Aug 11 '21 at 20:09
3

Hint: work out a small example, observe the result, try to extrapolate the reason.

To get started, here are some things to consider.

Start at a certain node, k succesive calls to Tree-Succcesor consititutes a partial tree walk. How many (at least and at most) nodes does this walk visit? (Hint: Think about key(x)). Keep in mind that an edge is visited at most twice (why?).

Final hint: The result is O(2h+k).

PengOne
  • 48,188
  • 17
  • 130
  • 149