红黑树

红黑树

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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import sys


# data structure that represents a node in the tree
class Node:
def __init__(self, data):
self.data = data # holds the key
self.parent = None # pointer to the parent
self.left = None # pointer to left child
self.right = None # pointer to right child
self.color = 1 # 1 . Red, 0 . Black


# class RedBlackTree implements the operations in Red Black Tree
class RedBlackTree:
def __init__(self):
self.null_node = Node(0)
self.null_node.color = 0
self.null_node.left = None
self.null_node.right = None
self.root = self.null_node

def __pre_order_helper(self, node):
if node != self.null_node:
sys.stdout.write(node.data + " ")
self.__pre_order_helper(node.left)
self.__pre_order_helper(node.right)

def __in_order_helper(self, node):
if node != self.null_node:
self.__in_order_helper(node.left)
sys.stdout.write(node.data + " ")
self.__in_order_helper(node.right)

def __post_order_helper(self, node):
if node != self.null_node:
self.__post_order_helper(node.left)
self.__post_order_helper(node.right)
sys.stdout.write(node.data + " ")

def __search_tree_helper(self, node, key):
if node == self.null_node or key == node.data:
return node

if key < node.data:
return self.__search_tree_helper(node.left, key)
return self.__search_tree_helper(node.right, key)

# fix the rb tree modified by the delete operation
def __fix_delete(self, x):
while x != self.root and x.color == 0:
if x == x.parent.left:
s = x.parent.right
if s.color == 1:
# case 3.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:
# case 3.2
s.color = 1
x = x.parent
else:
if s.right.color == 0:
# case 3.3
s.left.color = 0
s.color = 1
self.right_rotate(s)
s = x.parent.right

# case 3.4
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:
# case 3.1
s.color = 0
x.parent.color = 1
self.right_rotate(x.parent)
s = x.parent.left

if s.left.color == 0 and s.right.color == 0:
# case 3.2
s.color = 1
x = x.parent
else:
if s.left.color == 0:
# case 3.3
s.right.color = 0
s.color = 1
self.left_rotate(s)
s = x.parent.left

# case 3.4
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 __rb_transplant(self, u, v):
if u.parent is None:
self.root = v
elif u == u.parent.left:
u.parent.left = v
else:
u.parent.right = v
v.parent = u.parent

def __delete_node_helper(self, node, key):
# find the node containing key
z = self.null_node
while node != self.null_node:
if node.data == key:
z = node

if node.data <= key:
node = node.right
else:
node = node.left

if z == self.null_node:
print("Couldn't find key in the tree")
return

y = z
y_original_color = y.color
if z.left == self.null_node:
x = z.right
self.__rb_transplant(z, z.right)
elif z.right == self.null_node:
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.__fix_delete(x)

# fix the red-black tree
def __fix_insert(self, k):
while k.parent.color == 1:
if k.parent == k.parent.parent.right:
u = k.parent.parent.left # uncle
if u.color == 1:
# case 3.1
u.color = 0
k.parent.color = 0
k.parent.parent.color = 1
k = k.parent.parent
else:
if k == k.parent.left:
# case 3.2.2
k = k.parent
self.right_rotate(k)
# case 3.2.1
k.parent.color = 0
k.parent.parent.color = 1
self.left_rotate(k.parent.parent)
else:
u = k.parent.parent.right # uncle

if u.color == 1:
# mirror case 3.1
u.color = 0
k.parent.color = 0
k.parent.parent.color = 1
k = k.parent.parent
else:
if k == k.parent.right:
# mirror case 3.2.2
k = k.parent
self.left_rotate(k)
# mirror case 3.2.1
k.parent.color = 0
k.parent.parent.color = 1
self.right_rotate(k.parent.parent)
if k == self.root:
break
self.root.color = 0

def __print_helper(self, node, indent, last):
# print the tree structure on the screen
if node != self.null_node:
sys.stdout.write(indent)
if last:
sys.stdout.write("R----")
indent += " "
else:
sys.stdout.write("L----")
indent += "| "

s_color = "RED" if node.color == 1 else "BLACK"
print(str(node.data) + "(" + s_color + ")")
self.__print_helper(node.left, indent, False)
self.__print_helper(node.right, indent, True)

# Pre-Order traversal
# Node.Left Subtree.Right Subtree
def preorder(self):
self.__pre_order_helper(self.root)

# In-Order traversal
# left Subtree . Node . Right Subtree
def inorder(self):
self.__in_order_helper(self.root)

# Post-Order traversal
# Left Subtree . Right Subtree . Node
def postorder(self):
self.__post_order_helper(self.root)

# search the tree for the key k
# and return the corresponding node
def searchTree(self, k):
return self.__search_tree_helper(self.root, k)

# find the node with the minimum key
def minimum(self, node):
while node.left != self.null_node:
node = node.left
return node

# find the node with the maximum key
def maximum(self, node):
while node.right != self.null_node:
node = node.right
return node

# find the successor of a given node
def successor(self, x):
# if the right subtree is not None,
# the successor is the leftmost node in the
# right subtree
if x.right != self.null_node:
return self.minimum(x.right)

# else it is the lowest ancestor of x whose
# left child is also an ancestor of x.
y = x.parent
while y != self.null_node and x == y.right:
x = y
y = y.parent
return y

# find the predecessor of a given node
def predecessor(self, x):
# if the left subtree is not None,
# the predecessor is the rightmost node in the
# left subtree
if x.left != self.null_node:
return self.maximum(x.left)

y = x.parent
while y != self.null_node and x == y.left:
x = y
y = y.parent

return y

# rotate left at node x
def left_rotate(self, x):
y = x.right
x.right = y.left
if y.left != self.null_node:
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

# rotate right at node x
def right_rotate(self, x):
y = x.left
x.left = y.right
if y.right != self.null_node:
y.right.parent = x

y.parent = x.parent
if x.parent is None:
self.root = y
elif x == x.parent.right:
x.parent.right = y
else:
x.parent.left = y
y.right = x
x.parent = y

# insert the key to the tree in its appropriate position
# and fix the tree
def insert(self, key):
# Ordinary Binary Search Insertion
node = Node(key)
node.parent = None
node.data = key
node.left = self.null_node
node.right = self.null_node
node.color = 1 # new node must be red

y = None
x = self.root

while x != self.null_node:
y = x
if node.data < x.data:
x = x.left
else:
x = x.right

# y is parent of x
node.parent = y
if y is None:
self.root = node
elif node.data < y.data:
y.left = node
else:
y.right = node

# if new node is a root node, simply return
if node.parent is None:
node.color = 0
return

# if the grandparent is None, simply return
if node.parent.parent is None:
return

# Fix the tree
self.__fix_insert(node)

def get_root(self):
return self.root

# delete the node from the tree
def delete_node(self, data):
self.__delete_node_helper(self.root, data)

# print the tree structure on the screen
def pretty_print(self):
self.__print_helper(self.root, "", True)

参考资料

Red Black Trees


红黑树
https://wangqian0306.github.io/2021/red_black_tree/
作者
WangQian
发布于
2021年11月22日
许可协议