二叉查找树_删除节点

delete procedure: sketch map

在这里插入图片描述

interpretation the related algorithm with python psedudo codes:

the codes may help you to understand the pseudo-codes :in the book Introduction to Algorithm:
this algorith refer to searching funciton in the binary searching tree:

NIL = 0  # empty
y = 0  # initial
""" int tree T:
there,'node.p' represents the parent node(pointer) of the node 
we don't have to care whether the v(tree pointer) is a subtree of the T tree or not;and we no need to think about if the v is NIL(we allow v is NIL (empty)) 
to make:
v.p=u.p (if v,u!=NIP) (for v) 
u.p.left or u.p.right=v """
def transplant(T, u, v):
    """ judge aspect1: """
    """ the u don't have a parent node,the make the v to be the root node of the tree T directly: """
    if u.p==NIL:
        T.root=v
        """ else, the u node has its parent node,the update its parent node to update its left/right child pointer to be the v node (depend on the u node is its parent node's right or left child)  """
    elif u==u.p.left:
        u.p.left=v
    else:
        u.p.right=v
    """ judge another aspect: """
    """ if the v node is not NIL,the no matter its original parent is who,make v (or say,the tree root at v node) to recognize its new parent:u.p;
    after that,the parent node (u.p) could not find the u node any more(although the u node may could still know who is (ever was) its parent node )"""
    if v!=NIL:
        v.p=u.p

""" find the minimum node from node specified scan down """
def tree_minimum(node):
    while node.left!=NIL:
        node=node.left
    return node

''' delete the node z in the tree in any case:'''
def TREE_DELETE(T, z):
    NIL = 0  # empty
    y = 0  # initial
    if z.left == NIL:
        transplant(T, z, z.right)
    elif z.right == NIL:
        transplant(T, z, z.left)
    else:
        """ the subcase1:the node z has both right and left nodes: """
        y = tree_minimum(z.right)
        if y.p != z:
            """ this subcase is a little tricky:
            we can turn the case to another subcase (subcase2) which is easier to be conquer:
            we take care of the z.right subtree:
            we should transplant the y(the successor of the z node) by y.right(may be NIL);but,the y is still y node,just the y.p couldn't find the y node any more; """
            transplant(T, y, y.right)
            """ make the y adapt the z.right to be its new right child (so that the z node's right child wouldn't lose """
            y.right = z.right
            """ correspondingly,make the child recognize y as its new parent: """
            y.right.p = y
        """ the subcase2: that the y node is appropriate the right child of the node z  """
        transplant(T, z, y)
        """ z.left will lose its parent node z,due to the z node is to be delete,so we must arrange z.left in time properly:that the y.left to adapt the z.left: """
        y.left = z.left
        """ meanwhile,that the new child of y to recognize its new parent y(we use y.left.p to represent the old child z.left ) """
        y.left.p = y

posted @ 2021-04-03 14:32  xuchaoxin1375  阅读(9)  评论(0)    收藏  举报  来源