-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedBlackTree.py
More file actions
665 lines (628 loc) · 25.4 KB
/
RedBlackTree.py
File metadata and controls
665 lines (628 loc) · 25.4 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
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
"""
implement red black tree
"""
from Node import Node
class RedBlackTree:
def __init__(self):
"""
initialize a red black tree
"""
# initialize tree head
self.tree = Node(float("inf"), None, color=False)
# initialize root node
self.tree.setLeft(self._initializeNewNode())
self.tree.setRight(Node(None, None))
self.tree.left.setBlack()
@staticmethod
def _initializeNewNode(value=None):
"""
construct a new red node with two black leaves
:return: the constructed new node
"""
node = Node(value, None, color=True)
node.setLeft(Node(None, None))
node.setRight(Node(None, None))
if value is not None:
node.updateElementsNumber()
return node
def _balanceRotation(self, current_node):
"""
blance the tree
:return:
"""
def _leftLeft(node):
"""
case1: node is its father's left child, node and its father are both red and the node's father is its father's left child
:param node: the current node
:return: the root node of balanced subtree
"""
# obtain the father of the node
node_father = node.father
# obtain the root of the subtree
subtree_root = node_father.father
# obtain the father of the subtree
subtree_father = subtree_root.father
# set node_father as the root node of the sub tree
# check wheter the subtree is left or right childrenn of subtree_father
if subtree_root.value <= subtree_father.value:
# left
subtree_father.setLeft(node_father)
else:
# right
subtree_father.setRight(node_father)
# set the right children of node.father as subtree_father's left children
subtree_root.setLeft(node_father.right)
# set subtree_father as the right children of node.father
node_father.setRight(subtree_root)
# change the color of node
node.setBlack()
# update elements number
subtree_root.updateElementsNumber()
node_father.updateElementsNumber()
return node_father
def _rightLeft(node):
"""
case2: node is its father's right child, node and its father are both red, and the node's father is its father's left child
:param node: the current node
:return: the root node of balanced subtree
"""
# obtain the father of the node
node_father = node.father
# obtain the root of the subtree
subtree_root = node_father.father
# obtain the father of the subtree
subtree_father = subtree_root.father
# set node as the root node of the sub tree
# check wheter the subtree is left or right childrenn of subtree_father
if subtree_root.value <= subtree_father.value:
# left
subtree_father.setLeft(node)
else:
# right
subtree_father.setRight(node)
# set node's left children as the right children of node_father
node_father.setRight(node.left)
# set node's right children as the left children of subtree_root
subtree_root.setLeft(node.right)
# set subtree_root as node's right children
node.setRight(subtree_root)
# set node_father as node's left children
node.setLeft(node_father)
# change the color of node_father
node_father.setBlack()
# update elements number
subtree_root.updateElementsNumber()
node_father.updateElementsNumber()
node.updateElementsNumber()
return node
def _rightRight(node):
"""
case3: node is its father's right child, node and its father are both red and the node's father is its father's right child
:param node: the current node
:return: the root node of balanced subtree
"""
# obtain the father of the node
node_father = node.father
# obtain the root of the subtree
subtree_root = node_father.father
# obtain the father of the subtree
subtree_father = subtree_root.father
# set node_father as the root node of the sub tree
# check whether the subtree is left or right children of subtree_father
if subtree_root.value <= subtree_father.value:
# left
subtree_father.setLeft(node_father)
else:
# right
subtree_father.setRight(node_father)
# set the left children of node_father as subtree_father's right children
subtree_root.setRight(node_father.left)
# set subtree_father as the left children of node.father
node_father.setLeft(subtree_root)
# change the color of node
node.setBlack()
# update elements number
subtree_root.updateElementsNumber()
node_father.updateElementsNumber()
return node_father
def _leftRight(node):
"""
case4: node is its father's left child, node and its father are both red, and the node's father is its father's right child
:param node: the current node
:return: the root node of balanced subtree
"""
# obtain the father of the node
node_father = node.father
# obtain the root of the subtree
subtree_root = node_father.father
# obtain the father of the subtree
subtree_father = subtree_root.father
# set node as the root node of the sub tree
# check wheter the subtree is left or right childrenn of subtree_father
if subtree_root.value <= subtree_father.value:
# left
subtree_father.setLeft(node)
else:
# right
subtree_father.setRight(node)
# set node's right children as the left children of node_father
node_father.setLeft(node.right)
# set node's left children as the right children of subtree_root
subtree_root.setRight(node.left)
# set subtree_root as node's left children
node.setLeft(subtree_root)
# set node_father as node's right children
node.setRight(node_father)
# change the color of node_father
node_father.setBlack()
# update elements number
subtree_root.updateElementsNumber()
node_father.updateElementsNumber()
node.updateElementsNumber()
return node
# judge if the current node is root node
if current_node.father.father is None:
# paint current node black
if current_node.judgeRed():
current_node.setBlack()
return
# balance the tree
# when two consecutive red nodes occur
if current_node.judgeRed() and current_node.father.judgeRed():
if current_node.father.value <= current_node.father.father.value:
# the node's father is its father's left child
if current_node.value <= current_node.father.value:
# case1: the node is its father's left child
next_node = _leftLeft(current_node)
else:
# case2: the node is its father's right child
next_node = _rightLeft(current_node)
else:
# the node's father is its father's right child
if current_node.value > current_node.father.value:
# case3: the node is its father's right child
next_node = _rightRight(current_node)
else:
# case4: the node is its father's left child
next_node = _leftRight(current_node)
else:
next_node = current_node.father
self._balanceRotation(next_node)
def delete(self, data):
"""
delete the element with the value of data
:param data: the value to be deleted
:return:
"""
def delete_leaf(leaf):
"""
delete the node (maybe a leaf) in the red-black tree
:param leaf: the leaf to be deleted
:return:
"""
# when leaf is red, just delete it
if leaf.judgeRed():
if leaf.value <= leaf.father.value:
leaf.father.setLeft(Node(None, None))
else:
leaf.father.setRight(Node(None, None))
# update elements number
update_scale(leaf.father)
else:
# when leaf is black
# if leaf have no child
if leaf.right.judgeLeaf():
if leaf.value <= leaf.father.value:
leaf.father.setLeft(Node(None, None))
update_scale(leaf.father)
balanceLeft(leaf.father.left)
else:
leaf.father.setRight(Node(None, None))
update_scale(leaf.father)
balanceRight(leaf.father.right)
else:
# just paint the right child black and delete leaf
leaf.right.setBlack()
# delete leaf
if leaf.value <= leaf.father.value:
leaf.father.setLeft(leaf.right)
else:
leaf.father.setRight(leaf.right)
update_scale(leaf.father)
# detach leaf
leaf_value = leaf.value
del leaf
return leaf_value
def update_scale(node):
"""
update the number of elements in the subtree
:param node: the root of a subtree
:return:
"""
node.updateElementsNumber()
if node.father is None:
return
if node.father.father is None:
return
else:
update_scale(node.father)
def balanceLeft(node):
"""
balance if left children's black height is one less than that of right children
:param node:
:return:
"""
# judge if the node is root node
if node.father.father is None:
# paint node black
if node.judgeRed():
node.setBlack()
return
# if node's right brother is black
if not node.father.right.judgeRed():
# if node's brother's children is black
if (not node.father.right.left.judgeRed()) and (not node.father.right.right.judgeRed()):
# if node's father is black
if not node.father.judgeRed():
# paint node's brother red
node.father.right.setRed()
# node's father is the root of the subtree to be balanced
if node.father.value <= node.father.father.value:
balanceLeft(node.father)
else:
balanceRight(node.father)
else:
# swap the color between node's brother an d node's father
swapColor(node.father, node.father.right)
else:
if node.father.right.right.judgeRed():
leftRotate(node.father)
else:
rightRotate(node.father.right)
balanceLeft(node)
else:
leftRotate(node.father)
balanceLeft(node)
def balanceRight(node):
"""
balance if left children's black height is one more than that of right children
:param node:
:return:
"""
# judge if the node is root node
if node.father.father is None:
# paint node black
if node.judgeRed():
node.setBlack()
return
# if node's left brother is black
if not node.father.left.judgeRed():
# if node's brother's children is black
if (not node.father.left.left.judgeRed()) and (not node.father.left.right.judgeRed()):
# if node's father is black
if not node.father.judgeRed():
# paint node's brother red
node.father.left.setRed()
# node's father is the root of the subtree to be balanced
if node.father.value <= node.father.father.value:
balanceLeft(node.father)
else:
balanceRight(node.father)
else:
# swap the color between node's brother an d node's father
swapColor(node.father, node.father.left)
else:
if node.father.left.left.judgeRed():
rightRotate(node.father)
else:
leftRotate(node.father.left)
balanceRight(node)
else:
rightRotate(node.father)
balanceRight(node)
def leftRotate(node):
"""
letf rotate the subtree
:param node: the root of the sub tree
:return:
"""
# obtain subtree father
subtree_father = node.father
# obtain new root
new_root = node.right
# obtain new root's left child
new_root_left = new_root.left
# right rotate
if node.value <= subtree_father.value:
subtree_father.setLeft(new_root)
else:
subtree_father.setRight(new_root)
node.setRight(new_root_left)
new_root.setLeft(node)
new_root.right.setBlack()
swapColor(node, new_root)
# update elements number
node.updateElementsNumber()
new_root.updateElementsNumber()
def rightRotate(node):
"""
right rotate the subtree
:param node: the root of the sub tree
:return:
"""
# obtain subtree father
subtree_father = node.father
# obtain new root
new_root = node.left
# obtain new root's right child
new_root_right = new_root.right
# right rotate
if node.value <= subtree_father.value:
subtree_father.setLeft(new_root)
else:
subtree_father.setRight(new_root)
node.setLeft(new_root_right)
new_root.setRight(node)
new_root.left.setBlack()
swapColor(node, new_root)
# update elements number
node.updateElementsNumber()
new_root.updateElementsNumber()
def swapColor(node1, node2):
"""
swap the color of two nodes
:param node1:
:param node2:
:return:
"""
# obtain the color if two node
node1_color = node1.color
node2_color = node2.color
# swap color
node1.setColor(node2_color)
node2.setColor(node1_color)
def _min(subtree):
"""
return the min value of the subtree
:return:
"""
pointer = subtree
while True:
if pointer.left.value is None:
break
else:
pointer = pointer.left
return pointer
# search if data to be deleted exists in the tree
deletedData = self.search(data)
# if the value exists
if deletedData is not None:
if deletedData.right.value is not None:
# obtain the successive node
successive_value = delete_leaf(_min(deletedData.right))
else:
# the deleted node is a leaf
if deletedData.left.value is None:
delete_leaf(deletedData)
return
# the deleted node has a red left child
else:
successive_value = delete_leaf(deletedData.left)
deletedData.value = successive_value
def insert(self, data):
"""
insert an element
:param data: a new element
:return:
"""
def _judgeLeftOrRight(query_data, node):
"""
judege a data to the left or right child
:param query_data: the data
:param node: the current node
:return:
"""
# update the number of elements in the subtree of current node
node.addElementsNumber()
if query_data <= node.value:
# allocate data to left child of node
if node.left.value is None:
# assign the left child with the value of query_data
del node.left
node.setLeft(self._initializeNewNode(query_data))
return node.left
else:
return _judgeLeftOrRight(query_data, node.left)
else:
# allocate data to right child of node
if node.right.value is None:
# assign the right child with the value of query_data
del node.right
node.setRight(self._initializeNewNode(query_data))
return node.right
else:
return _judgeLeftOrRight(query_data, node.right)
# assign the value to the root node if the tree is empty
if self.tree.left.value is None:
self.tree.setLeft(self._initializeNewNode())
self.tree.left.setBlack()
self.tree.left.value = data
self.tree.left.updateElementsNumber()
return
# if data is not exists in the tree
if self.search(data) is None:
# obtain the handle of the new inserted node
new_node = _judgeLeftOrRight(data, self.tree.left)
self._balanceRotation(new_node)
def traverse(self):
"""
traverse the tree
:return: results of all the values in the tree
"""
def inOrderTraverse(node, results):
"""
in-order traverse the tree
:param results: a lists store values already traversed
:param node: the root node of the subtree
:return:
"""
if node.value is None:
return
inOrderTraverse(node.left, results)
results.append(node.value)
inOrderTraverse(node.right, results)
# initialize a result list
results = []
# traverse
inOrderTraverse(self.tree.left, results)
return results
def median(self):
"""
find the median value of the tree
:return:
"""
def indexing(sub_tree, index):
"""
recursively find the value of the input tree indexed by a particular number
:param sub_tree:
:return:
"""
if index == sub_tree.left.elements_number + 1:
return sub_tree.value
if index <= sub_tree.left.elements_number:
return indexing(sub_tree.left, index)
else:
return indexing(sub_tree.right, index - sub_tree.left.elements_number - 1)
# obtain the total number of elements in the red-black tree
elements_number = self.tree.left.elements_number
# even then return average value of the two middle values
# odd then return the median value
if elements_number % 2 == 0:
# even
index_left = elements_number / 2
index_right = index_left + 1
median_value = (indexing(self.tree.left, index_left) + indexing(self.tree.left, index_right)) / 2
return median_value
else:
# odd
index = int(elements_number / 2) + 1
return indexing(self.tree.left, index)
def min(self):
"""
return the min value of the tree
:return:
"""
pointer = self.tree.left
while True:
if pointer.left.value is None:
break
else:
pointer = pointer.left
return pointer.value
def max(self):
"""
return the max value of the tree
:return:
"""
pointer = self.tree.left
while True:
if pointer.right.value is None:
break
else:
pointer = pointer.right
return pointer.value
def search(self, query):
"""
seach if a value exists in the tree
:param query: a query value
:return: the node with the value of query if the qyery exists in the tree, else None
"""
pointer = self.tree.left
while True:
if pointer.value == query:
return pointer
elif query < pointer.value:
if pointer.left.value is None:
return None
else:
pointer = pointer.left
else:
if pointer.right.value is None:
return None
else:
pointer = pointer.right
def verify(self):
"""
test if current tree is a red black tree
:return:
"""
def blackPathLength(node):
"""
calculate the number of black nodes in the node's left children and right children
:param node:
:return: False if the tree is not a red black tree or the black height of the tree
"""
if node.value is None:
# if the node is a leaf
return 1
# judge if two consecutive red nodes exist
if (node.left.judgeRed() and node.judgeRed()) or (node.right.judgeRed() and node.judgeRed()):
return False
# calculate the number of black node in the left path
left_path_length = blackPathLength(node.left)
# calculate the number of black node in the right path
right_path_length = blackPathLength(node.right)
# compare the number of black nodes in left path and right path\
if isinstance(left_path_length, int) and isinstance(right_path_length, int):
if left_path_length == right_path_length:
if not node.judgeRed():
return left_path_length + 1
else:
return left_path_length
else:
return False
else:
return False
def judge_upward():
"""
judge if the elements is upward after in-order traverse
:param node:
:return:
"""
sequences = self.traverse()
for i in range(len(sequences)):
if i + 1 == len(sequences):
break
if sequences[i] > sequences[i+1]:
return False
return True
return (not self.tree.left.judgeRed()) and judge_upward(), blackPathLength(self.tree.left)
def intersection(tree1, tree2):
"""
calculate the values both in red black tree1 and red black tree2
:param tree1: the first red black tree
:param tree2: the second red black tree
:return:
"""
# obtain all values in each tree with an upward order
tree1_elements = tree1.traverse()
tree2_elements = tree2.traverse()
# obtain the muber of elements in the two trees
tree1_length = tree1.tree.left.elements_number
tree2_length = tree2.tree.left.elements_number
# initialize intersection
intersection_set = []
i = 0 # tree1 index
j = 0 # tree2 index
while True:
if i == tree1_length or j == tree2_length:
break
if tree1_elements[i] == tree2_elements[j]:
intersection_set.append(tree1_elements[i])
i += 1
j += 1
elif tree1_elements[i] < tree2_elements[j]:
i += 1
else:
j += 1
return intersection_set