二叉查找树_删除节点
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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」