-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathred_black_tree.py
369 lines (304 loc) · 8.68 KB
/
red_black_tree.py
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
366
367
368
369
# source: https://a.co/d/7oRsCT2
from collections import deque
BLACK = True
RED = False
class Node:
def __init__(self, key):
self.key = key
self.p = None # parent
self.color = RED
self.left = None
self.right = None
def print_color(self):
if self.color == BLACK:
return '(b)'
return '(r)'
class RedBlackTree:
def __init__(self):
self.NIL = Node(99999) # arbitrary value
self.NIL.color = BLACK
self.NIL.left = None
self.NIL.right = None
self.root = self.NIL
# O(1)
def left_rotate(self, x):
y = x.right
x.right = y.left
if y.left != self.NIL:
y.left.p = x
y.p = x.p
if x.p is None:
self.root = y
elif x == x.p.left:
x.p.left = y
else:
x.p.right = y
y.left = x
x.p = y
# O(1)
def right_rotate(self, x):
y = x.left
x.left = y.right
if y.right != self.NIL:
y.right.p = x
y.p = x.p
if x.p is None:
self.root = y
elif x == x.p.right:
x.p.right = y
else:
x.p.left = y
y.right = x
x.p = y
# O(logn) total
def insert(self, key):
z = Node(key)
z.left = self.NIL
z.right = self.NIL
y = None
x = self.root
while x != self.NIL:
y = x
if z.key < x.key:
x = x.left
else:
x = x.right
z.p = y
if y == None:
self.root = z
elif z.key < y.key:
y.left = z
else:
y.right = z
self.insert_fixup(z)
# O(logn)
def insert_fixup(self, z):
while z.p and z.p.color == RED:
if z.p == z.p.p.left:
y = z.p.p.right
if y.color == RED:
z.p.color = BLACK
y.color = BLACK
z.p.p.color = RED
z = z.p.p
else:
if z == z.p.right:
z = z.p
self.left_rotate(z)
z.p.color = BLACK
z.p.p.color = RED
self.right_rotate(z.p.p)
else:
y = z.p.p.left
if y.color == RED:
z.p.color = BLACK
y.color = BLACK
z.p.p.color = RED
z = z.p.p
else:
if z == z.p.left:
z = z.p
self.right_rotate(z)
z.p.color = BLACK
z.p.p.color = RED
self.left_rotate(z.p.p)
if z == self.root:
break
self.root.color = BLACK
# O(logn) total
def delete(self, k):
z = self.search(k)
if z == self.NIL:
return "Key not found!"
y = z
y_orig_color = y.color
# case 1
if z.left == self.NIL:
x = z.right
self.transplant(z, z.right)
# case 2
elif z.right == self.NIL:
x = z.left
self.transplant(z, z.left)
# case 3
else:
y = self.minimum(z.right)
y_orig_color = y.color
x = y.right
if y.p == z:
x.p = y
else:
self.transplant(y, y.right)
y.right = z.right
y.right.p = y
self.transplant(z, y)
y.left = z.left
y.left.p = y
y.color = z.color
if y_orig_color == BLACK:
self.delete_fixup(x)
# O(logn)
def delete_fixup(self, x):
while x != self.root and x.color == BLACK:
if x == x.p.left:
w = x.p.right
# type 1
if w.color == RED:
w.color = BLACK
x.p.color = RED
self.left_rotate(x.p)
w = x.p.right
# type 2
if w.left.color == BLACK and w.right.color == BLACK:
w.color = RED
x = x.p
else:
# type 3
if w.right.color == BLACK:
w.left.color = BLACK
w.color = RED
self.right_rotate(w)
w = x.p.right
# type 4
w.color = x.p.color
x.p.color = BLACK
w.right.color = BLACK
self.left_rotate(x.p)
x = self.root
else:
w = x.p.left
# type 1
if w.color == RED:
w.color = BLACK
x.p.color = RED
self.right_rotate(x.p)
w = x.p.left
# type 2
if w.right.color == BLACK and w.left.color == BLACK:
w.color = RED
x = x.p
else:
# type 3
if w.left.color == BLACK:
w.right.color = BLACK
w.color = RED
self.left_rotate(w)
w = x.p.left
# type 4
w.color = x.p.color
x.p.color = BLACK
w.left.color = BLACK
self.right_rotate(x.p)
x = self.root
x.color = BLACK
# O(1)
def transplant(self, u, v):
if u.p == None:
self.root = v
elif u == u.p.left:
u.p.left = v
else:
u.p.right = v
v.p = u.p
# O(h) = O(logn) for RB trees
def minimum(self, x):
while x.left != self.NIL:
x = x.left
return x
# O(h) = O(logn) for RB trees
def search(self, k):
x = self.root
while x != self.NIL and k != x.key:
if k < x.key:
x = x.left
else:
x = x.right
return x
# simple level-order tree traversal
def print_tree(self, print_color=False):
queue = deque()
queue.append(self.root)
while(queue):
node = queue.popleft()
if print_color:
print(f'{node.key}{node.print_color()}', end=' ')
else:
print(node.key, end=' ')
# don't add NIL nodes
if node.left != self.NIL:
queue.append(node.left)
if node.right != self.NIL:
queue.append(node.right)
def make_larger_tree():
RB = RedBlackTree()
RB.insert(8)
RB.insert(5)
RB.insert(15)
RB.insert(12)
RB.insert(19)
RB.insert(9)
RB.insert(13)
RB.insert(23)
return RB
# ignoring color, just demonstrating rotation
def rotations_video():
print('\n-- ROTATIONS VIDEO --')
RB = RedBlackTree()
RB.insert(5)
RB.insert(2)
RB.insert(10)
RB.insert(8)
RB.insert(12)
RB.insert(6)
RB.insert(9)
RB.print_tree()
print('\n\n-- After left rotation --')
five = RB.search(5)
RB.left_rotate(five)
RB.print_tree()
print('\n\n-- After right rotation --')
ten = RB.search(10)
RB.right_rotate(ten)
RB.print_tree()
def insertions_video():
RB = RedBlackTree()
print('\n\n-- INSERTIONS VIDEO, after case 0 --')
RB.insert(15)
RB.print_tree(True)
print('\n\n-- Insert 5 --')
RB.insert(5)
RB.print_tree(True)
print('\n\n-- Insert 1 (case 3) --')
RB.insert(1)
RB.print_tree(True)
print('\n\n-- Move to larger tree --')
RB = make_larger_tree()
RB.print_tree(True)
print('\n\n-- Insert 10 (case 1, 2, and 3) --')
RB.insert(10)
RB.print_tree(True)
def deletions_video():
print('\n\n-- DELETIONS VIDEO --')
RB = make_larger_tree()
RB.insert(10)
RB.print_tree(True)
print('\n\n-- Delete 19 (case 1) --')
RB.delete(19)
RB.print_tree(True)
print('\n\n-- Insert 1 --')
RB.insert(1)
RB.print_tree(True)
print('\n\n-- Delete 5 (case 2) --')
RB.delete(5)
RB.print_tree(True)
print('\n\n-- Delete 12 (case 3) --')
RB.delete(12)
RB.print_tree(True)
def main():
# https://youtu.be/95s3ndZRGbk
rotations_video()
# https://youtu.be/A3JZinzkMpk
insertions_video()
# https://youtu.be/lU99loSvD8s | https://youtu.be/iw8N1_keEWA
deletions_video()
main()