<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">1.设x是二叉搜索树中的一个节点。如果y是x左子树中的一个节点,那么y.key <= x.key,如果y是x右子树中的一个节点,那么y.key >= x.key。</span>
查找-递归
TREE-SEARCH(x, k)
if x == NIL || k == x.key
return x
if k < x.key
return TREE-SEARCH(x.left, k)
else
return TREE-SEARCH(x.right, k)
查找-非递归
TREE-SEARCH(x, k)
while x != null && k != x.key
if k < x.key
x = x.left
else
x = x.right
return x
最大关键字
TREE-MAXIMUM(x)
while x.left != NIL
x = x.left
return x
最小关键字
TREE-MINIMUM(x)
while x.right != NIL
x = x.right
return x
后继
TREE-SUCCESSOR(x)
if x.right != NIL
return TREE-MINIMUM(x)
y = x.parent
while y != NIL && x == y.right
x = y
y = y.parent
return y
前驱
<pre name="code" class="cpp">TREE-PREDECESSOR(x)
if x.left != NIL
return TREE-MAXIMUM(x)
y = x.parent
while y != NIL && x == y.left
x = y
y = y.parent
return y
插入
<pre name="code" class="cpp">TREE-INSERT(T, z)
y = NIL
t = T.root
while x != NIL
<span style="white-space:pre"> </span>y = t
<span style="white-space:pre"> </span>if z.key < t.key
<span style="white-space:pre"> </span> x = x.left
<span style="white-space:pre"> </span>else
<span style="white-space:pre"> </span> x = x.right
z.parent = y
if y == NIL
<span style="white-space:pre"> </span>T.root = z
else if z.key < y.key
<span style="white-space:pre"> </span>y.left = z
else
<span style="white-space:pre"> </span>y.right = z
替换:用v替换u
<pre name="code" class="cpp">TRANSPLANT(T, u, v)
if u.parent == NIL
<span style="white-space:pre"> </span>T.root = v
else if u == u.parent.left
<span style="white-space:pre"> </span>u.parent.left = v
else
<span style="white-space:pre"> </span>u.parent.right = v
if v != NIL
<span style="white-space:pre"> </span>v.parent = u.parent
删除
<pre name="code" class="cpp">TREE-DELETE(T, z)
if z.left == NIL
<span style="white-space:pre"> </span>TRANSPLANT(T, z, z.right)
else if z.right == NIL
<span style="white-space:pre"> </span>TRANSPLANT(T, z, z.left)
else
<span style="white-space:pre"> </span>y = TREE-MINIMUM(z.right)
<span style="white-space:pre"> </span>if y.parent != z
<span style="white-space:pre"> </span> TRANSPLANT(T, y, y.right)
<span style="white-space:pre"> </span> y.right = z.right
<span style="white-space:pre"> </span> y.right.parent = y
<span style="white-space:pre"> </span>TRANSPLANT(T, z, y)
<span style="white-space:pre"> </span>y.left = z.left
<span style="white-space:pre"> </span>y.left.parent = y