-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_avl.py
684 lines (590 loc) · 24.9 KB
/
test_avl.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
# DO NOT MODIFY THIS FILE
# Run me via: python3 -m unittest test_avl
import unittest
import time
from avl import AVLTree
class TestAVLTree(unittest.TestCase):
"""
Initialization
"""
def test_instantiation(self):
"""
Test 1: A AVLTree exists.
"""
try:
AVLTree()
except NameError:
self.fail("Could not instantiate AVLTree.")
# def test_initial_attributes(self):
# """
# Test 2: A AVL Tree is a recursive structure. When we refer to an object that "is a avl_tree,"
# we are referring to a root node of an AVL tree.
# Every node has a left child, right child, key, height, and balance factor.
# A new AVLTree has a left, right, key, that are each None, a height set to 1 and a balance factor set to 0.
# Hint: Define an initializer.
# """
# avl_tree = AVLTree()
# self.assertIsNone(avl_tree.left)
# self.assertIsNone(avl_tree.right)
# self.assertIsNone(avl_tree.key)
# self.assertEqual(1, avl_tree.height)
# self.assertEqual(0, avl_tree.balance_factor)
# # """
# # Cute, single-level trees. (Depth of zero.)
# # Insertion
# # When inserting a node, calculate the height of each node
# # """
# def test_height_single_smaller(self):
# """
# Test 3: Inserting a node into a single-level tree appends the new node as the
# left child, when the new node key is less than the parent's key.
# (A new node whose key is <= parent key becomes the left child.)
# The height of each node is equal to the height of its largest child + 1
# """
# avl_tree = AVLTree(5)
# child = AVLTree(1)
# avl_tree.insert(child)
# self.assertEqual(child, avl_tree.left)
# self.assertEqual(2, avl_tree.height)
# self.assertEqual(1, avl_tree.left.height)
# def test_height_single_equal(self):
# """
# Test 4: Inserting a node into a single-level tree appends the new node as the
# left child, when the new node value is equal to the the parent's key.
# (A new node whose key is <= parent key becomes the left child.)
# The height of each node is equal to the height of its largest child + 1
# """
# avl_tree = AVLTree(5)
# child = AVLTree(5)
# avl_tree.insert(child)
# self.assertEqual(child, avl_tree.left)
# self.assertEqual(2, avl_tree.height)
# self.assertEqual(1, avl_tree.left.height)
# def test_height_single_greater(self):
# """
# Test 5: Inserting a node into a single-level tree appends the new node as the
# right child, when the new node key is greater than the parent's key.
# (A new node whose key is > parent key becomes the right child.)
# The height of each node is equal to the height of its largest child + 1
# """
# avl_tree = AVLTree(5)
# child = AVLTree(7)
# avl_tree.insert(child)
# self.assertEqual(child, avl_tree.right)
# self.assertEqual(2, avl_tree.height)
# self.assertEqual(1, avl_tree.right.height)
# # """
# # Cute, single-level trees. (Depth of zero.)
# # Insertion
# # When inserting a node, calculate the balance factor of each node
# # """
# def test_balance_factor_single_smaller(self):
# """
# Test 6: Inserting a node into a single-level tree appends the new node as the
# left child, when the new node key is less than the parent's key.
# (A new node whose key is <= parent key becomes the left child.)
# The balance factor of each node is equal to the height of its left sub-tree
# minus the height of its right subtree
# """
# avl_tree = AVLTree(5)
# child = AVLTree(1)
# avl_tree.insert(child)
# self.assertEqual(child, avl_tree.left)
# self.assertEqual(1, avl_tree.balance_factor)
# self.assertEqual(0, avl_tree.left.balance_factor)
# def test_balance_factor_single_equal(self):
# """
# Test 7: Inserting a node into a single-level tree appends the new node as the
# left child, when the new node value is equal to the the parent's key.
# (A new node whose key is <= parent key becomes the left child.)
# The balance factor of each node is equal to the height of its left sub-tree
# minus the height of its right subtree
# """
# avl_tree = AVLTree(5)
# child = AVLTree(5)
# avl_tree.insert(child)
# self.assertEqual(child, avl_tree.left)
# self.assertEqual(2, avl_tree.height)
# self.assertEqual(1, avl_tree.left.height)
# def test_balance_factor_single_greater(self):
# """
# Test 8: Inserting a node into a single-level tree appends the new node as the
# right child, when the new node key is greater than the parent's key.
# (A new node whose key is > parent key becomes the right child.)
# The balance factor of each node is equal to the height of its left sub-tree
# minus the height of its right subtree
# """
# avl_tree = AVLTree(5)
# child = AVLTree(7)
# avl_tree.insert(child)
# self.assertEqual(child, avl_tree.right)
# self.assertEqual(2, avl_tree.height)
# self.assertEqual(1, avl_tree.right.height)
# # """
# # Toddler, two-level trees. (Depth of one.)
# # Insertion
# # Calculate height and balance factor
# # # """
# def test_height_insert_two_smaller_left(self):
# """
# Test 9: Inserting a node with a key that is less than the left child's key appends
# the new node as the left child's left child.
# The height of each node is equal to the height of its largest child + 1
# 5 5
# / => /
# 3 3
# /
# 1
# """
# avl_tree = AVLTree(5)
# left = AVLTree(3)
# child = AVLTree(1)
# avl_tree.insert(left)
# avl_tree.insert(child)
# self.assertEqual(left, avl_tree.left)
# self.assertEqual(child, avl_tree.left.left)
# self.assertEqual(3, avl_tree.height)
# self.assertEqual(2, avl_tree.left.height)
# self.assertEqual(1, avl_tree.left.left.height)
# def test_balance_factor_insert_two_smaller_left(self):
# """
# Test 10: Inserting a node with a key that is less than the left child's key appends
# the new node as the left child's left child.
# The height of each node is equal to the height of its largest child + 1
# 5 5
# / => /
# 3 3
# /
# 1
# """
# avl_tree = AVLTree(5)
# left = AVLTree(3)
# child = AVLTree(1)
# avl_tree.insert(left)
# avl_tree.insert(child)
# self.assertEqual(left, avl_tree.left)
# self.assertEqual(child, avl_tree.left.left)
# self.assertEqual(2, avl_tree.balance_factor)
# self.assertEqual(1, avl_tree.left.balance_factor)
# self.assertEqual(0, avl_tree.left.left.balance_factor)
# #********************************************
# # !!!!!IMPORTANT!!!!!
# #**********************************************
# # COMMENT OUT THE FOLLOWING LINES IN THIS FILE BEFORE CONTINUING
# # 162-166
# # 186-190
# #************************************************
# def test_return_root_insert_two_smaller(self):
# """
# Test 11: Insert returns the root of the three
# 5 5
# => /
# 3
# """
# avl_tree = five = AVLTree(5)
# three = AVLTree(3)
# avl_tree_root=avl_tree.insert(three)
# self.assertEqual(five, avl_tree_root)
# # """
# # Toddler, two-level trees.
# # Insertion
# # Left Imbalance, right rotation
# # # """
# def test_right_rotate_insert_two_smaller(self):
# """
# Test 12: Inserting a node with a key that is less than the left child's key
# causes a right rotation
# 5 3
# / => / \
# 3 1 5
# /
# 1
# HINT: create a right_rotate method
# """
# avl_tree = five = AVLTree(5)
# three = AVLTree(3)
# one = AVLTree(1)
# avl_tree=avl_tree.insert(three)
# avl_tree=avl_tree.insert(one)
# self.assertEqual(three, avl_tree)
# self.assertEqual(one, avl_tree.left)
# self.assertEqual(five, avl_tree.right)
# def test_height_right_rotate_insert_two_smaller(self):
# """
# Test 13: Inserting a node with a key that is less than the left child's key
# causes a right rotation
# The height of each node is equal to the height of its largest child + 1
# 5 3
# / => / \
# 3 1 5
# /
# 1
# """
# avl_tree = five= AVLTree(5)
# three = AVLTree(3)
# one = AVLTree(1)
# avl_tree=avl_tree.insert(three)
# avl_tree=avl_tree.insert(one)
# self.assertEqual(2, three.height)
# self.assertEqual(1, one.height)
# self.assertEqual(1, five.height)
# def test_balance_factor_right_rotate_insert_two_smaller(self):
# """
# Test 14: Inserting a node with a key that is less than the left child's key
# causes a right rotation
# The balance factor of each node is equal to the difference between the height
# of the left subtree and the right subtree
# 5 3
# / => / \
# 3 1 5
# /
# 1
# """
# avl_tree = five= AVLTree(5)
# three = AVLTree(3)
# one = AVLTree(1)
# avl_tree=avl_tree.insert(three)
# avl_tree=avl_tree.insert(one)
# self.assertEqual(0, three.balance_factor)
# self.assertEqual(0, one.balance_factor)
# self.assertEqual(0, five.balance_factor)
# # """
# # Toddler, two-level trees.
# # Insertion
# # Right Imbalance, Left rotatation
# # # """
# def test_left_rotate_insert_two_smaller_left(self):
# """
# Test 15: Inserting a node with a key that is less than the right child's key
# causes a left rotation
# 5 7
# \ => / \
# 7 5 9
# \
# 9
# HINT: create a left_rotate method
# """
# avl_tree = five = AVLTree(5)
# seven = AVLTree(7)
# nine = AVLTree(9)
# avl_tree=avl_tree.insert(seven)
# avl_tree=avl_tree.insert(nine)
# self.assertEqual(seven, avl_tree)
# self.assertEqual(five, avl_tree.left)
# self.assertEqual(nine, avl_tree.right)
# def test_height_left_rotate_insert_two_smaller(self):
# """
# Test 16: Inserting a node with a key that is less than the right child's key
# causes a left rotation
# The height of each node is equal to the height of its largest child + 1
# 5 7
# \ => / \
# 7 5 9
# \
# 9
# """
# avl_tree = five = AVLTree(5)
# seven = AVLTree(7)
# nine = AVLTree(9)
# avl_tree=avl_tree.insert(seven)
# avl_tree=avl_tree.insert(nine)
# self.assertEqual(2, seven.height)
# self.assertEqual(1, five.height)
# self.assertEqual(1, nine.height)
# def test_balance_factor_left_rotate_insert_two_smaller(self):
# """
# Test 17: Inserting a node with a key that is less than the right child's key
# causes a left rotation
# The balance factor of each node is equal to the difference between the height
# of the left subtree and the right subtree
# 5 7
# \ => / \
# 7 5 9
# \
# 9
# """
# avl_tree = five = AVLTree(5)
# seven = AVLTree(7)
# nine = AVLTree(9)
# avl_tree=avl_tree.insert(seven)
# avl_tree=avl_tree.insert(nine)
# self.assertEqual(0, seven.balance_factor)
# self.assertEqual(0, five.balance_factor)
# self.assertEqual(0, nine.balance_factor)
# # """
# # Toddler, two-level trees.
# # Insertion
# # Left Subtree Right Imbalance. Left Rotation Followed By Right rotation
# # # """
# def test_left_right_rotate_insert_two_smaller(self):
# """
# Test 18: Inserting a node with a key that is greater than the left child's key
# causes a left rotation followed by a right rotation
# 5 5 4
# / => / => / \
# 3 4 3 5
# \ /
# 4 3
# HINT: left rotate around node 3 followed by right_rotate around node 5
# """
# avl_tree = five = AVLTree(5)
# three = AVLTree(3)
# four = AVLTree(4)
# avl_tree=avl_tree.insert(three)
# avl_tree=avl_tree.insert(four)
# self.assertEqual(four, avl_tree)
# self.assertEqual(three, avl_tree.left)
# self.assertEqual(five, avl_tree.right)
# def test_height_left_right_rotate_insert_two_smaller(self):
# """
# Test 19: Inserting a node with a key that is greater than the left child's key
# causes a left rotation followed by a right rotation
# The height of each node is equal to the height of its largest child + 1
# 5 5 4
# / => / => / \
# 3 4 3 5
# \ /
# 4 3
# """
# avl_tree = five = AVLTree(5)
# three = AVLTree(3)
# four = AVLTree(4)
# avl_tree=avl_tree.insert(three)
# avl_tree=avl_tree.insert(four)
# self.assertEqual(2, four.height)
# self.assertEqual(1, three.height)
# self.assertEqual(1, five.height)
# def test_balance_factor_left_right_rotate_insert_two_smaller(self):
# """
# Test 20: Inserting a node with a key that is greater than the left child's key
# causes a left rotation followed by a right rotation
# The balance factor of each node is equal to the difference between the height
# of the left subtree and the right subtree
# 5 5 4
# / => / => / \
# 3 4 3 5
# \ /
# 4 3
# """
# avl_tree = five = AVLTree(5)
# three = AVLTree(3)
# four = AVLTree(4)
# avl_tree=avl_tree.insert(three)
# avl_tree=avl_tree.insert(four)
# self.assertEqual(0, four.balance_factor)
# self.assertEqual(0, three.balance_factor)
# self.assertEqual(0, five.balance_factor)
# # """
# # Toddler, two-level trees.
# # Insertion
# # Right Subtree Left Imbalance. Right Rotation Followed By Left rotation
# # # """
# def test_right_left_rotate_insert_two_smaller(self):
# """
# Test 21: Inserting a node with a key that is less than the right child's key
# causes a right rotation followed by a left rotation
# 5 5 6
# \ => \ => / \
# 7 6 5 7
# / \
# 6 7
# HINT: right rotate around node 7 followed by left rotate around node 5
# """
# avl_tree = five = AVLTree(5)
# seven = AVLTree(7)
# six = AVLTree(6)
# avl_tree=avl_tree.insert(seven)
# avl_tree=avl_tree.insert(six)
# self.assertEqual(six, avl_tree)
# self.assertEqual(five, avl_tree.left)
# self.assertEqual(seven, avl_tree.right)
# def test_height_right_left_rotate_insert_two_smaller(self):
# """
# Test 22: Inserting a node with a key that is less than the right child's key
# causes a right rotation followed by a left rotation
# The height of each node is equal to the height of its largest child + 1
# 5 5 6
# \ => \ => / \
# 7 6 5 7
# / \
# 6 7
# """
# avl_tree = five = AVLTree(5)
# seven = AVLTree(7)
# six = AVLTree(6)
# avl_tree=avl_tree.insert(seven)
# avl_tree=avl_tree.insert(six)
# self.assertEqual(2, six.height)
# self.assertEqual(1, seven.height)
# self.assertEqual(1, five.height)
# def test_balance_factor_right_left_rotate_insert_two_smaller(self):
# """
# Test 23: Inserting a node with a key that is less than the right child's key
# causes a right rotation followed by a left rotation
# The balance factor of each node is equal to the difference between the height
# of the left subtree and the right subtree
# 5 5 6
# \ => \ => / \
# 7 6 5 7
# / \
# 6 7
# """
# avl_tree = five = AVLTree(5)
# seven = AVLTree(7)
# six = AVLTree(6)
# avl_tree=avl_tree.insert(seven)
# avl_tree=avl_tree.insert(six)
# self.assertEqual(0, six.balance_factor)
# self.assertEqual(0, seven.balance_factor)
# self.assertEqual(0, five.balance_factor)
# """
# Teen-age, three-level trees. (Depth of two.)
# Hint: Don't just curse - be recursive.
# """
# def test_three_level_tree_height(self):
# """
# Test 24: Height of each node is one bigger than tha max of its child's heights
# 10
# / \
# 5 15
# / \
# 2 7
# Hint: Recursion, if you didn't already, makes this easy.
# """
# avl_tree = AVLTree(10)
# avl_tree.insert(AVLTree(5))
# avl_tree.insert(AVLTree(15))
# avl_tree.insert(AVLTree(2))
# avl_tree.insert(AVLTree(7))
# self.assertEqual(3, avl_tree.height)
# self.assertEqual(2, avl_tree.left.height)
# self.assertEqual(1, avl_tree.left.left.height)
# self.assertEqual(1, avl_tree.left.right.height)
# self.assertEqual(1, avl_tree.right.height)
# def test_three_level_tree_balance_factor(self):
# """
# Test 25: Balance factor of each node is the difference between its heights
# 10
# / \
# 5 15
# / \
# 2 7
# Hint: Recursion, if you didn't already, makes this easy.
# """
# avl_tree = AVLTree(10)
# avl_tree.insert(AVLTree(5))
# avl_tree.insert(AVLTree(15))
# avl_tree.insert(AVLTree(2))
# avl_tree.insert(AVLTree(7))
# self.assertEqual(1, avl_tree.balance_factor)
# self.assertEqual(0, avl_tree.left.balance_factor)
# self.assertEqual(0, avl_tree.left.left.balance_factor)
# self.assertEqual(0, avl_tree.left.right.balance_factor)
# self.assertEqual(0, avl_tree.right.balance_factor)
# def test_insert_three_level_tree(self):
# """
# Test 26: Inserting a key results in a left imbalance and a right rotation
# 10 10 5
# / \ / \ / \
# 5 15 => 5 15 => 2 10
# / \ / \ / / \
# 2 7 2 7 1 7 15
# /
# 1
# Hint: Recursion, if you didn't already, makes this easy.
# """
# one=AVLTree(1)
# two=AVLTree(2)
# five=AVLTree(5)
# seven=AVLTree(7)
# ten=AVLTree(10)
# fifteen=AVLTree(15)
# avl_tree = ten
# avl_tree = avl_tree.insert(five)
# avl_tree = avl_tree.insert(fifteen)
# avl_tree = avl_tree.insert(two)
# avl_tree = avl_tree.insert(seven)
# avl_tree = avl_tree.insert(one)
# self.assertEqual(five, avl_tree)
# self.assertEqual(two, avl_tree.left)
# self.assertEqual(one, avl_tree.left.left)
# self.assertEqual(ten, avl_tree.right)
# self.assertEqual(seven, avl_tree.right.left)
# self.assertEqual(fifteen, avl_tree.right.right)
# def test_three_level_tree_height_right_heavy(self):
# """
# Test 27: Height of each node is one bigger than than max of its child's heights
# 10
# / \
# 5 15
# / \
# 12 20
# Hint: Recursion, if you didn't already, makes this easy.
# """
# avl_tree = AVLTree(10)
# avl_tree.insert(AVLTree(5))
# avl_tree.insert(AVLTree(15))
# avl_tree.insert(AVLTree(12))
# avl_tree.insert(AVLTree(20))
# self.assertEqual(3, avl_tree.height)
# self.assertEqual(1, avl_tree.left.height)
# self.assertEqual(2, avl_tree.right.height)
# self.assertEqual(1, avl_tree.right.left.height)
# self.assertEqual(1, avl_tree.right.right.height)
# def test_three_level_tree_right_heavy_balance_factor(self):
# """
# Test 28: Balance factor of each node is the difference between its children's heights
# 10
# / \
# 5 15
# / \
# 12 20
# Hint: Recursion, if you didn't already, makes this easy.
# """
# avl_tree = AVLTree(10)
# avl_tree.insert(AVLTree(5))
# avl_tree.insert(AVLTree(15))
# avl_tree.insert(AVLTree(12))
# avl_tree.insert(AVLTree(20))
# self.assertEqual(-1, avl_tree.balance_factor)
# self.assertEqual(0, avl_tree.left.balance_factor)
# self.assertEqual(0, avl_tree.right.balance_factor)
# self.assertEqual(0, avl_tree.right.left.balance_factor)
# self.assertEqual(0, avl_tree.right.right.balance_factor)
# def test_insert_three_level_tree_right_heavy(self):
# """
# Test 29: Inserting a key results in a left imbalance and a right rotation
# 10
# / \
# 5 15
# / \
# 12 20
# 10 10 15
# / \ / \ / \
# 5 15 => 5 15 => 10 20
# / \ / \ / \ \
# 12 20 12 20 5 12 25
# \
# 25
# Hint: Recursion, if you didn't already, makes this easy.
# """
# five=AVLTree(5)
# ten=AVLTree(10)
# twelve=AVLTree(12)
# fifteen=AVLTree(15)
# twenty=AVLTree(20)
# twentyfive=AVLTree(25)
# avl_tree = ten
# avl_tree = avl_tree.insert(five)
# avl_tree = avl_tree.insert(fifteen)
# avl_tree = avl_tree.insert(twelve)
# avl_tree = avl_tree.insert(twenty)
# avl_tree = avl_tree.insert(twentyfive)
# self.assertEqual(fifteen, avl_tree)
# self.assertEqual(ten, avl_tree.left)
# self.assertEqual(five, avl_tree.left.left)
# self.assertEqual(twelve, avl_tree.left.right)
# self.assertEqual(twenty, avl_tree.right)
# self.assertEqual(twentyfive, avl_tree.right.right)
if __name__ == '__main__':
unittest.main()