-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_bst.py
More file actions
946 lines (865 loc) · 33.7 KB
/
test_bst.py
File metadata and controls
946 lines (865 loc) · 33.7 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
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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
# DO NOT MODIFY THIS FILE
# Run me via: python3 -m unittest test_bst
import unittest
import time
from bst import BinarySearchTree
class TestBinarySearchTree(unittest.TestCase):
"""
Initialization
"""
def test_instantiation(self):
"""
A BinarySearchTree exists.
"""
try:
BinarySearchTree()
except NameError:
self.fail("Could not instantiate BinarySearchTree.")
# def test_initial_attributes(self):
# """
# A BST is a recursive structure. When we refer to an object that "is a BST,"
# we are referring to a root node of a binary search tree.
# Every node has a left child, right child, and a key.
# A new BST has a left, right and key that are each None.
# Hint: Define an initializer.
# """
# bst = BinarySearchTree()
# self.assertIsNone(bst.left)
# self.assertIsNone(bst.right)
# self.assertIsNone(bst.key)
# def test_instatiate_with_key(self):
# """
# A BST (node) can be instantiated with an initial key.
# When one isn't provided, the key is None.
# Hint: Use Python's 'default arguments' feature.
# """
# bst = BinarySearchTree()
# self.assertIsNone(bst.key)
# bst = BinarySearchTree(42)
# self.assertEqual(42, bst.key)
# """
# Cute, single-level trees. (Depth of zero.)
# """
# def test_insert_single_smaller(self):
# """
# 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.)
# """
# bst = BinarySearchTree(5)
# child = BinarySearchTree(1)
# bst.insert(child)
# self.assertEqual(child, bst.left)
# def test_insert_single_equal(self):
# """
# 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.)
# """
# bst = BinarySearchTree(5)
# child = BinarySearchTree(5)
# bst.insert(child)
# self.assertEqual(child, bst.left)
# def test_insert_single_greater(self):
# """
# 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.)
# """
# bst = BinarySearchTree(5)
# child = BinarySearchTree(7)
# bst.insert(child)
# self.assertEqual(child, bst.right)
# def test_search_single_none(self):
# """
# Searching a single-level tree for a key that doesn't exist returns None.
# """
# bst = BinarySearchTree(5)
# self.assertIsNone(bst.search(-999))
# def test_search_single_one(self):
# """
# Searching a single-level tree for a key that does exist returns that node / tree.
# """
# bst = BinarySearchTree(5)
# self.assertEqual(bst, bst.search(5))
# def test_delete_single_nonexistent(self):
# """
# Deleting a node with a key that does not exist returns the root node of
# the single-level tree.
# """
# bst = BinarySearchTree(5)
# self.assertEqual(bst, bst.delete(-999))
# def test_delete_single(self):
# """
# Deleting the node of a single-level tree returns None.
# """
# bst = BinarySearchTree(5)
# self.assertIsNone(bst.delete(5))
# """
# Toddler, two-level trees. (Depth of one.)
# """
# def test_insert_two_smaller_left(self):
# """
# 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.
# 5 5
# / \ => / \
# 3 7 3 7
# /
# 1
# Hint: Nest your logic. Delegate with recursion.
# """
# bst = BinarySearchTree(5)
# bst.left = BinarySearchTree(3)
# bst.right = BinarySearchTree(7)
# child = BinarySearchTree(1)
# bst.insert(child)
# self.assertEqual(child, bst.left.left)
# def test_insert_two_greater_left(self):
# """
# Inserting a node with a key that is greater than the left child's key
# (and less than the parent/root) appends the new node as the left child's
# right child.
# 5 5
# / \ => / \
# 3 7 3 7
# \
# 4
# Hint: If this test immediately passes, you are on a happy path.
# """
# bst = BinarySearchTree(5)
# bst.left = BinarySearchTree(3)
# bst.right = BinarySearchTree(7)
# child = BinarySearchTree(4)
# bst.insert(child)
# self.assertEqual(child, bst.left.right)
# def test_insert_two_greater_right(self):
# """
# Inserting a node with a key that is greater than the right child's key
# appends the new node as the right child's right child.
# 5 5
# / \ => / \
# 3 7 3 7
# \
# 9
# """
# bst = BinarySearchTree(5)
# bst.left = BinarySearchTree(3)
# bst.right = BinarySearchTree(7)
# child = BinarySearchTree(9)
# bst.insert(child)
# self.assertEqual(child, bst.right.right)
# def test_insert_two_smaller_right(self):
# """
# Inserting a node with a key that is less than the right child's key
# (and greater than the parent/root) appends the new node as the right
# child's left child.
# 5 5
# / \ => / \
# 3 7 3 7
# /
# 6
# """
# bst = BinarySearchTree(5)
# bst.left = BinarySearchTree(3)
# bst.right = BinarySearchTree(7)
# child = BinarySearchTree(6)
# bst.insert(child)
# self.assertEqual(child, bst.right.left)
# def test_search_two_basics(self):
# """
# Searching a two-level tree for a key that doesn't exist returns None.
# """
# bst = BinarySearchTree(5)
# bst.left = BinarySearchTree(3)
# bst.right = BinarySearchTree(7)
# self.assertIsNone(bst.search(-999))
# def test_search_two_root(self):
# """
# Searching a two-level tree for a key that exists in the root returns
# the root.
# """
# bst = BinarySearchTree(5)
# bst.left = BinarySearchTree(3)
# bst.right = BinarySearchTree(7)
# self.assertEqual(bst, bst.search(5))
# def test_search_two_left(self):
# """
# Searching a two-level tree for a key that exists in the left subtree
# returns that left node / subtree.
# Hint: Try a brutish, 'naive' approach.
# """
# bst = BinarySearchTree(5)
# left = BinarySearchTree(3)
# right = BinarySearchTree(7)
# bst.left = left
# bst.right = right
# self.assertEqual(left, bst.search(3))
# def test_search_two_right(self):
# """
# Searching a two-level tree for a key that exists in the right subtree
# returns that right node / subtree.
# """
# bst = BinarySearchTree(5)
# left = BinarySearchTree(3)
# right = BinarySearchTree(7)
# bst.left = left
# bst.right = right
# self.assertEqual(right, bst.search(7))
# def test_delete_two_nonexistent(self):
# """
# Deleting a node with a key that does not exist does not modify the tree
# and returns the root node of the tree.
# """
# bst = BinarySearchTree(5)
# bst.left = BinarySearchTree(3)
# bst.right = BinarySearchTree(7)
# self.assertEqual(bst, bst.delete(-999))
# self.assertEqual(5, bst.key)
# self.assertEqual(3, bst.left.key)
# self.assertEqual(7, bst.right.key)
# def test_delete_two_left_leaf(self):
# """
# Deleting the left child of a two-level tree removes the left child and
# returns the root node.
# 5 5
# / \ => \
# 3 7 7
# Hint: Consult the BST rules. Time to improve your `delete` method... a little.
# """
# bst = BinarySearchTree(5)
# bst.left = BinarySearchTree(3)
# bst.right = BinarySearchTree(7)
# self.assertEqual(bst, bst.delete(3))
# self.assertIsNone(bst.left)
# self.assertEqual(5, bst.key)
# self.assertEqual(7, bst.right.key)
# def test_delete_two_right_leaf(self):
# """
# Deleting the right child of a two-level tree removes the right child and
# returns the root node.
# 5 5
# / \ => /
# 3 7 3
# Hint: Small changes to `delete`, lest you overthink it.
# """
# bst = BinarySearchTree(5)
# bst.left = BinarySearchTree(3)
# bst.right = BinarySearchTree(7)
# self.assertEqual(bst, bst.delete(7))
# self.assertIsNone(bst.right)
# self.assertEqual(5, bst.key)
# self.assertEqual(3, bst.left.key)
# def test_delete_two_root_with_left(self):
# """
# Deleting the root of a two-level tree that has only a left child makes
# the left child the new root, and `delete` returns it.
# 5
# / => 3
# 3
# Hint: Small steps of a little nested logic.
# """
# bst = BinarySearchTree(5)
# initial_left_child = BinarySearchTree(3)
# bst.left = initial_left_child
# bst = bst.delete(5)
# self.assertEqual(initial_left_child, bst)
# self.assertEqual(3, bst.key)
# self.assertTrue(bst.is_leaf())
# def test_delete_two_root_with_right(self):
# """
# Deleting the root of a two-level tree that has only a right child makes
# the right child the new root, and `delete` returns it.
# 5
# \ => 7
# 7
# """
# bst = BinarySearchTree(5)
# initial_right_child = BinarySearchTree(7)
# bst.right = initial_right_child
# bst = bst.delete(5)
# self.assertEqual(initial_right_child, bst)
# self.assertEqual(7, bst.key)
# self.assertTrue(bst.is_leaf())
# def test_delete_two_root(self):
# """
# Deleting the root of a two-level tree promotes the right child to be the
# new root, and `delete` returns it.
# 5 7
# / \ => /
# 3 7 3
# Hint: Consult the bst deletion rules... but be direct for now.
# """
# bst = BinarySearchTree(5)
# left = BinarySearchTree(3)
# initial_right_child = BinarySearchTree(7)
# bst.left = left
# bst.right = initial_right_child
# bst = bst.delete(5)
# self.assertEqual(initial_right_child, bst)
# self.assertEqual(3, bst.left.key)
# self.assertIsNone(bst.right)
# """
# Teen-age, three-level trees. (Depth of two.)
# Hint: Don't just curse - be recursive.
# """
# def test_insert_three_smaller_leftmost_leaf(self):
# """
# Inserting a node with a key that is less than the leftmost leaf node's
# key appends the new node as the leftmost leaf's left child.
# 10 10
# / \ / \
# 5 15 => 5 15
# / \ / \ / \ / \
# 2 7 12 17 2 7 12 17
# /
# 1
# Hint: Recursion, if you didn't already, makes this easy.
# """
# bst = BinarySearchTree(10)
# bst.left = BinarySearchTree(5)
# bst.right = BinarySearchTree(15)
# bst.left.left = BinarySearchTree(2)
# bst.left.right = BinarySearchTree(7)
# bst.right.left = BinarySearchTree(12)
# bst.right.right = BinarySearchTree(17)
# child = BinarySearchTree(1)
# bst.insert(child)
# self.assertEqual(child, bst.left.left.left)
# def test_insert_three_larger_leftmost_leaf(self):
# """
# Inserting a node with a key that is greater than the leftmost leaf node's
# key (but less than it's parents') appends the new node as the leftmost
# leaf's right child.
# 10 10
# / \ / \
# 5 15 => 5 15
# / \ / \ / \ / \
# 2 7 12 17 2 7 12 17
# \
# 3
# """
# bst = three_level_tree() # Same tree as pictured above to the left.
# child = BinarySearchTree(3)
# bst.insert(child)
# self.assertEqual(child, bst.left.left.right)
# def test_insert_three_smaller_left_right_leaf(self):
# """
# Inserting a node with a key that is less than the 'inner left' leaf node's
# key (but greater than it's parent and less than the root) appends the new
# node as the 'inner left' leaf's left child.
# 10 10
# / \ / \
# 5 15 => 5 15
# / \ / \ / \ / \
# 2 7 12 17 2 7 12 17
# /
# 6
# """
# bst = three_level_tree()
# child = BinarySearchTree(6)
# bst.insert(child)
# self.assertEqual(child, bst.left.right.left)
# def test_insert_three_larger_left_right_leaf(self):
# """
# Inserting a node with a key that is greater than the 'inner left' leaf node's
# key (but less than the root) appends the new node as the 'inner left' leaf's
# right child.
# 10 10
# / \ / \
# 5 15 => 5 15
# / \ / \ / \ / \
# 2 7 12 17 2 7 12 17
# \
# 8
# """
# bst = three_level_tree()
# child = BinarySearchTree(8)
# bst.insert(child)
# self.assertEqual(child, bst.left.right.right)
# def test_insert_three_smaller_right_left_leaf(self):
# """
# Inserting a node with a key that is less than the 'inner right' leaf node's
# key (and less than it's parent, but greater than the root) appends the
# new node as the 'inner right' leaf's left child.
# 10 10
# / \ / \
# 5 15 => 5 15
# / \ / \ / \ / \
# 2 7 12 17 2 7 12 17
# /
# 11
# """
# bst = three_level_tree()
# child = BinarySearchTree(11)
# bst.insert(child)
# self.assertEqual(child, bst.right.left.left)
# def test_insert_three_larger_right_left_leaf(self):
# """
# Inserting a node with a key that is greater than the 'inner right' leaf
# node's key (but less than it's parent) appends the new node as the
# 'inner right' leaf's right child.
# 10 10
# / \ / \
# 5 15 => 5 15
# / \ / \ / \ / \
# 2 7 12 17 2 7 12 17
# \
# 13
# """
# bst = three_level_tree()
# child = BinarySearchTree(13)
# bst.insert(child)
# self.assertEqual(child, bst.right.left.right)
# def test_insert_three_smaller_right_right_leaf(self):
# """
# Inserting a node with a key that is less than the rightmost leaf node's
# key (but greater than it's parent) appends the new node as the rightmost
# leaf's left child.
# 10 10
# / \ / \
# 5 15 => 5 15
# / \ / \ / \ / \
# 2 7 12 17 2 7 12 17
# /
# 16
# """
# bst = three_level_tree()
# child = BinarySearchTree(16)
# bst.insert(child)
# self.assertEqual(child, bst.right.right.left)
# def test_insert_three_greater_right_right_leaf(self):
# """
# Inserting a node with a key that is less than the rightmost leaf node's
# key (but greater than it's parent) appends the new node as the rightmost
# leaf's left child.
# 10 10
# / \ / \
# 5 15 => 5 15
# / \ / \ / \ / \
# 2 7 12 17 2 7 12 17
# \
# 19
# """
# bst = three_level_tree()
# child = BinarySearchTree(19)
# bst.insert(child)
# self.assertEqual(child, bst.right.right.right)
# def test_search_three_not_found(self):
# """
# Searching for a non-existent key in a three-level tree returns None.
# """
# bst = three_level_tree()
# self.assertIsNone(bst.search(-999))
# def test_search_three_root(self):
# """
# Searching a three-level tree for a key that exists in the root returns
# the root.
# """
# bst = three_level_tree()
# self.assertEqual(bst, bst.search(10))
# def test_search_three_outer_left(self):
# """
# Searching a three-level tree for a key that exists in the leftmost leaf
# returns that leftmost leaf.
# Hint: Be recursive.
# """
# bst = three_level_tree()
# self.assertEqual(bst.left.left, bst.search(2))
# def test_search_three_inner_left(self):
# """
# Searching a three-level tree for a key that exists in the inner left leaf
# returns that inner left leaf.
# """
# bst = three_level_tree()
# self.assertEqual(bst.left.right, bst.search(7))
# def test_search_three_inner_right(self):
# """
# Searching a three-level tree for a key that exists in the inner right leaf
# returns that inner right leaf.
# """
# bst = three_level_tree()
# self.assertEqual(bst.right.left, bst.search(12))
# def test_search_three_outer_right(self):
# """
# Searching a three-level tree for a key that exists in the rightmost leaf
# returns that rightmost leaf.
# """
# bst = three_level_tree()
# self.assertEqual(bst.right.right, bst.search(17))
# """
# Before proper deletions, let's add some convenience methods.
# """
# def test_is_leaf(self):
# """
# A node without children is a leaf node.
# """
# bst = BinarySearchTree(10)
# self.assertTrue(bst.is_leaf())
# def test_with_left_child_is_not_leaf(self):
# """
# A node with a left child is not a leaf node.
# """
# bst = BinarySearchTree(10)
# bst.left = BinarySearchTree(5)
# self.assertFalse(bst.is_leaf())
# def test_with_right_child_is_not_leaf(self):
# """
# A node with a right child is not a leaf node.
# """
# bst = BinarySearchTree(10)
# bst.right = BinarySearchTree(15)
# self.assertFalse(bst.is_leaf())
# def test_has_left_child(self):
# """
# A node with a left child returns True.
# """
# bst = BinarySearchTree(10)
# bst.left = BinarySearchTree(5)
# self.assertTrue(bst.has_left_child())
# def test_not_has_left_child(self):
# """
# A node without a left child returns False.
# """
# bst = BinarySearchTree(10)
# self.assertFalse(bst.has_left_child())
# def test_has_right_child(self):
# """
# A node with a right child returns True.
# """
# bst = BinarySearchTree(10)
# bst.right = BinarySearchTree(15)
# self.assertTrue(bst.has_right_child())
# def test_not_has_right_child(self):
# """
# A node without a right child returns False.
# """
# bst = BinarySearchTree(10)
# self.assertFalse(bst.has_right_child())
# def test_find_minimum_one(self):
# """
# A tree's 'minimum' node is the one with the smallest key.
# """
# bst = BinarySearchTree(10)
# self.assertEqual(bst, bst.minimum())
# def test_find_minimum_two(self):
# """
# A tree's 'minimum' node is the one with the smallest key.
# """
# bst = BinarySearchTree(10)
# left = BinarySearchTree(5)
# right = BinarySearchTree(15)
# bst.left = left
# bst.right = right
# self.assertEqual(left, bst.minimum())
# def test_find_minimum_three(self):
# """
# A tree's 'minimum' node is the one with the smallest key.
# Hint: The recipe is simple. And recursive.
# """
# bst = three_level_tree()
# self.assertEqual(bst.left.left, bst.minimum())
# # If all of your tests are passing, try refactoring your implementation by
# # using those new convenience methods. One change at a time, keeping the
# # tests passing.
# def test_delete_three_nonexistent(self):
# """
# Deleting a node with a key that does not exist does not modify the tree
# and returns the root node of a three-level tree.
# 10 10
# / \ / \
# 5 15 => 5 15
# / \ / \ / \ / \
# 2 7 12 17 2 7 12 17
# """
# bst = three_level_tree()
# self.assertEqual(bst, bst.delete(-999))
# self.assertEqual(10, bst.key)
# self.assertEqual(5, bst.left.key)
# self.assertEqual(15, bst.right.key)
# self.assertEqual(2, bst.left.left.key)
# self.assertEqual(7, bst.left.right.key)
# self.assertEqual(12, bst.right.left.key)
# self.assertEqual(17, bst.right.right.key)
# def test_delete_three_leftmost_leaf(self):
# """
# Deleting the leftmost leaf of a three-level tree removes the leftmost leaf
# and returns the root node of the three-level tree.
# 10 10
# / \ / \
# 5 15 => 5 15
# / \ / \ \ / \
# 2 7 12 17 7 12 17
# Hint: Consult the BST rules. Use paper. Draw pictures, hand-write code.
# Be recursive.
# """
# bst = three_level_tree()
# self.assertEqual(bst, bst.delete(2))
# self.assertIsNone(bst.left.left)
# self.assertEqual(10, bst.key)
# self.assertEqual(5, bst.left.key)
# self.assertEqual(15, bst.right.key)
# self.assertEqual(7, bst.left.right.key)
# self.assertEqual(12, bst.right.left.key)
# self.assertEqual(17, bst.right.right.key)
# def test_delete_three_inner_left_leaf(self):
# """
# Deleting the 'inner left' leaf of a three-level tree removes the inner
# left leaf and returns the root node of the three-level tree.
# 10 10
# / \ / \
# 5 15 => 5 15
# / \ / \ / / \
# 2 7 12 17 2 12 17
# """
# bst = three_level_tree()
# self.assertEqual(bst, bst.delete(7))
# self.assertIsNone(bst.left.right)
# self.assertEqual(10, bst.key)
# self.assertEqual(5, bst.left.key)
# self.assertEqual(15, bst.right.key)
# self.assertEqual(2, bst.left.left.key)
# self.assertEqual(12, bst.right.left.key)
# self.assertEqual(17, bst.right.right.key)
# def test_delete_three_inner_right_leaf(self):
# """
# Deleting the 'inner right' leaf of a three-level tree removes the inner
# right leaf and returns the root node of the three-level tree.
# 10 10
# / \ / \
# 5 15 => 5 15
# / \ / \ / \ \
# 2 7 12 17 2 7 17
# """
# bst = three_level_tree()
# self.assertEqual(bst, bst.delete(12))
# self.assertIsNone(bst.right.left)
# self.assertEqual(10, bst.key)
# self.assertEqual(5, bst.left.key)
# self.assertEqual(15, bst.right.key)
# self.assertEqual(2, bst.left.left.key)
# self.assertEqual(7, bst.left.right.key)
# self.assertEqual(17, bst.right.right.key)
# def test_delete_three_rightmost_leaf(self):
# """
# Deleting the rightmost leaf of a three-level tree removes the rightmost
# leaf and returns the root node of the three-level tree.
# 10 10
# / \ / \
# 5 15 => 5 15
# / \ / \ / \ /
# 2 7 12 17 2 7 12
# """
# bst = three_level_tree()
# self.assertEqual(bst, bst.delete(17))
# self.assertIsNone(bst.right.right)
# self.assertEqual(10, bst.key)
# self.assertEqual(5, bst.left.key)
# self.assertEqual(15, bst.right.key)
# self.assertEqual(2, bst.left.left.key)
# self.assertEqual(7, bst.left.right.key)
# self.assertEqual(12, bst.right.left.key)
# def test_delete_three_left(self):
# """
# Deleting a node with two children causes the leaf with the smallest key
# in the deleted node's right subtree to take its place; and, delete still
# returns the root of the tree.
# 10 10
# / \ / \
# 5 15 => 7 15
# / \ / \ / / \
# 2 7 12 17 2 12 17
# """
# bst = three_level_tree()
# self.assertEqual(bst, bst.delete(5))
# self.assertEqual(7, bst.left.key)
# self.assertIsNone(bst.left.right)
# self.assertEqual(10, bst.key)
# self.assertEqual(15, bst.right.key)
# self.assertEqual(2, bst.left.left.key)
# self.assertEqual(12, bst.right.left.key)
# self.assertEqual(17, bst.right.right.key)
# def test_delete_three_right(self):
# """
# Deleting a node with two children causes the leaf with the smallest key
# in the deleted node's right subtree to take its place; and, delete still
# returns the root of the tree.
# 10 10
# / \ / \
# 5 15 => 5 17
# / \ / \ / \ /
# 2 7 12 17 2 7 12
# """
# bst = three_level_tree()
# self.assertEqual(bst, bst.delete(15))
# self.assertEqual(17, bst.right.key)
# self.assertIsNone(bst.right.right)
# self.assertEqual(10, bst.key)
# self.assertEqual(5, bst.left.key)
# self.assertEqual(2, bst.left.left.key)
# self.assertEqual(7, bst.left.right.key)
# self.assertEqual(12, bst.right.left.key)
# def test_delete_three_root(self):
# """
# Deleting a node with two children causes the leaf with the smallest key
# in the deleted node's right subtree to take its place; and, delete still
# returns the root of the tree.
# 10 12
# / \ / \
# 5 15 => 5 15
# / \ / \ / \ \
# 2 7 12 17 2 7 17
# """
# bst = three_level_tree()
# bst = bst.delete(10)
# self.assertEqual(12, bst.key)
# self.assertEqual(5, bst.left.key)
# self.assertEqual(15, bst.right.key)
# self.assertEqual(2, bst.left.left.key)
# self.assertEqual(7, bst.left.right.key)
# self.assertIsNone(bst.right.left)
# self.assertEqual(17, bst.right.right.key)
# """
# Mature, N-level trees.
# """
# def test_insertions(self):
# """
# 10 10 10
# / \ / \ / \
# 5 15 => 5 15 => 5 15
# / \ / \ / \ / \ / \ / \
# 2 7 12 17 2 7 12 17 2 7 12 30
# / \ / \
# 16 45 16 45
# / \ \
# 30 99 99
# """
# bst = three_level_tree()
# bst.insert(BinarySearchTree(45))
# bst.insert(BinarySearchTree(16))
# bst.insert(BinarySearchTree(30))
# bst.insert(BinarySearchTree(99))
# self.assertEqual(45, bst.right.right.right.key)
# self.assertEqual(99, bst.right.right.right.right.key)
# bst.delete(17)
# self.assertEqual(30, bst.right.right.key)
# self.assertEqual(16, bst.right.right.left.key)
# self.assertEqual(45, bst.right.right.right.key)
# self.assertEqual(99, bst.right.right.right.right.key)
# self.assertIsNone(bst.right.right.right.left)
# """
# Traversals
# """
# def test_one_pre_order(self):
# """
# The pre-order traversal of a single-node tree is a list containing that
# node's key.
# """
# bst = BinarySearchTree(10)
# self.assertEqual([10], bst.keys('pre'))
# def test_one_in_order(self):
# """
# The in-order traversal of a single-node tree is a list containing that
# node's key.
# """
# bst = BinarySearchTree(5)
# self.assertEqual([5], bst.keys('in'))
# def test_one_post_order(self):
# """
# The post-order traversal of a single-node tree is a list containing that
# node's key.
# """
# bst = BinarySearchTree(3)
# self.assertEqual([3], bst.keys('post'))
# def test_two_pre_order(self):
# """
# The pre-order traversal of a two-level tree is a list containing the keys
# in 'pre-order'.
# 5
# / \ => [5, 3, 7]
# 3 7
# """
# bst = BinarySearchTree(5)
# bst.left = BinarySearchTree(3)
# bst.right = BinarySearchTree(7)
# self.assertEqual([5, 3, 7], bst.keys('pre'))
# def test_two_in_order(self):
# """
# The in-order traversal of a two-level tree is a list containing the keys
# in 'in-order'.
# 5
# / \ => [3, 5, 7]
# 3 7
# """
# bst = BinarySearchTree(5)
# bst.left = BinarySearchTree(3)
# bst.right = BinarySearchTree(7)
# self.assertEqual([3, 5, 7], bst.keys('in'))
# def test_two_post_order(self):
# """
# The post-order traversal of a two-level tree is a list containing the keys
# in 'post-order'.
# 5
# / \ => [3, 7, 5]
# 3 7
# """
# bst = BinarySearchTree(5)
# bst.left = BinarySearchTree(3)
# bst.right = BinarySearchTree(7)
# self.assertEqual([3, 7, 5], bst.keys('post'))
# def test_three_pre_order(self):
# """
# The pre-order traversal of a three-level tree is a list containing the keys
# in 'pre-order'.
# 10
# / \
# 5 15 => [10, 5, 2, 7, 15, 12, 17]
# / \ / \
# 2 7 12 17
# """
# bst = three_level_tree()
# self.assertEqual([10, 5, 2, 7, 15, 12, 17], bst.keys('pre'))
# def test_three_in_order(self):
# """
# The in-order traversal of a three-level tree is a list containing the keys
# in 'in-order'.
# 10
# / \
# 5 15 => [2, 5, 7, 10, 12, 15, 17]
# / \ / \
# 2 7 12 17
# """
# bst = three_level_tree()
# self.assertEqual([2, 5, 7, 10, 12, 15, 17], bst.keys('in'))
# def test_three_post_order(self):
# """
# The post-order traversal of a three-level tree is a list containing the keys
# in 'post-order'.
# 10
# / \
# 5 15 => [2, 7, 5, 12, 17, 15, 10]
# / \ / \
# 2 7 12 17
# """
# bst = three_level_tree()
# self.assertEqual([2, 7, 5, 12, 17, 15, 10], bst.keys('post'))
# def three_level_tree():
# """
# 10
# / \
# 5 15
# / \ / \
# 2 7 12 17
# """
# bst = BinarySearchTree(10)
# bst.left = BinarySearchTree(5)
# bst.right = BinarySearchTree(15)
# bst.left.left = BinarySearchTree(2)
# bst.left.right = BinarySearchTree(7)
# bst.right.left = BinarySearchTree(12)
# bst.right.right = BinarySearchTree(17)
# return bst
def fake_value():
return f"FAKE {time.time()}"
if __name__ == '__main__':
unittest.main()