-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathredblacktree.py
407 lines (353 loc) · 12.3 KB
/
redblacktree.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
"""
# Structure of a red black tree
1. A node is either red or black.
2. The root is black.
3. All leaves are black.
4. If a node is red, then both its children are black.
5. Every path from a given node to a leaf node has the same
number of black nodes.
"""
class Node(object):
""" Implementation of red black tree node
a node has value, color (RED or BLACK),
parent node (node or None) and left and right child (node or None)
"""
RED = True
BLACK = False
def __init__(self, value, color=RED):
self.color = color
self.value = value
self.left = None
self.right = None
self.parent = None
def __str__(self):
return str(self.value) + ':' + str('R' if self.color else 'B')
def verbose(self):
return '{} (parent:{} left:{} right:{})'.format(
self, self.parent, self.left, self.right)
class RedBlackTree:
""" Implementation of Red Black Tree """
def __init__(self):
self.root = None
def max_depth(self, root=None):
""" return max depth of tree """
if root is None:
return 0
else:
return max(self.max_depth(root.left),
self.max_depth(root.right)) + 1
def depth(self, node):
""" returns the value of the node depth
relative to the root of the tree
"""
if node is None:
return 0
node_ = node
depth = 0
while node_ != self.root:
node_ = node_.parent
depth += 1
return depth
def min(self, current=None):
""" return minimum value in tree """
if not current:
current = self.root
while current.left is not None:
current = current.left
return current
def max(self, current=None):
""" return maximum value in tree """
if not current:
current = self.root
while current.right is not None:
current = current.right
return current
def search(self, value):
""" return a Node with given value otherwise None"""
return self.__search(self.root, value)
def __search(self, node, value):
while node is not None and value != node.value:
if value < node.value:
node = node.left
else:
node = node.right
return node
def successor(self, value):
""" return a node with nearest number that is more than given """
current = self.search(value)
if current is None:
raise Exception(('a Node with value ({})'
' does not exist').format(value))
return self.__successor(current)
def __successor(self, current):
if current.right is not None:
return self.min(current.right)
while (current.parent is not None
and current.parent.right is current):
current = current.parent
return current.parent
def insert(self, key):
""" insert a Node with given key to Red Black Tree """
# define a new Node
node = Node(key)
# start from root of tree
x = self.root
y = None
while x is not None:
# find a parent for Node
y = x
if key < x.value:
x = x.left
else:
x = x.right
# set parent for current Node
node.parent = y
if y is None:
# set Node as new tree root
self.root = node
elif key < y.value:
# set Node as left branch
y.left = node
else:
# set Node as right branch
y.right = node
# set default value for current Node
node.left = None
node.right = None
node.color = Node.RED
# run fixup function for
# restore red black properties of the tree
self.__insert_fixup(node)
def __insert_fixup(self, x):
""" restore red-black tree properties after insert new node """
while x != self.root and x.parent.color == Node.RED:
# we have a violation
if x.parent == x.parent.parent.left:
# we are on left branch
y = x.parent.parent.right
if y is not None and y.color == Node.RED:
# parent is red
x.parent.color = Node.BLACK
y.color = Node.BLACK
x.parent.parent.color = Node.RED
x = x.parent.parent
else:
# uncle is black
if x == x.parent.right:
# make x a left child
x = x.parent
self.__left_rotate(x)
# recolor and rotate
x.parent.color = Node.BLACK
x.parent.parent.color = Node.RED
self.__right_rotate(x.parent.parent)
else:
# mirror image of above code
y = x.parent.parent.left
if y is not None and y.color == Node.RED:
# parent is red
x.parent.color = Node.BLACK
y.color = Node.BLACK
x.parent.parent.color = Node.RED
x = x.parent.parent
else:
# parent is black
if x == x.parent.left:
x = x.parent
self.__right_rotate(x)
x.parent.color = Node.BLACK
x.parent.parent.color = Node.RED
self.__left_rotate(x.parent.parent)
self.root.color = Node.BLACK
def __left_rotate(self, x):
""" transformation of the left subtree to the right subtree """
if not x.right:
raise Exception("a right branch of Node is None")
# get right subtree
y = x.right
# transformation of the left subtree to the right
x.right = y.left
if y.left:
y.left.parent = x
# set new parent
y.parent = x.parent
if not x.parent:
# set new root
self.root = y
else:
if x == x.parent.left:
# we are on left branch
x.parent.left = y
else:
x.parent.right = y
# set x as left parent node
y.left = x
x.parent = y
def __right_rotate(self, x):
""" transformation of the right subtree to the left subtree """
if not x.left:
raise Exception("a right branch of Node is None")
# get left subtree
y = x.left
# transformation of the right subtree to the left
x.left = y.right
if y.right:
y.right.parent = x
# set new parent
y.parent = x.parent
if not x.parent:
# set new root
self.root = y
else:
if x == x.parent.left:
# we are on left branch
x.parent.left = y
else:
x.parent.right = y
# set x as right parent node
y.right = x
x.parent = y
def transplant(self, node, newnode):
""" transplant new node to current node """
if node.parent is None:
self.root = newnode
elif node == node.parent.left:
node.parent.left = newnode
else:
node.parent.right = newnode
if newnode is not None:
newnode.parent = node.parent
def delete(self, value):
""" delete value from tree """
node = self.search(value)
if node:
self.__delete(node)
def __delete(self, node):
y = node
color = y.color
if node.left is None:
x = node.right
self.transplant(node, node.right)
elif node.right is None:
x = node.left
self.transplant(node, node.left)
else:
y = self.min(node.right)
color = y.color
x = y.right
if x is not None and x.parent is not None and y.parent == node:
x.parent = y
else:
self.transplant(y, y.right)
y.right = node.right
y.right.parent = y
self.transplant(node, y)
y.left = node.left
y.left.parent = y
y.color = node.color
if x and color == Node.BLACK:
self.__delete_fixup(x)
def __delete_fixup(self, x):
""" restore red-black tree properties after insert new node """
while x != self.root and x.color == Node.BLACK:
# we have a violation
if x == x.parent.left:
# we are on left branch
y = x.parent.right
if y is not None and y.color == Node.RED:
# parent is red
y.color = Node.BLACK
x.parent.color = Node.RED
x = x.parent.parent
self.__left_rotate(x.parent)
y = x.parent.right
if y.left.color == Node.BLACK and y.right.color == Node.BLACK:
y.color = Node.RED
x = x.parent
else:
if y.right.color == Node.BLACK:
y.left.color = Node.BLACK
y.color = Node.RED
self.__right_rotate(y)
y = x.parent.right
y.color = x.parent.color
x.parent.color = Node.BLACK
y.right.color = Node.BLACK
self.__left_rotate(x.parent)
x = self.root
else:
y = x.parent.left
if y is not None and y.color == Node.RED:
# parent is red
y.color = Node.BLACK
x.parent.color = Node.RED
x = x.parent.parent
self.__right_rotate(x.parent)
y = x.parent.left
if y.right.color == Node.BLACK and y.left.color == Node.BLACK:
y.color = Node.RED
x = x.parent
else:
if y.left.color == Node.BLACK:
y.right.color = Node.BLACK
y.color = Node.RED
self.__left_rotate(y)
y = x.parent.left
y.color = x.parent.color
x.parent.color = Node.BLACK
y.left.color = Node.BLACK
self.__right_rotate(x.parent)
x = self.root
x.color = Node.BLACK
def __str__(self):
""" return a string representation of Tree """
# a variable to hold the node in ascending order
sortnodes = []
if self.root is None:
return ""
# last node in tree
maxnode = self.max(self.root)
# first node in tree
node = self.min(self.root)
while True:
sortnodes.append((node, self.depth(node)))
if node == maxnode:
break
# next node
node = self.__successor(node)
# max depth of tree
maxdepth = self.max_depth(self.root)
# list of tree strings
strings = ['' for _ in range(maxdepth + 1)]
for node, rank in sortnodes:
for level in range(maxdepth + 1):
if rank == level:
strings[level] += str(node)
else:
strings[level] += ' ' * len(str(node))
return "\n".join(strings)
if __name__ in "__main__":
# example 1
print('example 1')
print("=" * 30)
tree = RedBlackTree()
for i in [0, -12, -8, 10, -100]:
print('insert {} to tree'.format(i))
tree.insert(i)
print(tree)
for i in [-100, -8]:
print('delete {} from tree'.format(i))
tree.delete(i)
print(tree)
# example 2
print('example 2')
print("=" * 30)
tree = RedBlackTree()
for i in range(1, 11):
print('insert {} to tree'.format(i))
tree.insert(i)
print(tree)
for i in range(1, 11):
print('delete {} from tree'.format(i))
tree.delete(i)
print(tree)