文章目录


rotate operation:

The selected point to rotate is rotated around the child node on the specified side (essentially altering the pointer)
红黑树_插入操作(实例)_ide

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 :

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

the different insert cases:

the properties of the red-black tree:

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

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:

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

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

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

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!
红黑树_插入操作(实例)_ide_07
红黑树_插入操作(实例)_python_08

the right_rotation pseudocode is symmetric (similarly)

classic exsample from Introduction to algorithm:

红黑树_插入操作(实例)_python_09
红黑树_插入操作(实例)_python_10
红黑树_插入操作(实例)_ide_11
this is case 2
红黑树_插入操作(实例)_ide_12
红黑树_插入操作(实例)_ide_13

红黑树_插入操作(实例)_ide_14
in case3 the color need to change:
红黑树_插入操作(实例)_ide_15

fixup for example:

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

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)