红黑树_插入操作(实例)

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)
posted @   xuchaoxin1375  阅读(7)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示