@@ -33,9 +33,14 @@ def empty(self) -> None:
33
33
Empties the tree
34
34
35
35
>>> t = BinarySearchTree()
36
- >>> assert t.root is None
37
36
>>> t.put(8)
38
- >>> assert t.root is not None
37
+ >>> t.put(10)
38
+ >>> t.put(5)
39
+ >>> t.root is None
40
+ False
41
+ >>> t.empty()
42
+ >>> t.root is None
43
+ True
39
44
"""
40
45
self .root = None
41
46
@@ -49,6 +54,9 @@ def is_empty(self) -> bool:
49
54
>>> t.put(8)
50
55
>>> t.is_empty()
51
56
False
57
+ >>> t.remove(8)
58
+ >>> t.is_empty()
59
+ True
52
60
"""
53
61
return self .root is None
54
62
@@ -58,16 +66,37 @@ def put(self, label: int) -> None:
58
66
59
67
>>> t = BinarySearchTree()
60
68
>>> t.put(8)
61
- >>> assert t.root.parent is None
62
- >>> assert t.root.label == 8
69
+ >>> t.root.parent is None
70
+ True
71
+ >>> t.root.label == 8
72
+ True
63
73
64
74
>>> t.put(10)
65
- >>> assert t.root.right.parent == t.root
66
- >>> assert t.root.right.label == 10
75
+ >>> t.root.right.parent == t.root
76
+ True
77
+ >>> t.root.right.label == 10
78
+ True
67
79
68
80
>>> t.put(3)
69
- >>> assert t.root.left.parent == t.root
70
- >>> assert t.root.left.label == 3
81
+ >>> t.root.left.parent == t.root
82
+ True
83
+ >>> t.root.left.label == 3
84
+ True
85
+
86
+ >>> t.put(5)
87
+ >>> t.root.left.right.parent == t.root.left
88
+ True
89
+ >>> t.root.left.right.label == 5
90
+ True
91
+
92
+ >>> inorder = [node.label for node in t.inorder_traversal()]
93
+ >>> inorder == sorted(inorder)
94
+ True
95
+
96
+ >>> t.put(5)
97
+ Traceback (most recent call last):
98
+ ...
99
+ ValueError: Node with label 5 already exists
71
100
"""
72
101
self .root = self ._put (self .root , label )
73
102
@@ -91,8 +120,16 @@ def search(self, label: int) -> Node:
91
120
>>> t = BinarySearchTree()
92
121
>>> t.put(8)
93
122
>>> t.put(10)
94
- >>> node = t.search(8)
95
- >>> assert node.label == 8
123
+ >>> t.put(5)
124
+ >>> t.put(7)
125
+ >>> t.search(8).label
126
+ 8
127
+ >>> t.search(5).label
128
+ 5
129
+ >>> t.search(7).label
130
+ 7
131
+ >>> t.search(10).label
132
+ 10
96
133
97
134
>>> node = t.search(3)
98
135
Traceback (most recent call last):
@@ -119,8 +156,43 @@ def remove(self, label: int) -> None:
119
156
>>> t = BinarySearchTree()
120
157
>>> t.put(8)
121
158
>>> t.put(10)
159
+ >>> t.put(5)
160
+ >>> t.put(3)
161
+ >>> t.put(7)
162
+ >>> t.put(9)
163
+ >>> t.put(15)
164
+
165
+ >>> t.exists(3)
166
+ True
167
+ >>> t.remove(3)
168
+ >>> t.exists(3)
169
+ False
170
+
171
+ >>> t.exists(5)
172
+ True
173
+ >>> t.remove(5)
174
+ >>> t.exists(5), t.exists(7)
175
+ (False, True)
176
+
177
+ >>> t.exists(8)
178
+ True
122
179
>>> t.remove(8)
123
- >>> assert t.root.label == 10
180
+ >>> t.exists(8), t.exists(7), t.exists(10)
181
+ (False, True, True)
182
+ >>> t.root.label
183
+ 9
184
+
185
+ >>> t.remove(10)
186
+ >>> t.remove(15)
187
+ >>> t.remove(9)
188
+ >>> t.exists(9)
189
+ False
190
+ >>> t.root.label
191
+ 7
192
+
193
+ >>> t.remove(7)
194
+ >>> t.root == None
195
+ True
124
196
125
197
>>> t.remove(3)
126
198
Traceback (most recent call last):
@@ -144,6 +216,9 @@ def remove(self, label: int) -> None:
144
216
self ._reassign_nodes (node , None )
145
217
146
218
def _reassign_nodes (self , node : Node , new_children : Node | None ) -> None :
219
+ """
220
+ Reassigns 'new_children' in place of 'node' in tree.
221
+ """
147
222
if new_children :
148
223
new_children .parent = node .parent
149
224
@@ -156,6 +231,10 @@ def _reassign_nodes(self, node: Node, new_children: Node | None) -> None:
156
231
self .root = new_children
157
232
158
233
def _get_lowest_node (self , node : Node ) -> Node :
234
+ """
235
+ Removes node with minimum label from tree, reassigning
236
+ children if necessary and returns it.
237
+ """
159
238
if node .left :
160
239
lowest_node = self ._get_lowest_node (node .left )
161
240
else :
@@ -221,6 +300,10 @@ def get_min_label(self) -> int:
221
300
>>> t.put(10)
222
301
>>> t.get_min_label()
223
302
8
303
+ >>> t.put(5)
304
+ >>> t.put(3)
305
+ >>> t.get_min_label()
306
+ 3
224
307
"""
225
308
if self .root is None :
226
309
raise ValueError ("Binary search tree is empty" )
0 commit comments