红黑树节点删除示例代码
时间: 2024-03-14 21:41:47 浏览: 78
红黑树插入以及删除代码
5星 · 资源好评率100%
红黑树节点删除的示例代码如下所示[^2]:
```python
class Node:
def __init__(self, key):
self.key = key
self.left = None
self.right = None
self.parent = None
self.color = 1
class RedBlackTree:
def __init__(self):
self.TNULL = Node(0)
self.TNULL.color = 0
self.TNULL.left = None
self.TNULL.right = None
self.root = self.TNULL
def delete_node(self, key):
self.delete_helper(self.root, key)
def balance(self, x):
while x != self.root and x.color == 0:
if x == x.parent.left:
s = x.parent.right
if s.color == 1:
s.color = 0
x.parent.color = 1
self.left_rotate(x.parent)
s = x.parent.right
if s.left.color == 0 and s.right.color == 0:
s.color = 1
x = x.parent
else:
if s.right.color == 0:
s.left.color = 0
s.color = 1
self.right_rotate(s)
s = x.parent.right
s.color = x.parent.color
x.parent.color = 0
s.right.color = 0
self.left_rotate(x.parent)
x = self.root
else:
s = x.parent.left
if s.color == 1:
s.color = 0
x.parent.color = 1
self.right_rotate(x.parent)
s = x.parent.left
if s.right.color == 0 and s.right.color == 0:
s.color = 1
x = x.parent
else:
if s.left.color == 0:
s.right.color = 0
s.color = 1
self.left_rotate(s)
s = x.parent.left
s.color = x.parent.color
x.parent.color = 0
s.left.color = 0
self.right_rotate(x.parent)
x = self.root
x.color = 0
def delete_node_helper(self, node, key):
z = self.TNULL
while node != self.TNULL:
if node.key == key:
z = node
if node.key <= key:
node = node.right
else:
node = node.left
if z == self.TNULL:
print("Couldn't find key in the tree")
return
y = z
y_original_color = y.color
if z.left == self.TNULL:
x = z.right
self.rb_transplant(z, z.right)
elif (z.right == self.TNULL):
x = z.left
self.rb_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.rb_transplant(y, y.right)
y.right = z.right
y.right.parent = y
self.rb_transplant(z, y)
y.left = z.left
y.left.parent = y
y.color = z.color
if y_original_color == 0:
self.balance(x)
def rb_transplant(self, u, v):
if u.parent == None:
self.root = v
elif u == u.parent.left:
u.parent.left = v
else:
u.parent.right = v
v.parent = u.parent
def minimum(self, node):
while node.left != self.TNULL:
node = node.left
return node
def left_rotate(self, x):
y = x.right
x.right = y.left
if y.left != self.TNULL:
y.left.parent = x
y.parent = x.parent
if x.parent == None:
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.TNULL:
y.right.parent = x
y.parent = x.parent
if x.parent == None:
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, key):
node = Node(key)
node.parent = None
node.key = key
node.left = self.TNULL
node.right = self.TNULL
node.color = 1
y = None
x = self.root
while x != self.TNULL:
y = x
if node.key < x.key:
x = x.left
else:
x = x.right
node.parent = y
if y == None:
self.root = node
elif node.key < y.key:
y.left = node
else:
y.right = node
if node.parent == None:
node.color = 0
return
if node.parent.parent == None:
return
self.balance(node)
def inorder_helper(self, node):
if node != self.TNULL:
self.inorder_helper(node.left)
print(node.key, end=" ")
self.inorder_helper(node.right)
def preorder_helper(self, node):
if node != self.TNULL:
print(node.key, end=" ")
self.preorder_helper(node.left)
self.preorder_helper(node.right)
def postorder_helper(self, node):
if node != self.TNULL:
self.postorder_helper(node.left)
self.postorder_helper(node.right)
print(node.key, end=" ")
def search_tree_helper(self, node, key):
if node == self.TNULL or key == node.key:
return node
if key < node.key:
return self.search_tree_helper(node.left, key)
return self.search_tree_helper(node.right, key)
def delete(self, key):
self.delete_node_helper(self.root, key)
def searchTree(self, k):
return self.search_tree_helper(self.root, k)
def inorder(self):
self.inorder_helper(self.root)
def preorder(self):
self.preorder_helper(self.root)
def postorder(self):
self.postorder_helper(self.root)
def searchTree(self, k):
return self.search_tree_helper(self.root, k)
# 示例代码
bst = RedBlackTree()
bst.insert(55)
bst.insert(40)
bst.insert(65)
bst.insert(60)
bst.insert(75)
bst.insert(57)
print("Inorder traversal of the given tree:")
bst.inorder()
print("\nDelete 40")
bst.delete(40)
print("Inorder traversal of the modified tree:")
bst.inorder()
print("\nDelete 65")
bst.delete(65)
print("Inorder traversal of the modified tree:")
bst.inorder()
```
请注意,这只是一个示例代码,用于演示红黑树节点删除的基本操作。具体的实现可能会根据不同的编程语言和应用场景而有所不同。
阅读全文