二叉搜索树

<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
 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值