问题
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
x
be the starting node andz
be the ending node afterk
successive calls to TREE-SUCCESSOR. - Let
p
be the simple path betweenx
andz
inclusive. - Let
y
be the common ancestor ofx
andz
thatp
visits. - The length of
p
is at most2h
, which isO(h)
. - Let
output
be the elements that their values are betweenx.key
andz.key
inclusive. - The size of
output
isO(k)
. - In the execution of
k
successive calls to TREE-SUCCESSOR, the nodes that are inp
are visited, and besides the nodesx
,y
andz
, if a sub tree of a node inp
is 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