Prove the efficiency of repeated calls to successor() in binary trees?

后端 未结 2 1116
粉色の甜心
粉色の甜心 2021-02-06 09:34

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 suc

2条回答
  •  清歌不尽
    2021-02-06 10:01

    • 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).

提交回复
热议问题