我怎么能证明这工作在二叉搜索树?
问题描述:
我要你给我一个提示,以证明这项工作从Cormen的书: 证明,无论什么节点,我们开始在一个高度小时二叉搜索树,K 连续调用树后继需要O(K + H)的时间。
I'd want you to give me a hint to prove this exercise from the book of Cormen: "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."
答
- 让
X
是起始节点和以Z
是后结束节点K
连续调用树的继任者。 - 让
P
介于的简单路径X
和以Z
的包容性。 - 让
是
是的共同祖先X
和以Z
的P
探访。 -
P
的长度最多为2小时
,这是0(H)
。 - 让
输出
是元素,它们的值之间的x.key
和ž .KEY
的包容性。 -
输出
的大小是O(K)
。 - 在
K
连续调用树的继任者执行, 这是P
的节点访问, 另外该节点X
,是
和以Z
, 如果一个节点的P
被访问它所有的元素都在输出
。 的子树
- 所以运行时间
0(H + K)
。
- 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)
.