# def red black tree
class RBTree(object):
def __init__(self):
#init
self.nil = RBTreeNode(0)
#nil=do not have the father node and son node
self.root = self.nil
class RBTreeNode(object):
def __init__(self,x):
self.key =x
self.left = None
self.right = None
self.parent = None
self.color = 'black'
self.size = None
def LeftRotate(T,x):
y = x.right
x.right = y.left
if y.left != T.nil:
y.left.parent = x
y.parent = x.parent
if x.parent == T.nil:
#x is root
T.root = y
#fix the shuzhizhen
elif x == x.parent.left:
x.parent.left = y
else:
x.parent.right = y
y.left = x
x.parent = y
def RightRotate(T,x):
y = x.left
x.left = y.right
if y.right != T.nil:
y.right.parent = x
y.parent = x.parent
if x.parent == T.nil:
T.root = y
elif x == x.parent.left:
x.parent.left = y
y.right = x
x.parent = y
def RBInsert(T, z):
y = T.nil
#use y to record the parent node of present node
x = T.root
#start from the root
while x != T.nil:#find the location to insert
y = x
if z.key < x.key:
x = x.left
else:
x = x.right
z.parent = y
if y == T.nil:
T.root = z
else:
if z.key < y.key:
y.left = z
else:
y.right = z
z.left = T.nil
z.right = T.nil
RBInsertFixup(T,z)
return z.key, 'color is', z.color
#print(z.key, 'color is', z.color)
def RBInsertFixup(T,z):
while z.parent.color == 'red':
# if z is root, do not go into this xunhuan
if z.parent == z.parent.parent.left:
y = z.parent.parent.right
if y.color == 'red':
y.color = 'black'
z.parent.color = 'black'
z.parent.parent.color = 'red'
z = z.parent.parent
else:
if z == z.parent.right:
z = z.parent
LeftRotate(T,z)
z.parent.color = 'black'
z.parent.parent.color = 'red'
RightRotate(T,z.parent.parent)
else:
y = z.parent.parent.left
if y.color == 'red':
z.parent.color = 'black'
y.color = 'black'
z.parent.parent.color = 'red'
z = z.parent.parent
else:
if z == z.parent.left:
z = z.parent
RightRotate(T, z)
z.parent.color = 'black'
z.parent.parent.color = 'red'
LeftRotate(T, z.parent.parent)
T.root.color = 'black'
def RBTransplant(T,u,v):# replace u by v
if u.parent == T.nil:
T.root = v
elif u == u.parent.left:
u.parent.left = v
else:
u.parent.right = v
v.parent = u.parent
def RBdelete(T,z):#wrong
if z.left == T.nil or z.right == T.nil:
y = z
else:
y = TreeMinimum(z)
if y.left != T.nil:
x = y.left
else:
x = y.right
x.parent = y.parent
if y.parent == T.nil:
T.root = x
else:
if y == y.parent.left:
y.parent.left = x
else:
y.parent.right = x
if y != z:
z = y
if y.color == 'black':
RBDeleteFixup(T,x)
return y
def RBDelete(T,z):
y = z
y_original_color = y.color
if z.left == T.nil: #do not have left child
x = z.right
RBTransplant(T,z,z.right)
elif z.right == T.nil:#do not have right child
x = z.left
RBTransplant(T,z,z.left)
else:
#both have
y = TreeMinimum(z.right)
#return z.right.left
y_original_color = y.color
x = y.right
if y.parent == z:
x.parent =y
else:
RBTransplant(T,y,y.right)
y.right = z.right
y.right.parent = y
RBTransplant(T,z,y)
y.left = z.left
y.left.parent = y
y.color = z.color
if y_original_color == 'black':
RBDeleteFixup(T,x)
def RBDeleteFixup(T,x):
while x != T.root and x.color == 'black':
if x == x.parent.left:
w = x.parent.right
if w.color == 'red':
w.color = 'black'
x.parent.color = 'red'
LeftRotate(T,x.parent)
w = x.parent.right
if w.left.color == 'black' and w.right.color == 'black':
w.color = 'red'
x = x.parent
else:
if w.right.color == 'black':
w.left.color = 'black'
w.color = 'red'
RightRotate(T,w)
w = x.parent.right
w.color = x.parent.color
x.parent.color = 'black'
w.right.color = 'black'
LeftRotate(T, x.parent)
x = T.root
else:
w = x.parent.left
if w.color == 'red':
w.color = 'black'
x.parent.color = 'red'
RightRotate(T,x.parent)
w = x.parent.left
if w.left.color == 'black' and w.right.color == 'black':
w.color = 'red'
x = x.parent
else:
if w.left.color == 'black':
w.right.color = 'black'
w.color = 'red'
LeftRotate(T,w)
w = x.parent.left
w.color = x.parent.color
x.parent.color = 'black'
w.left.color = 'black'
RightRotate(T, x.parent)
x = T.root
x.color = 'black'
def TreeMinimum(x):
while x.left != T.nil:
x = x.left
return x
def Midsort(x):
if x!= None:
Midsort(x.left)
if x.key!=0:
print('key:', x.key,'x.parent',x.parent.key)
Midsort(x.right)
nodes = [11,2,14,1,7,15,5,8,4]
T = RBTree()
for node in nodes:
print('insert number',RBInsert(T,RBTreeNode(node)))
print('Midsort')
Midsort(T.root)
#RBdelete(T,T.root)
RBDelete(T,T.root)
print('Midsort')
Midsort(T.root)
#RBdelete(T,T.root)
RBDelete(T,T.root)
print('Midsort')
Midsort(T.root)
算法基础实验3_0:红黑树的基本实现python
最新推荐文章于 2024-03-22 07:44:27 发布