-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_bst.py
311 lines (279 loc) · 8.65 KB
/
test_bst.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
# Run me with `python -m unittest test_bst`
import unittest
from bst import BinarySearchTree
class TestTree(unittest.TestCase):
"""
Initialization
"""
def test_instantiation(self):
"""
A BinarySearchTree exists.
"""
try:
BinarySearchTree()
except NameError:
self.fail("Could not instantiate BinarySearchTree.")
def test_instantiation_with_value(self):
"""
A BST can be instantiated with a value.
"""
fake_value = "FAKE"
bst = BinarySearchTree(fake_value)
self.assertEqual(fake_value, bst.value)
def test_has_left_right_and_parent_initially_none(self):
"""
BST has left, right, and prev values that start out as None.
"""
bst = BinarySearchTree()
self.assertEqual(None, bst.left)
self.assertEqual(None, bst.right)
self.assertEqual(None, bst.parent)
"""
Insertion
"""
def test_insert_smaller_values_as_left(self):
"""
Child nodes smaller than the root are inserted on the left.
"""
bst = BinarySearchTree(100)
child = BinarySearchTree(50)
bst.insert(child)
self.assertEqual(child, bst.left)
def test_insert_larger_values_as_right(self):
"""
Child nodes larger than the root are inserted on the right.
"""
bst = BinarySearchTree(100)
child = BinarySearchTree(150)
bst.insert(child)
self.assertEqual(child, bst.right)
def test_insert_many_small_large_and_equal_values_in_correct_locations(self):
"""
Many inserted nodes all end up in correct locations.
"""
bst = BinarySearchTree(100)
small_child_1 = BinarySearchTree(20)
small_child_2 = BinarySearchTree(30)
small_child_3 = BinarySearchTree(10)
equal_child = BinarySearchTree(100)
large_child_1 = BinarySearchTree(110)
large_child_2 = BinarySearchTree(130)
large_child_3 = BinarySearchTree(120)
bst.insert(small_child_1)
bst.insert(small_child_2)
bst.insert(small_child_3)
bst.insert(equal_child)
bst.insert(large_child_1)
bst.insert(large_child_2)
bst.insert(large_child_3)
self.assertEqual(small_child_1, bst.left)
self.assertEqual(small_child_2, small_child_1.right)
self.assertEqual(small_child_3, small_child_1.left)
self.assertEqual(equal_child, small_child_2.right)
self.assertEqual(large_child_1, bst.right)
self.assertEqual(large_child_2, large_child_1.right)
self.assertEqual(large_child_3, large_child_2.left)
def test_insertee_has_parent_matching_previous_node(self):
"""
When inserted, a node's parent value becomes the node before it.
"""
bst = BinarySearchTree(100)
child_1 = BinarySearchTree(20)
child_2 = BinarySearchTree(30)
child_3 = BinarySearchTree(10)
bst.insert(child_1)
bst.insert(child_2)
bst.insert(child_3)
self.assertEqual(bst, child_1.parent)
self.assertEqual(child_1, child_2.parent)
self.assertEqual(child_1, child_3.parent)
"""
Finding
"""
def test_find_root_value_returns_root(self):
"""
Using find for root's value returns the root.
"""
bst = BinarySearchTree(100)
self.assertEqual(bst, bst.find(100))
def test_find_smallest_leaf_node(self):
"""
The smallest leaf node can be found.
"""
bst = BinarySearchTree(100)
child_1 = BinarySearchTree(20)
child_2 = BinarySearchTree(10)
child_3 = BinarySearchTree(150)
bst.insert(child_1)
bst.insert(child_2)
bst.insert(child_3)
self.assertEqual(child_2, bst.find(10))
def test_find_largest_leaf_node(self):
"""
The largest leaf node can be found.
"""
bst = BinarySearchTree(100)
child_1 = BinarySearchTree(110)
child_2 = BinarySearchTree(120)
child_3 = BinarySearchTree(50)
bst.insert(child_1)
bst.insert(child_2)
bst.insert(child_3)
self.assertEqual(child_2, bst.find(120))
def test_find_node_at_complex_location(self):
"""
Nodes can be found in complicated locations.
"""
bst = BinarySearchTree(100)
child_1 = BinarySearchTree(10)
child_2 = BinarySearchTree(130)
child_3 = BinarySearchTree(110)
child_4 = BinarySearchTree(120)
bst.insert(child_1)
bst.insert(child_2)
bst.insert(child_3)
bst.insert(child_4)
self.assertEqual(child_3, bst.find(110))
def test_find_value_without_node(self):
"""
None is returned when no node has the correct value.
"""
bst = BinarySearchTree(100)
self.assertEqual(None, bst.find(50))
self.assertEqual(None, bst.find(150))
"""
Traversal
"""
def test_pre_order_traversal(self):
"""
Pre-order traversals return lists of BST values in pre-order.
"""
bst = BinarySearchTree(100)
small_child_1 = BinarySearchTree(20)
small_child_2 = BinarySearchTree(30)
small_child_3 = BinarySearchTree(10)
large_child_1 = BinarySearchTree(110)
large_child_2 = BinarySearchTree(130)
large_child_3 = BinarySearchTree(120)
bst.insert(small_child_1)
bst.insert(small_child_2)
bst.insert(small_child_3)
bst.insert(large_child_1)
bst.insert(large_child_2)
bst.insert(large_child_3)
self.assertEqual([100, 20, 10, 30, 110, 130, 120], bst.traverse_pre_order())
def test_in_order_traversal(self):
"""
In-order traversals return lists of BST values in-order.
"""
bst = BinarySearchTree(100)
small_child_1 = BinarySearchTree(20)
small_child_2 = BinarySearchTree(30)
small_child_3 = BinarySearchTree(10)
large_child_1 = BinarySearchTree(110)
large_child_2 = BinarySearchTree(130)
large_child_3 = BinarySearchTree(120)
bst.insert(small_child_1)
bst.insert(small_child_2)
bst.insert(small_child_3)
bst.insert(large_child_1)
bst.insert(large_child_2)
bst.insert(large_child_3)
self.assertEqual([10, 20, 30, 100, 110, 120, 130], bst.traverse_in_order())
def test_post_order_traversal(self):
"""
Post-order traversals return lists of BST values in post-order.
"""
bst = BinarySearchTree(100)
small_child_1 = BinarySearchTree(20)
small_child_2 = BinarySearchTree(30)
small_child_3 = BinarySearchTree(10)
large_child_1 = BinarySearchTree(110)
large_child_2 = BinarySearchTree(130)
large_child_3 = BinarySearchTree(120)
bst.insert(small_child_1)
bst.insert(small_child_2)
bst.insert(small_child_3)
bst.insert(large_child_1)
bst.insert(large_child_2)
bst.insert(large_child_3)
self.assertEqual([10, 30, 20, 120, 130, 110, 100], bst.traverse_post_order())
"""
Deletion
"""
def test_delete_leaf_node(self):
"""
Delete removes leaf nodes correctly.
"""
bst = BinarySearchTree(100)
small_child_1 = BinarySearchTree(20)
small_child_2 = BinarySearchTree(10)
equal_child = BinarySearchTree(100)
large_child = BinarySearchTree(150)
bst.insert(small_child_1)
bst.insert(small_child_2)
bst.insert(equal_child)
bst.insert(large_child)
bst.delete(small_child_2)
bst.delete(equal_child)
bst.delete(large_child)
self.assertEqual(small_child_1, bst.left)
self.assertEqual(None, small_child_1.left)
self.assertEqual(None, small_child_1.right)
self.assertEqual(None, bst.right)
def test_delete_node_with_single_child(self):
"""
Delete removes nodes with single children correctly.
"""
bst = BinarySearchTree(100)
small_child = BinarySearchTree(50)
equal_child = BinarySearchTree(100)
large_child_1 = BinarySearchTree(110)
large_child_2 = BinarySearchTree(120)
bst.insert(small_child)
bst.insert(equal_child)
bst.insert(large_child_1)
bst.insert(large_child_2)
bst.delete(small_child)
bst.delete(large_child_1)
self.assertEqual(equal_child, bst.left)
self.assertEqual(bst, equal_child.parent)
self.assertEqual(large_child_2, bst.right)
self.assertEqual(bst, large_child_2.parent)
def test_delete_node_with_two_children(self):
"""
Delete removes nodes with two children correctly.
"""
bst = BinarySearchTree(100)
small_child_1 = BinarySearchTree(20)
small_child_2 = BinarySearchTree(10)
small_child_3 = BinarySearchTree(30)
large_child_1 = BinarySearchTree(120)
large_child_2 = BinarySearchTree(110)
large_child_3 = BinarySearchTree(130)
large_child_4 = BinarySearchTree(122)
large_child_5 = BinarySearchTree(123)
bst.insert(small_child_1)
bst.insert(small_child_2)
bst.insert(small_child_3)
bst.insert(large_child_1)
bst.insert(large_child_2)
bst.insert(large_child_3)
bst.insert(large_child_4)
bst.insert(large_child_5)
bst.delete(small_child_1)
bst.delete(large_child_1)
self.assertEqual(bst, small_child_3.parent)
self.assertEqual(small_child_3, bst.left)
self.assertEqual(small_child_3, small_child_2.parent)
self.assertEqual(small_child_2, small_child_3.left)
self.assertEqual(bst, large_child_4.parent)
self.assertEqual(large_child_4, bst.right)
self.assertEqual(large_child_4, large_child_2.parent)
self.assertEqual(large_child_2, large_child_4.left)
self.assertEqual(large_child_4, large_child_3.parent)
self.assertEqual(large_child_3, large_child_4.right)
self.assertEqual(large_child_3, large_child_5.parent)
self.assertEqual(large_child_5, large_child_3.left)
if __name__ == '__main__':
unittest.main()