-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathred_black_tree.py
340 lines (288 loc) · 11.5 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
from node import Node
from collections import deque
'''
Code snippets from Shane Downs Gator AVL project 1 submission
red black tree insertion rebalance algorithm advice from Programiz
'''
class RedBlackTree:
def __init__(self):
self.root = None
self.node_count = 0
def left_rotate(self, n):
child = n.right
n.right = child.left
if child.left is not None:
child.left.parent = n
child.parent = n.parent
if n.parent is None:
self.root = child
elif n.parent.left == n:
n.parent.left = child
else:
n.parent.right = child
child.left = n
n.parent = child
def right_rotate(self, n):
child = n.left
n.left = child.right
if child.right is not None:
child.right.parent = n
child.parent = n.parent
if n.parent is None:
self.root = child
elif n.parent.right == n:
n.parent.right = child
else:
n.parent.left = child
child.right = n
n.parent = child
def insert_helper(self, new_node):
parent = new_node.parent
grandparent = parent.parent
while parent.color == "R":
if parent == grandparent.right:
uncle = grandparent.left
if uncle is not None and uncle.color == "R":
parent.color, uncle.color = "B", "B"
grandparent.color = "R"
new_node = grandparent
else:
if new_node == parent.left:
new_node = parent
self.right_rotate(new_node)
parent.color = "B"
grandparent.color = "R"
self.left_rotate(grandparent)
else:
uncle = grandparent.right
if uncle is not None and uncle.color == "R":
parent.color, uncle.color = "B", "B"
grandparent.color = "R"
new_node = grandparent
else:
if new_node == parent.right:
new_node = parent
self.left_rotate(new_node)
parent.color = "B"
grandparent.color = "R"
self.right_rotate(grandparent)
if new_node == self.root:
break
self.root.color = "B"
def insert_node(self, root, keyword, article):
new_node = Node(keyword, article)
new_node.articles.append(article)
self.node_count += 1
if root is None:
new_node.color = "B"
self.root = new_node # First node in the tree, create root
return root
current = root
parent = None
while current is not None:
parent = current
if keyword == current.keyword:
current.articles.append(article)
return
if keyword < current.keyword: # Traverse left subtree
current = current.left
else:
current = current.right # Traverse right subtree
if keyword < parent.keyword:
parent.left = new_node
new_node.parent = parent # Assign parent nodes
else:
parent.right = new_node
new_node.parent = parent
self.insert_helper(new_node) # Branch to insertion helper to maintain red black properties
def print_bfs(self, root):
node_queue = deque() # Queue for traversing root node then adj nodes down to leaves
height_queue = deque() # Height queue to print nodes on same lvl
node_queue.append(root)
height = 1
height_queue.append(height)
current_lvl = 1 # Maintain lvl to print nodes on same line
while bool(node_queue): # while not empty
height = height_queue.popleft() # Get current node and height
current = node_queue.popleft()
if height > current_lvl: # Update current lvl
print("\n")
current_lvl = height
print(current.articles, end=" ")
if current.left is not None: # Traverse left subtree
node_queue.append(current.left)
height_queue.append(height + 1)
if current.right is not None: # Traverse right subtree
node_queue.append(current.right)
height_queue.append(height + 1)
def search_red_black(self, root, keyword):
current = root
while current is not None: # While curr is not a leaf
if current.keyword == keyword: # Found the matching node
return current
if keyword < current.keyword: # Traverse left subtree
current = current.left
else:
current = current.right # Traverse right subtree
return -1
def inorder_traverse(self, root):
if root is not None: # LNR
yield from self.inorder_traverse(root.left) # Yield keyword used for iter over large datasets
yield root.keyword, root.articles
yield from self.inorder_traverse(root.right)
def delete_node(self, root):
current = root
keyword = root.keyword
while current is not None:
if current.keyword == keyword: # Found the matching node
self.delete_helper(current)
if keyword < current.keyword: # Traverse left subtree
current = current.left
else:
current = current.right
def delete_helper(self, node): # Not used in final build. commented out code below was my first attempt
if node is None:
return
original_color = node.color
if node.left is None:
x = node.right
self.swap_nodes(node, x)
elif node.right is None:
x = node.left
self.swap_nodes(node, x)
else:
if node.right.left is not None: # Find inorder successor
inorder_successor = node.right.left
else:
inorder_successor = node.right
original_color = inorder_successor.color
x = inorder_successor.right
if node.right == inorder_successor:
x.parent = inorder_successor
else:
self.swap_nodes(inorder_successor, inorder_successor.right)
inorder_successor.right = node.right
inorder_successor.right.parent = inorder_successor
self.swap_nodes(node, inorder_successor)
inorder_successor.left = node.left
inorder_successor.left.parent = inorder_successor
inorder_successor.color = node.color
if original_color == "B":
self.fix_rb_delete(x)
# # Deletion w/ 2 children
# if node.right is not None and node.left is not None: # node is "x", inorder successor is "y"
# if node.right.left is not None: # Find inorder successor
# inorder_successor = node.right.left
# else:
# inorder_successor = node.right
#
# if parent is None:
# inorder_successor.left = node.left
# if node.right.left == inorder_successor:
# inorder_successor.right = node.right
# node.right.left = None
#
# if node.right == inorder_successor:
# node.right = None
#
# self.root = inorder_successor
#
# if parent.right == node:
# if node.left is not None:
# inorder_successor.left = node.left
# parent.right = inorder_successor
#
# else:
# if node.left is not None:
# inorder_successor.left = node.left
# parent.left = inorder_successor
#
# if inorder_successor.color == "B" and node.color == "B":
# self.fix_rb_delete(inorder_successor)
# else:
# del node
#
# # Delete leaf node
# if node.left is None and node.right is None: # node is "x", double_black is "y"
# node.color = "B"
# self.fix_rb_delete(node)
#
# # Delete node w/ 1 child
# if node.left is None and node.right is not None: # node is "x", child is "y"
# child = node.right
# if parent.left == node:
# parent.left = child
# else:
# parent.right = child
#
# if node.color == "B" and child.color == "B":
# self.fix_rb_delete(child)
# else:
# del node
#
# if node.left is not None and node.right is None:
# child = node.left
# if parent.right == node:
# parent.right = child
# else:
# parent.left = child
#
# if node.color == "B" and child.color == "B":
# self.fix_rb_delete(child)
# else:
# del node
def swap_nodes(self, a, b):
if a.parent is None:
self.root = b
if a.parent.left == a:
a.parent.left = b
else:
a.parent.right = b
b.parent = a.parent
def fix_rb_delete(self, node_to_del): # Rebalance after delete
parent = node_to_del.parent
while not node_to_del == self.root and node_to_del.color == "B":
if parent.left == node_to_del:
sibling = parent.right
if sibling is not None and sibling.color == "R":
sibling.color = "B"
parent.color = "R"
self.left_rotate(parent)
sibling = parent.right
if sibling is not None and sibling.left.color == "B" and sibling.right.color == "B":
sibling.color = "R"
node_to_del = parent
else:
if sibling is not None and sibling.right.color == "B":
sibling.left.color = "B"
sibling.color = "R"
self.right_rotate(sibling)
sibling = parent.right
sibling.color = parent.color
parent.color = "B"
sibling.right.color = "B"
self.left_rotate(parent)
self.root = node_to_del
else:
sibling = parent.left
if sibling is not None and sibling.color == "R":
sibling.color = "B"
parent.color = "R"
self.right_rotate(parent)
sibling = parent.left
if sibling is not None and sibling.right.color == "B" and sibling.left.color == "B":
sibling.color = "R"
node_to_del = parent
else:
if sibling is not None and sibling.left.color == "B":
sibling.right.color = "B"
sibling.color = "R"
self.left_rotate(sibling)
sibling = parent.left
sibling.color = parent.color
parent.color = "B"
sibling.left.color = "B"
self.right_rotate(parent)
self.root = node_to_del
# Case 2
self.root.color = "B"