红黑树_插入操作(实例)
文章目录
- rotate operation:
- the different insert cases:
- if not so lucky
- case1:the uncle node of the node to be insert to the tree is red:
- case2:the uncle node of the node(z) to be insert is black and the node z is the right child of its parent node
- case3:the uncle node of the node(z) to be insert is black and the node z is the left child of its parent node:
- the right_rotation pseudocode is symmetric (similarly)
- classic exsample from Introduction to algorithm:
- fixup for example:
- pseudocode red_black_tree_fixup() in python:
- pseudocode red_black_tree_insert() in python:
rotate operation:
The selected point to rotate is rotated around the child node on the specified side (essentially altering the pointer)
pseudocode the left_rotate() in python:
def left_rotate(T,x): """[summary] Args: T (tree): [description] x (node): [description] """ """ this is a bidirectional process:parent recognize its child,and its child recognize its parent! """ """ there the y is a pointer of a node (it could be initialized to null) """ y=x.right #make the y pointer hook the node which is x.right node x.right=y.left if y.left != T.nil: """ y.left as a child to recognize its new parent node x """ y.left.p=x """ make y as a child to recognize its new parent:x.p (link x's parent to y)""" y.p=x.p """ then we should set the node y as the correct side(left/right) child of its new parent(x.p);or,make y as the T.root """ """ if x is the T.root(be equivalent to x.p==T.nil) """ if x.p==T.nil: """ make the y as T.root node """ T.root=y # """ elif the x is the left child of its parent: """ elif x==x.p.left: """then set the y as the left child of its new parent(x.p)""" x.p.left=y else: """ then set the y as the right child of its new parent(x.p)""" x.p.right=y y.left=x x.p=y
for example :
the different insert cases:
the properties of the red-black tree:
if not so lucky
the node(z) to be insert is red (default to paint as red,this is beacuse,if you paint the node(z) as black the black height must will be heigh than others,so this is not a good idea)
the parent node of the node(z) to be insert in the red-black tree is red ,the the cases will be divided as following:
case1:the uncle node of the node to be insert to the tree is red:
case2:the uncle node of the node(z) to be insert is black and the node z is the right child of its parent node
case3:the uncle node of the node(z) to be insert is black and the node z is the left child of its parent node:
the case 2 is similar with case3 and the case2 of will be turn to case3 ,you should care about the case3 is how to do transformation!
the right_rotation pseudocode is symmetric (similarly)
…
classic exsample from Introduction to algorithm:
this is case 2
in case3 the color need to change:
fixup for example:
pseudocode red_black_tree_fixup() in python:
from rotation import * RED="red" BLACK="black" def red_black_tree_fixup(T, z): """[fixup the rb tree] Args: T (tree): [red_black_tree] z (node): the node to be insert to the RB tree """ """ when the z.p.color=red,then over the fixup process """ while z.p.color==RED: """ if the z.p is the left child of its parent:""" if z.p==z.p.p.left: """ make the y pointer link to the z.p.p.right as the uncle of the z""" y=z.p.p.right #z's uncle node # todo comprare: """ according the uncle node's(y) color to classify three cases: """ if y.color==RED: """ change the color of z's parent and uncle's color:""" z.p.color=BLACK y.color=BLACK """ meanwhile change the color of z's grandparent:(in this case,the grandparent of z must be black,because the RB tree's properties""" z.p.p.color=RED """ iterate the be inserted node :make the z's grandparent as the new violate node(if not lucky),well,then we repeat the similar process by the while loop""" z=z.p.p # """ z's uncle(y) is black: """ else: # """ case 2: # make case2 to case3: """ if z==z.p.right: """ the z is the right child of its parent: that pointer z to point old z's parent this is to make rotation operation:""" z=z.p left_rotate(T,z) # """ case3: """ elif z==z.p.left: pass """ the case3,it could be handle directly by the following statements out of the judge code segment:""" """ change the color of the z.p and z.p.p """ z.p.color=BLACK z.p.p.color=RED right_rotate(T,z.p.p) else: """ z.p==z.p.p.right """ """ the same then clause with 'right' and 'left' exchanged""" pass
pseudocode red_black_tree_insert() in python:
from fixup import red_black_tree_fixup RED="red" def red_black_insert(T,z): """red_black_insert operation. we know that the new node to be inserted must will be inserted to the leaf node of bst(bst-like tree) Args: T (tree): red_black_tree z (node): node to be inserted """ leaf=T.nil #node leaf is initialized to T.nil x=T.root # the x node is to auxiliary to find the leaf node while x!=T.nil: """ the leaf is the parent of the node x,if x get NIL,then the leaf is the leaf node """ leaf=x """ iterate x go down the tree """ if z.key<x.key: x=x.left else: x=x.right """ make the leaf node leaf as the inserted node z's parent """ z.p=leaf """ if the origin tree is empty:leaf==T.nil """ if leaf==T.nil: """ make the node z as the T.root """ T.root=z # """ put the node z to the proper side of the leaf node leaf to maintain the bst property: """ elif z.key<leaf.key: leaf.left=z else : leaf.right=z """ reset the new node's child(both=T.nil);and set the color of the node z """ z.left=T.nil z.right=T.nil z.color=RED """ to fixup the inserted but maybe not meet the propriety of red_black tree:(outsource the fixup operation) """ red_black_tree_fixup(T,z)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了