-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_tree_breadth_first.py
More file actions
92 lines (70 loc) · 2.22 KB
/
Copy pathtest_tree_breadth_first.py
File metadata and controls
92 lines (70 loc) · 2.22 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
import pytest
from data_structures.binary_tree import BinaryTree, Node
from code_challenges.tree_breadth_first import breadth_first
# @pytest.mark.skip("TODO")
def test_exists():
assert breadth_first
# @pytest.mark.skip("TODO")
def test_rootless_tree():
tree = BinaryTree()
expected = []
actual = breadth_first(tree)
assert actual == expected
# @pytest.mark.skip("TODO")
def test_single_node():
tree = BinaryTree()
tree.root = Node("apples")
expected = ["apples"]
actual = breadth_first(tree)
assert actual == expected
# @pytest.mark.skip("TODO")
def test_two_nodes():
tree = BinaryTree()
tree.root = Node("apples")
tree.root.right = Node("bananas")
expected = ["apples", "bananas"]
actual = breadth_first(tree)
assert actual == expected
# @pytest.mark.skip("TODO")
def test_four_nodes():
tree = BinaryTree()
tree.root = Node("apples")
tree.root.left = Node("bananas")
tree.root.right = Node("cucumbers")
tree.root.right.right = Node("dates")
expected = ["apples", "bananas", "cucumbers", "dates"]
actual = breadth_first(tree)
assert actual == expected
# @pytest.mark.skip("TODO")
def test_example_from_reading():
"""
We build these out by hand because the example has some gaps
i.e. it is not added to left-to-right
2
7 5
2 6 9
5 11 4
result = [2,7,5,2,6,9,5,11,4]
"""
tree = BinaryTree()
level_0 = Node(2)
level_1_first = Node(7)
level_1_second = Node(5)
level_2_first = Node(2)
level_2_second = Node(6)
level_2_third = Node(9)
level_3_first = Node(5)
level_3_second = Node(11)
level_3_third = Node(4)
tree.root = level_0
level_0.left = level_1_first
level_0.right = level_1_second
level_1_first.left = level_2_first
level_1_first.right = level_2_second
level_1_second.right = level_2_third
level_2_second.left = level_3_first
level_2_second.right = level_3_second
level_2_third.right = level_3_third
expected = [2, 7, 5, 2, 6, 9, 5, 11, 4]
actual = breadth_first(tree)
assert actual == expected