forked from AdaGold/tree-practice
-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathtest_binary_search_tree.py
239 lines (190 loc) · 5.13 KB
/
test_binary_search_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
import pytest
from binary_search_tree.tree import Tree
@pytest.fixture()
def empty_tree() -> Tree():
return Tree()
@pytest.fixture()
def tree_with_nodes(empty_tree) -> Tree():
empty_tree.add(5, "Peter")
empty_tree.add(3, "Paul")
empty_tree.add(1, "Mary")
empty_tree.add(10, "Karla")
empty_tree.add(15, "Ada")
empty_tree.add(25, "Kari")
return empty_tree
def test_find_returns_none_for_empty_tree(empty_tree):
assert empty_tree.find(5) == None
def test_can_find_single_root_node(empty_tree):
empty_tree.add(25, "Kari")
assert empty_tree.find(25) == "Kari"
def test_add_and_find(tree_with_nodes):
assert tree_with_nodes.find(5) == "Peter"
assert tree_with_nodes.find(15) == "Ada"
assert tree_with_nodes.find(3) == "Paul"
def test_can_find_leaf_nodes(tree_with_nodes):
assert tree_with_nodes.find(1) == "Mary"
assert tree_with_nodes.find(25) == "Kari"
tree_with_nodes.add(13, "Alicia")
assert tree_with_nodes.find(13) == "Alicia"
def test_find_returns_none_for_values_not_in_tree(tree_with_nodes):
assert tree_with_nodes.find(6) == None
def test_inorder_with_empty_tree(empty_tree):
answer = empty_tree.inorder()
assert empty_tree.inorder() == []
def test_inorder_with_nodes(tree_with_nodes):
expected_answer = [
{
"key": 1,
"value": "Mary"
},
{
"key": 3,
"value": "Paul"
},
{
"key": 5,
"value": "Peter"
},
{
"key": 10,
"value": "Karla"
},
{
"key": 15,
"value": "Ada"
},
{
"key": 25,
"value": "Kari"
}
]
answer = tree_with_nodes.inorder()
assert answer == expected_answer
def test_preorder_on_empty_tree(empty_tree):
assert empty_tree.preorder() == []
def test_preorder_on_tree_with_nodes(tree_with_nodes):
expected_answer = [
{
"key": 5,
"value": "Peter"
},
{
"key": 3,
"value": "Paul"
},
{
"key": 1,
"value": "Mary"
},
{
"key": 10,
"value": "Karla"
},
{
"key": 15,
"value": "Ada"
},
{
"key": 25,
"value": "Kari"
}
]
answer = tree_with_nodes.preorder()
assert answer == expected_answer
def test_postorder_on_empty_tree(empty_tree):
assert empty_tree.postorder() == []
def test_postorder_on_tree_with_nodes(tree_with_nodes):
expected_answer = [
{
"key": 1,
"value": "Mary"
},
{
"key": 3,
"value": "Paul"
},
{
"key": 25,
"value": "Kari"
},
{
"key": 15,
"value": "Ada"
},
{
"key": 10,
"value": "Karla"
},
{
"key": 5,
"value": "Peter"
}
]
answer = tree_with_nodes.postorder()
assert answer == expected_answer
def test_height_of_empty_tree_is_zero(empty_tree):
assert empty_tree.height() == 0
def test_height_of_one_node_tree(empty_tree):
empty_tree.add(5, "pasta")
assert empty_tree.height() == 1
def test_height_of_many_node_tree(tree_with_nodes):
assert tree_with_nodes.height() == 4
tree_with_nodes.add(2, "pasta")
tree_with_nodes.add(2.5, "bread")
assert tree_with_nodes.height() == 5
def test_will_report_height_of_balanced_tree():
balanced_tree = Tree()
balanced_tree.add(5, "Peter")
balanced_tree.add(3, "Paul")
balanced_tree.add(4, "Kate")
balanced_tree.add(1, "Mary")
balanced_tree.add(10, "Karla")
balanced_tree.add(8, "Ada")
balanced_tree.add(25, "Kari")
assert balanced_tree.height() == 3
def test_will_report_height_of_unbalanced_tree():
unbalanced_tree = Tree()
unbalanced_tree.add(100, "Peter")
unbalanced_tree.add(110, "Paul")
unbalanced_tree.add(120, "Kate")
unbalanced_tree.add(130, "Mary")
unbalanced_tree.add(140, "Karla")
assert unbalanced_tree.height() == 5
unbalanced_tree = Tree()
unbalanced_tree.add(140, "Peter")
unbalanced_tree.add(130, "Paul")
unbalanced_tree.add(120, "Kate")
unbalanced_tree.add(110, "Mary")
unbalanced_tree.add(100, "Karla")
assert unbalanced_tree.height() == 5
# def test_bfs_with_empty_tree(empty_tree):
# assert empty_tree.bfs() == []
# def test_bfs_with_tree_with_nodes(tree_with_nodes):
# expected_answer = [
# {
# "key": 5,
# "value": "Peter"
# },
# {
# "key": 3,
# "value": "Paul"
# },
# {
# "key": 10,
# "value": "Karla"
# },
# {
# "key": 1,
# "value": "Mary"
# },
# {
# "key": 15,
# "value": "Ada"
# },
# {
# "key": 25,
# "value": "Kari"
# }
# ]
# answer = tree_with_nodes.bfs()
# assert answer == expected_answer