红黑树删除python
时间: 2023-11-13 09:54:52 浏览: 139
红黑树demo python代码
红黑树的删除操作相对于插入操作要复杂一些,需要考虑多种情况。下面是红黑树删除的Python实现代码:
```python
class RBNode:
def __init__(self, val, color='red'):
self.val = val
self.color = color
self.left = None
self.right = None
self.parent = None
class RBTree:
def __init__(self):
self.nil = RBNode(None, 'black')
self.root = self.nil
def left_rotate(self, x):
y = x.right
x.right = y.left
if y.left != self.nil:
y.left.parent = x
y.parent = x.parent
if x.parent == self.nil:
self.root = y
elif x == x.parent.left:
x.parent.left = y
else:
x.parent.right = y
y.left = x
x.parent = y
def right_rotate(self, x):
y = x.left
x.left = y.right
if y.right != self.nil:
y.right.parent = x
y.parent = x.parent
if x.parent == self.nil:
self.root = y
elif x == x.parent.right:
x.parent.right = y
else:
x.parent.left = y
y.right = x
x.parent = y
def insert(self, val):
z = RBNode(val)
y = self.nil
x = self.root
while x != self.nil:
y = x
if z.val < x.val:
x = x.left
else:
x = x.right
z.parent = y
if y == self.nil:
self.root = z
elif z.val < y.val:
y.left = z
else:
y.right = z
z.left = self.nil
z.right = self.nil
z.color = 'red'
self.insert_fixup(z)
def insert_fixup(self, z):
while z.parent.color == 'red':
if z.parent == z.parent.parent.left:
y = z.parent.parent.right
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.right:
z = z.parent
self.left_rotate(z)
z.parent.color = 'black'
z.parent.parent.color = 'red'
self.right_rotate(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
self.right_rotate(z)
z.parent.color = 'black'
z.parent.parent.color = 'red'
self.left_rotate(z.parent.parent)
self.root.color = 'black'
def transplant(self, u, v):
if u.parent == self.nil:
self.root = v
elif u == u.parent.left:
u.parent.left = v
else:
u.parent.right = v
v.parent = u.parent
def minimum(self, x):
while x.left != self.nil:
x = x.left
return x
def delete(self, val):
z = self.search(val)
if z == self.nil:
return
y = z
y_original_color = y.color
if z.left == self.nil:
x = z.right
self.transplant(z, z.right)
elif z.right == self.nil:
x = z.left
self.transplant(z, z.left)
else:
y = self.minimum(z.right)
y_original_color = y.color
x = y.right
if y.parent == z:
x.parent = y
else:
self.transplant(y, y.right)
y.right = z.right
y.right.parent = y
self.transplant(z, y)
y.left = z.left
y.left.parent = y
y.color = z.color
if y_original_color == 'black':
self.delete_fixup(x)
def delete_fixup(self, x):
while x != self.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'
self.left_rotate(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'
self.right_rotate(w)
w = x.parent.right
w.color = x.parent.color
x.parent.color = 'black'
w.right.color = 'black'
self.left_rotate(x.parent)
x = self.root
else:
w = x.parent.left
if w.color == 'red':
w.color = 'black'
x.parent.color = 'red'
self.right_rotate(x.parent)
w = x.parent.left
if w.right.color == 'black' and w.left.color == 'black':
w.color = 'red'
x = x.parent
else:
if w.left.color == 'black':
w.right.color = 'black'
w.color = 'red'
self.left_rotate(w)
w = x.parent.left
w.color = x.parent.color
x.parent.color = 'black'
w.left.color = 'black'
self.right_rotate(x.parent)
x = self.root
x.color = 'black'
def search(self, val):
x = self.root
while x != self.nil and val != x.val:
if val < x.val:
x = x.left
else:
x = x.right
return x
# 测试代码
tree = RBTree()
tree.insert(10)
tree.insert(20)
tree.insert(30)
tree.insert(100)
tree.insert(90)
tree.insert(40)
tree.insert(50)
tree.delete(20)
```
阅读全文