-
Notifications
You must be signed in to change notification settings - Fork 169
/
test_tree.py
900 lines (790 loc) · 26.6 KB
/
test_tree.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
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
from __future__ import absolute_import, unicode_literals
import copy
import random
import pytest
from binarytree import Node, build, tree, bst, heap
from binarytree import get_parent_node
from binarytree.exceptions import (
NodeValueError,
NodeIndexError,
NodeTypeError,
NodeModifyError,
NodeNotFoundError,
TreeHeightError,
NodeReferenceError,
)
from tests.utils import (
builtin_print,
pprint_default,
pprint_with_index
)
REPETITIONS = 20
@pytest.mark.order1
def test_node_set_attributes():
root = Node(1)
assert root.left is None
assert root.right is None
assert root.value == 1
assert repr(root) == 'Node(1)'
left_child = Node(2)
root.left = left_child
assert root.left is left_child
assert root.right is None
assert root.value == 1
assert root.left.left is None
assert root.left.right is None
assert root.left.value == 2
assert repr(left_child) == 'Node(2)'
right_child = Node(3)
root.right = right_child
assert root.left is left_child
assert root.right is right_child
assert root.value == 1
assert root.right.left is None
assert root.right.right is None
assert root.right.value == 3
assert repr(right_child) == 'Node(3)'
last_node = Node(4)
left_child.right = last_node
assert root.left.right is last_node
assert root.left.right.value == 4
assert repr(last_node) == 'Node(4)'
with pytest.raises(NodeValueError) as err:
# noinspection PyTypeChecker
Node('this_is_not_an_integer')
assert str(err.value) == 'node value must be a number'
with pytest.raises(NodeTypeError) as err:
# noinspection PyTypeChecker
Node(1, 'this_is_not_a_node')
assert str(err.value) == 'left child must be a Node instance'
with pytest.raises(NodeTypeError) as err:
# noinspection PyTypeChecker
Node(1, Node(1), 'this_is_not_a_node')
assert str(err.value) == 'right child must be a Node instance'
with pytest.raises(NodeValueError) as err:
root.value = 'this_is_not_an_integer'
assert root.value == 1
assert str(err.value) == 'node value must be a number'
with pytest.raises(NodeTypeError) as err:
root.left = 'this_is_not_a_node'
assert root.left is left_child
assert str(err.value) == 'left child must be a Node instance'
with pytest.raises(NodeTypeError) as err:
root.right = 'this_is_not_a_node'
assert root.right is right_child
assert str(err.value) == 'right child must be a Node instance'
@pytest.mark.order2
def test_tree_build():
root = build([])
assert root is None
root = build([1])
assert root.value == 1
assert root.left is None
assert root.right is None
root = build([1, 2])
assert root.value == 1
assert root.left.value == 2
assert root.right is None
root = build([1, 2, 3])
assert root.value == 1
assert root.left.value == 2
assert root.right.value == 3
assert root.left.left is None
assert root.left.right is None
assert root.right.left is None
assert root.right.right is None
root = build([1, 2, 3, None, 4])
assert root.value == 1
assert root.left.value == 2
assert root.right.value == 3
assert root.left.left is None
assert root.left.right.value == 4
assert root.right.left is None
assert root.right.right is None
assert root.left.right.left is None
assert root.left.right.right is None
with pytest.raises(NodeNotFoundError) as err:
build([None, 1, 2])
assert str(err.value) == 'parent node missing at index 0'
with pytest.raises(NodeNotFoundError) as err:
build([1, None, 2, 3, 4])
assert str(err.value) == 'parent node missing at index 1'
@pytest.mark.order3
def test_tree_get_node():
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
root.left.right.left = Node(6)
assert root[0] is root
assert root[1] is root.left
assert root[2] is root.right
assert root[3] is root.left.left
assert root[4] is root.left.right
assert root[9] is root.left.right.left
for index in [5, 6, 7, 8, 10]:
with pytest.raises(NodeNotFoundError) as err:
assert root[index]
assert str(err.value) == 'node missing at index {}'.format(index)
with pytest.raises(NodeIndexError) as err:
assert root[-1]
assert str(err.value) == 'node index must be a non-negative int'
@pytest.mark.order4
def test_tree_set_node():
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
root.left.right.left = Node(6)
new_node_1 = Node(7)
new_node_2 = Node(8)
new_node_3 = Node(9)
with pytest.raises(NodeModifyError) as err:
root[0] = new_node_1
assert str(err.value) == 'cannot modify the root node'
with pytest.raises(NodeIndexError) as err:
root[-1] = new_node_1
assert str(err.value) == 'node index must be a non-negative int'
with pytest.raises(NodeNotFoundError) as err:
root[100] = new_node_1
assert str(err.value) == 'parent node missing at index 49'
root[10] = new_node_1
assert root.value == 1
assert root.left.value == 2
assert root.right.value == 3
assert root.left.left.value == 4
assert root.left.right.value == 5
assert root.left.right.left.value == 6
assert root.left.right.right is new_node_1
root[4] = new_node_2
assert root.value == 1
assert root.left.value == 2
assert root.right.value == 3
assert root.left.left.value == 4
assert root.left.right.value == 8
assert root.left.right.left is None
assert root.left.right.right is None
root[1] = new_node_3
root[2] = new_node_2
assert root.left is new_node_3
assert root.right is new_node_2
@pytest.mark.order5
def test_tree_del_node():
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
root.left.right.left = Node(6)
with pytest.raises(NodeModifyError) as err:
del root[0]
assert str(err.value) == 'cannot delete the root node'
with pytest.raises(NodeIndexError) as err:
del root[-1]
assert str(err.value) == 'node index must be a non-negative int'
with pytest.raises(NodeNotFoundError) as err:
del root[10]
assert str(err.value) == 'no node to delete at index 10'
with pytest.raises(NodeNotFoundError) as err:
del root[100]
assert str(err.value) == 'no node to delete at index 100'
del root[3]
assert root.left.left is None
assert root.left.value == 2
assert root.left.right.value == 5
assert root.left.right.right is None
assert root.left.right.left.value == 6
assert root.left.right.left.left is None
assert root.left.right.left.right is None
assert root.right.value == 3
assert root.right.left is None
assert root.right.right is None
assert root.size == 5
del root[2]
assert root.left.left is None
assert root.left.value == 2
assert root.left.right.value == 5
assert root.left.right.right is None
assert root.left.right.left.value == 6
assert root.left.right.left.left is None
assert root.left.right.left.right is None
assert root.right is None
assert root.size == 4
del root[4]
assert root.left.left is None
assert root.left.right is None
assert root.right is None
assert root.size == 2
del root[1]
assert root.left is None
assert root.right is None
assert root.size == 1
@pytest.mark.order6
def test_tree_print_no_index():
for printer in [builtin_print, pprint_default]:
lines = printer([1])
assert lines == ['1']
lines = printer([1, 2])
assert lines == [' 1',
' /',
'2']
lines = printer([1, None, 3])
assert lines == ['1',
' \\',
' 3']
lines = printer([1, 2, 3])
assert lines == [' 1',
' / \\',
'2 3']
lines = printer([1, 2, 3, None, 5])
assert lines == [' __1',
' / \\',
'2 3',
' \\',
' 5']
lines = printer([1, 2, 3, None, 5, 6])
assert lines == [' __1__',
' / \\',
'2 3',
' \\ /',
' 5 6']
lines = printer([1, 2, 3, None, 5, 6, 7])
assert lines == [' __1__',
' / \\',
'2 3',
' \\ / \\',
' 5 6 7']
lines = printer([1, 2, 3, 8, 5, 6, 7])
assert lines == [' __1__',
' / \\',
' 2 3',
' / \\ / \\',
'8 5 6 7']
@pytest.mark.order7
def test_tree_print_with_index():
lines = pprint_with_index([1])
assert lines == ['0:1']
lines = pprint_with_index([1, 2])
assert lines == [' _0:1',
' /',
'1:2']
lines = pprint_with_index([1, None, 3])
assert lines == ['0:1_',
' \\',
' 2:3']
lines = pprint_with_index([1, 2, 3])
assert lines == [' _0:1_',
' / \\',
'1:2 2:3']
lines = pprint_with_index([1, 2, 3, None, 5])
assert lines == [' _____0:1_',
' / \\',
'1:2_ 2:3',
' \\',
' 4:5']
lines = pprint_with_index([1, 2, 3, None, 5, 6])
assert lines == [' _____0:1_____',
' / \\',
'1:2_ _2:3',
' \\ /',
' 4:5 5:6']
lines = pprint_with_index([1, 2, 3, None, 5, 6, 7])
assert lines == [' _____0:1_____',
' / \\',
'1:2_ _2:3_',
' \\ / \\',
' 4:5 5:6 6:7']
lines = pprint_with_index([1, 2, 3, 8, 5, 6, 7])
assert lines == [' _____0:1_____',
' / \\',
' _1:2_ _2:3_',
' / \\ / \\',
'3:8 4:5 5:6 6:7']
@pytest.mark.order8
def test_tree_validate():
class TestNode(Node):
def __setattr__(self, attr, value):
object.__setattr__(self, attr, value)
root = Node(1)
root.validate() # Should pass
root = Node(1)
root.left = Node(2)
root.validate() # Should pass
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.validate() # Should pass
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
root.left.right.left = Node(6)
root.validate() # Should pass
root = TestNode(1)
root.left = 'not_a_node'
with pytest.raises(NodeTypeError) as err:
root.validate()
assert str(err.value) == 'invalid node instance at index 1'
root = TestNode(1)
root.right = TestNode(2)
root.right.value = 'not_an_integer'
with pytest.raises(NodeValueError) as err:
root.validate()
assert str(err.value) == 'invalid node value at index 2'
root = TestNode(1)
root.left = TestNode(2)
root.left.right = root
with pytest.raises(NodeReferenceError) as err:
root.validate()
assert str(err.value) == 'cyclic node reference at index 4'
@pytest.mark.order9
def test_tree_properties():
root = Node(1)
assert root.properties == {
'height': 0,
'is_balanced': True,
'is_bst': True,
'is_complete': True,
'is_max_heap': True,
'is_min_heap': True,
'is_perfect': True,
'is_strict': True,
'is_symmetric': True,
'leaf_count': 1,
'max_leaf_depth': 0,
'max_node_value': 1,
'min_leaf_depth': 0,
'min_node_value': 1,
'size': 1
}
assert root.height == 0
assert root.is_balanced is True
assert root.is_bst is True
assert root.is_complete is True
assert root.is_max_heap is True
assert root.is_min_heap is True
assert root.is_perfect is True
assert root.is_strict is True
assert root.is_symmetric is True
assert root.leaf_count == 1
assert root.max_leaf_depth == 0
assert root.max_node_value == 1
assert root.min_leaf_depth == 0
assert root.min_node_value == 1
assert root.size == len(root) == 1
root.left = Node(2)
assert root.properties == {
'height': 1,
'is_balanced': True,
'is_bst': False,
'is_complete': True,
'is_max_heap': False,
'is_min_heap': True,
'is_perfect': False,
'is_strict': False,
'is_symmetric': False,
'leaf_count': 1,
'max_leaf_depth': 1,
'max_node_value': 2,
'min_leaf_depth': 1,
'min_node_value': 1,
'size': 2
}
assert root.height == 1
assert root.is_balanced is True
assert root.is_bst is False
assert root.is_complete is True
assert root.is_max_heap is False
assert root.is_min_heap is True
assert root.is_perfect is False
assert root.is_strict is False
assert root.is_symmetric is False
assert root.leaf_count == 1
assert root.max_leaf_depth == 1
assert root.max_node_value == 2
assert root.min_leaf_depth == 1
assert root.min_node_value == 1
assert root.size == len(root) == 2
root.right = Node(3)
assert root.properties == {
'height': 1,
'is_balanced': True,
'is_bst': False,
'is_complete': True,
'is_max_heap': False,
'is_min_heap': True,
'is_perfect': True,
'is_strict': True,
'is_symmetric': False,
'leaf_count': 2,
'max_leaf_depth': 1,
'max_node_value': 3,
'min_leaf_depth': 1,
'min_node_value': 1,
'size': 3
}
assert root.height == 1
assert root.is_balanced is True
assert root.is_bst is False
assert root.is_complete is True
assert root.is_max_heap is False
assert root.is_min_heap is True
assert root.is_perfect is True
assert root.is_strict is True
assert root.is_symmetric is False
assert root.leaf_count == 2
assert root.max_leaf_depth == 1
assert root.max_node_value == 3
assert root.min_leaf_depth == 1
assert root.min_node_value == 1
assert root.size == len(root) == 3
root.left.left = Node(4)
assert root.properties == {
'height': 2,
'is_balanced': True,
'is_bst': False,
'is_complete': True,
'is_max_heap': False,
'is_min_heap': True,
'is_perfect': False,
'is_strict': False,
'is_symmetric': False,
'leaf_count': 2,
'max_leaf_depth': 2,
'max_node_value': 4,
'min_leaf_depth': 1,
'min_node_value': 1,
'size': 4
}
assert root.height == 2
assert root.is_balanced is True
assert root.is_bst is False
assert root.is_complete is True
assert root.is_max_heap is False
assert root.is_min_heap is True
assert root.is_perfect is False
assert root.is_strict is False
assert root.is_symmetric is False
assert root.leaf_count == 2
assert root.max_leaf_depth == 2
assert root.max_node_value == 4
assert root.min_leaf_depth == 1
assert root.min_node_value == 1
assert root.size == len(root) == 4
root.right.left = Node(5)
assert root.properties == {
'height': 2,
'is_balanced': True,
'is_bst': False,
'is_complete': False,
'is_max_heap': False,
'is_min_heap': False,
'is_perfect': False,
'is_strict': False,
'is_symmetric': False,
'leaf_count': 2,
'max_leaf_depth': 2,
'max_node_value': 5,
'min_leaf_depth': 2,
'min_node_value': 1,
'size': 5
}
assert root.height == 2
assert root.is_balanced is True
assert root.is_bst is False
assert root.is_complete is False
assert root.is_max_heap is False
assert root.is_min_heap is False
assert root.is_perfect is False
assert root.is_strict is False
assert root.is_symmetric is False
assert root.leaf_count == 2
assert root.max_leaf_depth == 2
assert root.max_node_value == 5
assert root.min_leaf_depth == 2
assert root.min_node_value == 1
assert root.size == len(root) == 5
root.right.left.left = Node(6)
assert root.properties == {
'height': 3,
'is_balanced': False,
'is_bst': False,
'is_complete': False,
'is_max_heap': False,
'is_min_heap': False,
'is_perfect': False,
'is_strict': False,
'is_symmetric': False,
'leaf_count': 2,
'max_leaf_depth': 3,
'max_node_value': 6,
'min_leaf_depth': 2,
'min_node_value': 1,
'size': 6
}
assert root.height == 3
assert root.is_balanced is False
assert root.is_bst is False
assert root.is_complete is False
assert root.is_max_heap is False
assert root.is_min_heap is False
assert root.is_perfect is False
assert root.is_strict is False
assert root.is_symmetric is False
assert root.leaf_count == 2
assert root.max_leaf_depth == 3
assert root.max_node_value == 6
assert root.min_leaf_depth == 2
assert root.min_node_value == 1
assert root.size == len(root) == 6
root.left.left.left = Node(7)
assert root.properties == {
'height': 3,
'is_balanced': False,
'is_bst': False,
'is_complete': False,
'is_max_heap': False,
'is_min_heap': False,
'is_perfect': False,
'is_strict': False,
'is_symmetric': False,
'leaf_count': 2,
'max_leaf_depth': 3,
'max_node_value': 7,
'min_leaf_depth': 3,
'min_node_value': 1,
'size': 7
}
assert root.height == 3
assert root.is_balanced is False
assert root.is_bst is False
assert root.is_complete is False
assert root.is_max_heap is False
assert root.is_min_heap is False
assert root.is_perfect is False
assert root.is_strict is False
assert root.is_symmetric is False
assert root.leaf_count == 2
assert root.max_leaf_depth == 3
assert root.max_node_value == 7
assert root.min_leaf_depth == 3
assert root.min_node_value == 1
assert root.size == len(root) == 7
@pytest.mark.order10
def test_tree_traversal():
n1 = Node(1)
assert n1.levels == [[n1]]
assert n1.leaves == [n1]
assert n1.inorder == [n1]
assert n1.preorder == [n1]
assert n1.postorder == [n1]
assert n1.levelorder == [n1]
n2 = Node(2)
n1.left = n2
assert n1.levels == [[n1], [n2]]
assert n1.leaves == [n2]
assert n1.inorder == [n2, n1]
assert n1.preorder == [n1, n2]
assert n1.postorder == [n2, n1]
assert n1.levelorder == [n1, n2]
n3 = Node(3)
n1.right = n3
assert n1.levels == [[n1], [n2, n3]]
assert n1.leaves == [n2, n3]
assert n1.inorder == [n2, n1, n3]
assert n1.preorder == [n1, n2, n3]
assert n1.postorder == [n2, n3, n1]
assert n1.levelorder == [n1, n2, n3]
n4 = Node(4)
n5 = Node(5)
n2.left = n4
n2.right = n5
assert n1.levels == [[n1], [n2, n3], [n4, n5]]
assert n1.leaves == [n3, n4, n5]
assert n1.inorder == [n4, n2, n5, n1, n3]
assert n1.preorder == [n1, n2, n4, n5, n3]
assert n1.postorder == [n4, n5, n2, n3, n1]
assert n1.levelorder == [n1, n2, n3, n4, n5]
def test_tree_list_representation():
root = Node(1)
assert root.values == [1]
root.right = Node(3)
assert root.values == [1, None, 3]
root.left = Node(2)
assert root.values == [1, 2, 3]
root.right.left = Node(4)
assert root.values == [1, 2, 3, None, None, 4]
root.right.right = Node(5)
assert root.values == [1, 2, 3, None, None, 4, 5]
root.left.left = Node(6)
assert root.values == [1, 2, 3, 6, None, 4, 5]
root.left.right = Node(7)
assert root.values == [1, 2, 3, 6, 7, 4, 5]
@pytest.mark.order11
def test_tree_generation():
for invalid_height in ['foo', -1, None]:
with pytest.raises(TreeHeightError) as err:
tree(height=invalid_height)
assert str(err.value) == 'height must be an int between 0 - 9'
root = tree(height=0)
root.validate()
assert root.height == 0
assert root.left is None
assert root.right is None
assert isinstance(root.value, int)
for _ in range(REPETITIONS):
random_height = random.randint(1, 9)
root = tree(random_height)
root.validate()
assert root.height == random_height
for _ in range(REPETITIONS):
random_height = random.randint(1, 9)
root = tree(random_height, is_perfect=True)
root.validate()
assert root.height == random_height
assert root.is_perfect is True
assert root.is_balanced is True
assert root.is_strict is True
@pytest.mark.order12
def test_bst_generation():
for invalid_height in ['foo', -1, None]:
with pytest.raises(TreeHeightError) as err:
bst(height=invalid_height)
assert str(err.value) == 'height must be an int between 0 - 9'
root = bst(height=0)
root.validate()
assert root.height == 0
assert root.left is None
assert root.right is None
assert isinstance(root.value, int)
for _ in range(REPETITIONS):
random_height = random.randint(1, 9)
root = bst(random_height)
root.validate()
assert root.is_bst is True
assert root.height == random_height
for _ in range(REPETITIONS):
random_height = random.randint(1, 9)
root = bst(random_height, is_perfect=True)
root.validate()
assert root.height == random_height
if not root.is_bst:
print(root)
raise Exception('boo')
assert root.is_bst is True
assert root.is_perfect is True
assert root.is_balanced is True
assert root.is_strict is True
@pytest.mark.order13
def test_heap_generation():
for invalid_height in ['foo', -1, None]:
with pytest.raises(TreeHeightError) as err:
heap(height=invalid_height)
assert str(err.value) == 'height must be an int between 0 - 9'
root = heap(height=0)
root.validate()
assert root.height == 0
assert root.left is None
assert root.right is None
assert isinstance(root.value, int)
for _ in range(REPETITIONS):
random_height = random.randint(1, 9)
root = heap(random_height, is_max=True)
root.validate()
assert root.is_max_heap is True
assert root.is_min_heap is False
assert root.height == random_height
for _ in range(REPETITIONS):
random_height = random.randint(1, 9)
root = heap(random_height, is_max=False)
root.validate()
assert root.is_max_heap is False
assert root.is_min_heap is True
assert root.height == random_height
for _ in range(REPETITIONS):
random_height = random.randint(1, 9)
root = heap(random_height, is_perfect=True)
root.validate()
assert root.is_max_heap is True
assert root.is_min_heap is False
assert root.is_perfect is True
assert root.is_balanced is True
assert root.is_strict is True
assert root.height == random_height
@pytest.mark.order13
def test_heap_float_values():
root = Node(1.0)
root.left = Node(0.5)
root.right = Node(1.5)
assert root.height == 1
assert root.is_balanced is True
assert root.is_bst is True
assert root.is_complete is True
assert root.is_max_heap is False
assert root.is_min_heap is False
assert root.is_perfect is True
assert root.is_strict is True
assert root.leaf_count == 2
assert root.max_leaf_depth == 1
assert root.max_node_value == 1.5
assert root.min_leaf_depth == 1
assert root.min_node_value == 0.5
assert root.size == 3
for printer in [builtin_print, pprint_default]:
lines = printer([1.0])
assert lines == ['1.0']
lines = printer([1.0, 2.0])
assert lines == [' _1.0',
' /',
'2.0']
lines = printer([1.0, None, 3.0])
assert lines == ['1.0_',
' \\',
' 3.0']
lines = printer([1.0, 2.0, 3.0])
assert lines == [' _1.0_',
' / \\',
'2.0 3.0']
lines = printer([1.0, 2.0, 3.0, None, 5.0])
assert lines == [' _____1.0_',
' / \\',
'2.0_ 3.0',
' \\',
' 5.0']
for builder in [tree, bst, heap]:
for _ in range(REPETITIONS):
root = builder()
root_copy = copy.deepcopy(root)
for node in root:
node.value += 0.1
assert root.height == root_copy.height
assert root.is_balanced == root_copy.is_balanced
assert root.is_bst == root_copy.is_bst
assert root.is_complete == root_copy.is_complete
assert root.is_max_heap == root_copy.is_max_heap
assert root.is_min_heap == root_copy.is_min_heap
assert root.is_perfect == root_copy.is_perfect
assert root.is_strict == root_copy.is_strict
assert root.is_symmetric == root_copy.is_symmetric
assert root.leaf_count == root_copy.leaf_count
assert root.max_leaf_depth == root_copy.max_leaf_depth
assert root.max_node_value == root_copy.max_node_value + 0.1
assert root.min_leaf_depth == root_copy.min_leaf_depth
assert root.min_node_value == root_copy.min_node_value + 0.1
assert root.size == root_copy.size
@pytest.mark.order14
def test_get_parent_node():
root = Node(0)
root.left = Node(1)
root.right = Node(2)
root.left.left = Node(3)
root.right.right = Node(4)
assert get_parent_node(root, root.left.left) == root.left
assert get_parent_node(root, root.left) == root
assert get_parent_node(root, root) is None
assert get_parent_node(root, root.right.right) == root.right
assert get_parent_node(root, root.right) == root
assert get_parent_node(root, Node(5)) is None