-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRedBlackInsertion
More file actions
133 lines (114 loc) · 3.62 KB
/
RedBlackInsertion
File metadata and controls
133 lines (114 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
class Node:
def __init__(self, key, color, parent=None, left=None, right=None):
self.key = key
self.color = color # 'R' for red, 'B' for black
self.parent = parent
self.left = left
self.right = right
class RedBlackTree:
def __init__(self):
self.NIL_LEAF = Node(None, 'B')
self.root = self.NIL_LEAF
def left_rotate(self, x):
y = x.right
x.right = y.left
if y.left != self.NIL_LEAF:
y.left.parent = x
y.parent = x.parent
if x.parent is 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, y):
x = y.left
y.left = x.right
if x.right != self.NIL_LEAF:
x.right.parent = y
x.parent = y.parent
if y.parent is None:
self.root = x
elif y == y.parent.left:
y.parent.left = x
else:
y.parent.right = x
x.right = y
y.parent = x
def insert(self, key):
node = Node(key, 'R')
node.parent = None
node.key = key
node.left = self.NIL_LEAF
node.right = self.NIL_LEAF
node.color = 'R'
parent = None
current = self.root
while current != self.NIL_LEAF:
parent = current
if node.key < current.key:
current = current.left
else:
current = current.right
node.parent = parent
if parent is None:
self.root = node
elif node.key < parent.key:
parent.left = node
else:
parent.right = node
if node.parent is None:
node.color = 'B'
return
if node.parent.parent is None:
return
self._insert_fixup(node)
def _insert_fixup(self, k):
while k.parent.color == 'R':
if k.parent == k.parent.parent.right:
u = k.parent.parent.left
if u.color == 'R':
u.color = 'B'
k.parent.color = 'B'
k.parent.parent.color = 'R'
k = k.parent.parent
else:
if k == k.parent.left:
k = k.parent
self.right_rotate(k)
k.parent.color = 'B'
k.parent.parent.color = 'R'
self.left_rotate(k.parent.parent)
else:
u = k.parent.parent.right
if u.color == 'R':
u.color = 'B'
k.parent.color = 'B'
k.parent.parent.color = 'R'
k = k.parent.parent
else:
if k == k.parent.right:
k = k.parent
self.left_rotate(k)
k.parent.color = 'B'
k.parent.parent.color = 'R'
self.right_rotate(k.parent.parent)
if k == self.root:
break
self.root.color = 'B'
# Helper function to print the tree (inorder traversal)
def inorder_traversal(node):
if node != None:
inorder_traversal(node.left)
print(node.key, node.color)
inorder_traversal(node.right)
# Example usage
if __name__ == "__main__":
tree = RedBlackTree()
keys = [10, 20, 30, 40, 50, 60, 70, 80, 90]
for key in keys:
tree.insert(key)
print("Inorder traversal of the Red-Black Tree:")
inorder_traversal(tree.root)