问题
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.
回答1:
- Let
xbe the starting node andzbe the ending node afterksuccessive calls to TREE-SUCCESSOR. - Let
pbe the simple path betweenxandzinclusive. - Let
ybe the common ancestor ofxandzthatpvisits. - The length of
pis at most2h, which isO(h). - Let
outputbe the elements that their values are betweenx.keyandz.keyinclusive. - The size of
outputisO(k). - In the execution of
ksuccessive calls to TREE-SUCCESSOR, the nodes that are inpare visited, and besides the nodesx,yandz, if a sub tree of a node inpis visited then all its elements are inoutput. - So the running time is
O(h+k).
回答2:
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).
来源:https://stackoverflow.com/questions/8454771/prove-the-efficiency-of-repeated-calls-to-successor-in-binary-trees