-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
740 lines (522 loc) · 18.1 KB
/
main.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
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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
import random
from enum import Enum
import sys
sys.setrecursionlimit(3001)
#GENERATING THE SETS
#Setting Set X
n = random.randint(1000,3000)
print("\nn: " ,n)
X = set(random.sample(range(-3000,3000), n))
print(len(X))
#Setting Set Y
m = random.randint(500,1000)
print("\nm: ", m)
Y = set(random.sample(range(-3000,3000), m))
print(len(Y))
#Setting Set Z
k = random.randint(1000, 2000)
print("\nk: ", k)
Z = set(random.sample(range(-3000, 3000), k))
print(len(Z))
#Determining Common Variables
intersection = X.intersection(Y)
print("\nSets X and Y have %d elements in common" % len(intersection))
#AVL TREE
class AVL_NODE(object):
def __init__(self, content):
self.content = content
self.height = 1
self.left = None
self.right = None
class AVL(object):
def __init__(self):
self.nodes = 0
self.comparisons = 0
self.searches = 0
self.rotations = 0
#Returning Values
def retRotations(self):
return self.rotations
def retSearches(self):
return self.searches
def retCompares(self):
return self.comparisons
def retNodes(self, root):
if not root:
return
self.retNodes(root.left)
self.nodes += 1
self.retNodes(root.right)
def retNodeCount(self, root):
self.nodes = 0
self.retNodes(root)
return self.nodes
def retMin(self, root):
if root is None or root.left is None:
return root
return self.retMin(root.left)
def retBalance(self, root):
if not root:
return 0
#Left Subtree Height - Right Subtree Height
return self.retHeight(root.left) - self.retHeight(root.right)
#Traversals
def preOrder(self, root):
if not root:
return
print(root.content," ")
self.preOrder(root.left)
self.preOrder(root.right)
def inOrder(self, root):
if not root:
return
self.inOrder(root.left)
print(root.content," ", end="")
self.inOrder(root.right)
def postOrder(self, root):
if not root:
return
self.postOrder(root.left)
self.postOrder(root.right)
print(root.content," ", end="")
#Rotation Functionality
def leftrotate(self, x):
#Update Counter
self.rotations += 1
y = x.right
turn = y.left
#Rotation
y.left = x
x.right = turn
#Update Heights
x.height = 1 + max(self.retHeight(x.left), self.retHeight(x.right))
y.height = 1 + max(self.retHeight(y.left), self.retHeight(y.right))
#New Root
return y
def rightrotate(self, x):
#Update Counter
self.rotations += 1
y = x.left
turn = y.right
#Rotation
y.right = x
x.left = turn
#Update Heights
x.height = 1 + max(self.retHeight(x.left), self.retHeight(x.right))
y.height = 1 + max(self.retHeight(y.left), self.retHeight(y.right))
#New Root
return y
#INSERT FUNCTION
def insert(self, root, content):
self.comparisons += 1
if not root:
return AVL_NODE(content)
elif content < root.content:
root.left = self.insert(root.left, content)
else:
root.right = self.insert(root.right, content)
#Parent Height
root.height = 1 + max(self.retHeight(root.left), self.retHeight(root.right))
balance = self.retBalance(root)
#A balance larger than 1 implies an unbalanced tree and needs rotations
#Rotating and Rebalancing
#Left Left
if balance > 1:
if self.retBalance(root.left) >= 0:
return self.rightrotate(root)
#Left Right
else:
root.left = self.leftrotate(root.left)
return self.rightrotate(root)
#Right Right
if balance < -1:
if self.retBalance(root.right) <= 0:
return self.leftrotate(root)
#Right Left
else:
root.right = self.rightrotate(root.right)
return self.leftrotate(root)
return root
#DELETE FUNCTION
def delete(self, root, content):
if not root:
return root
elif content < root.content:
self.comparisons += 1
root.left = self.delete(root.left, content)
elif content > root.content:
self.comparisons += 1
root.right = self.delete(root.right, content)
else:
if root.left is None:
temp = root.right
root = None
return temp
elif root.right is None:
temp = root.left
root = None
return temp
temp = self.retMin(root.right)
root.content = temp.content
root.right = self.delete(root.right, temp.content)
if root is None:
return root
root.height = 1 + max(self.retHeight(root.left), self.retHeight(root.right))
balance = self.retBalance(root)
#Rebalancing the Tree under the same conditions as insertion
#Left Left
if balance > 1:
if self.retBalance(root.left) >= 0:
return self.rightrotate(root)
#Left Right
else:
root.left = self.leftrotate(root.left)
return self.rightrotate(root)
#Right Right
if balance < -1:
if self.retBalance(root.right) <= 0:
return self.leftrotate(root)
#Right Left
else:
root.right = self.rightrotate(root.right)
return self.leftrotate(root)
return root
#SEARCH FUNCTION
def search(self, root, content):
self.searches += 1
if root is None:
return False
elif root.content == content:
return True
elif root.content < content:
return self.search(root.right, content)
return self.search(root.left, content)
def retHeight(self, root):
if not root:
return 0
return root.height
#PRINTER
def printer(root, indent):
tab = 4
if root is None:
return
indent = indent + tab
printer(root.right, indent)
for i in range(tab, indent):
print(end=" ")
print(root.content)
printer(root.left, indent)
#RED BLACK TREE
class Colour(Enum):
Black = 1
Red = 2
class RBT_NODE():
def __init__(self, content = None, colour = Colour.Red):
self.right = None
self.left = None
self.parent = None
self.content = content
self.colour = colour
class RBT:
def __init__(self):
self.NULL = RBT_NODE(content = None, colour = Colour.Black)
self.root = self.NULL
self.size = 0
self.searches = 0
self.height = 0
self.rotations = 0
self.comparisons = 0
self.size = 0
#Returing Values
def retRotations(self):
return self.rotations
def retSearches(self):
return self.searches
def retSize(self):
return self.size
def retCompares(self):
return self.comparisons
#Minima and Maxima of Tree
def maximum(self):
if self.size == 0:
return "Empty Tree"
while self.root.right != self.NULL:
self.root = self.root.right
return self.root.content
def minimum(self):
if self.size == 0:
return "Empty Tree"
while self.root.left != self.NULL:
self.root = self.root.left
return self.root.content
def minode(self, x):
while x.left != self.NULL:
x = x.left
return x
#Height
def retHeight(self, root):
left_height = 0
right_height = 0
if root is None:
return 0
while root.right != self.NULL:
root = root.right
right_height += 1
while root.left != self.NULL:
root = root.left
left_height += 1
if left_height >= right_height:
self.height = left_height
else:
self.height = right_height
return self.height
#Traversals
def preorder(self, root):
if self.size == 0:
print("Empty")
return
if root != self.NULL and root.content != None:
print(root.content," ", end="")
self.preorder(root.left)
self.preorder(root.right)
def inorder(self, root):
if self.size == 0:
print("Empty")
return
if root != self.NULL and root.content != None:
self.inOrder(root.left)
print(root.content," ", end="")
self.inorder(root.right)
def postorder(self, root):
if self.size == 0:
print("Empty")
return
if root != self.NULL and root.content != None:
self.postorder(root.left)
self.postorder(root.right)
print(root.content," ", end="")
#Rotations
def leftrotate(self, x):
#Incrementing Rotation Counter
self.rotations += 1
y = x.right
x.right = y.left
if y.left != self.NULL:
y.left.parent = x
y.parent = x.parent
if x.parent == self.NULL:
self.root = y
elif x == x.parent.left:
x.parent.left = y
else:
x.parent.right = y
y.left = x
x.parent = y
#In Reality, simply substituting right for left and vice versa for the above
def rightrotate(self, x):
#Incrementing Rotation Counter
self.rotations += 1
y = x.left
x.left = y.right
if y.right != self.NULL:
y.right.parent = x
y.parent = x.parent
if x.parent == self.NULL:
self.root = y
elif x == x.parent.right:
x.parent.right = y
else:
x.parent.left = y
y.right = x
x.parent = y
#Insertion
def insert_helper(self, x):
i = 0
while x.parent.colour == Colour.Red:
if x.parent == x.parent.parent.left:
y = x.parent.parent.right
if y.colour == Colour.Red:
x.parent.colour = Colour.Black
y.colour = Colour.Black
x.parent.parent.colour = Colour.Red
x = x.parent.parent
else:
if x==x.parent.right:
x = x.parent
self.leftrotate(x)
x.parent.colour = Colour.Black
x.parent.parent.colour = Colour.Red
self.rightrotate(x.parent.parent)
else:
y = x.parent.parent.left
if y.colour == Colour.Red:
x.parent.colour = Colour.Black
y.colour = Colour.Black
x.parent.parent.colour = Colour.Red
x = x.parent.parent
else:
if x == x.parent.left:
x = x.parent
self.rightrotate(x)
x.parent.colour = Colour.Black
x.parent.parent.colour = Colour.Red
self.leftrotate(x.parent.parent)
i += 1
self.root.colour = Colour.Black
def insert(self, x):
node = RBT_NODE(content=x)
y = self.NULL
z = self.root
while z!= self.NULL:
y = z
self.comparisons += 1
if node.content < z.content:
z = z.left
else:
z = z.right
node.parent = y
if y == self.NULL:
self.root = node
elif node.content < y.content:
y.left = node
else:
y.right = node
node.left = self.NULL
node.right = self.NULL
node.colour = Colour.Red
self.insert_helper(node)
self.size += 1
#Searching and Searching by Key
def search(self, root, key):
self.searches += 1
if root == self.NULL:
return "Root does not Exist!"
elif key == root.content:
return "Found"
elif key < root.content:
return self.search(root.left, key)
else:
return self.search(root.right, key)
def key_search(self, root, key):
self.comparisons += 1
if root == self.NULL:
return None
elif key == root.content:
return self.root
elif key < root.content:
return self.key_search(root.left, key)
else:
return self.key_search(root.right, key)
def transplant(self, x, y):
if x.parent == self.NULL:
self.root = y
elif x == x.parent.left:
x.parent.left = y
else:
x.parent.right = y
y.parent = x.parent
#Deletion
def delete_helper(self, x):
while x!= self.root and x.colour == Colour.Black:
if x == x.parent.left:
a = x.parent.right
if a.colour == Colour.Red:
a.colour = Colour.Black
x.parent.colour = Colour.Red
self.leftrotate(x.parent)
a = x.parent.right
if a.left.colour == Colour.Black and a.right.colour == Colour.Black:
a.colour = Colour.Red
x = x.parent
else:
if a.right.colour == Colour.Black:
a.left.colour = Colour.Black
a.colour = Colour.Red
self.rightrotate(a)
a = x.parent.right
a.colour = x.parent.colour
x.parent.colour = Colour.Black
a.right.colour = Colour.Black
self.leftrotate(x.parent)
x = self.root
else:
a = x.parent.left
if a.colour == Colour.Red:
a.colour = Colour.Black
x.parent.colour = Colour.Red
self.rightrotate(x.parent)
a = x.parent.left
if a.right.colour == Colour.Black and a.left.colour == Colour.Black:
a.colour = Colour.Red
x = x.parent
else:
if a.left.colour == Colour.Black:
a.right.colour = Colour.Black
a.colour = Colour.Red
self.leftrotate(a)
a = x.parent.left
a.colour = x.parent.colour
x.parent.colour = Colour.Black
a.left.colour = Colour.Black
self.rightrotate(x.parent)
x = self.root
x.colour = Colour.Black
def delete(self, z):
if self.size == 0:
print("Empty Tree")
return
node1 = self.key_search(self.root, z)
w = node1
if node1 == None:
return
col = w.colour
if node1.left == self.NULL:
x = node1.right
self.transplant(node1, node1.right)
elif node1.right == self.NULL:
x = node1.left
self.transplant(node1, node1.right)
else:
w = self.minode(node1.right)
x = w.right
if w.parent == node1:
x.parent = w
else:
self.transplant(w, w.right)
w.right = node1.right
w.right.parent = w
self.transplant(node1, w)
w.left = node1.left
w.left.parent = w
w.colour = node1.colour
if col == Colour.Black:
self.delete_helper(x)
self.size -= 1
if __name__ == '__main__':
AVLTREE = AVL()
Root = None
RBTREE = RBT()
for y in X:
Root = AVLTREE.insert(Root, y)
for i in X:
RBTREE.insert(i)
print("After Insertion\n")
print("AVL {} Rotations, Height {}, Nodes {}, Comparisons {}".format(AVLTREE.retRotations(), AVLTREE.retHeight(Root), AVLTREE.retNodeCount(Root), AVLTREE.retCompares()))
print("RBT {} Rotations, Height {}, Nodes {}, Comparisons {}".format(RBTREE.retRotations(), RBTREE.retHeight(RBTREE.root), RBTREE.retSize(), RBTREE.retCompares()))
#Deletion
for i in Y:
AVLTREE.delete(Root, i)
for i in Y:
RBTREE.delete(i)
print("After Deletion\n")
print("AVL {} Rotations, Height {}, Nodes {}, Comparisons {}".format(AVLTREE.retRotations(), AVLTREE.retHeight(Root), AVLTREE.retNodeCount(Root), AVLTREE.retCompares()))
print("RBT {} Rotations, Height {}, Nodes {}, Comparisons {}".format(RBTREE.retRotations(), RBTREE.retHeight(RBTREE.root), RBTREE.retSize(), RBTREE.retCompares()))
#Searching
for i in Z:
AVLTREE.search(Root, i)
for i in Z:
RBTREE.search(RBTREE.root, i)
print("After Searching\n")
print("K Value: ", k)
print("AVL: {} Total Comparisons".format(AVLTREE.retCompares()))
print("RBT: {} Total Comparisons".format(RBTREE.retCompares()))