Skip to content

Commit 55e459b

Browse files
committed
update/add doctests for BinarySearchTree class methods
1 parent 03a4251 commit 55e459b

File tree

1 file changed

+94
-11
lines changed

1 file changed

+94
-11
lines changed

data_structures/binary_tree/binary_search_tree_recursive.py

+94-11
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,14 @@ def empty(self) -> None:
3333
Empties the tree
3434
3535
>>> t = BinarySearchTree()
36-
>>> assert t.root is None
3736
>>> 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
3944
"""
4045
self.root = None
4146

@@ -49,6 +54,9 @@ def is_empty(self) -> bool:
4954
>>> t.put(8)
5055
>>> t.is_empty()
5156
False
57+
>>> t.remove(8)
58+
>>> t.is_empty()
59+
True
5260
"""
5361
return self.root is None
5462

@@ -58,16 +66,37 @@ def put(self, label: int) -> None:
5866
5967
>>> t = BinarySearchTree()
6068
>>> 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
6373
6474
>>> 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
6779
6880
>>> 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
71100
"""
72101
self.root = self._put(self.root, label)
73102

@@ -91,8 +120,16 @@ def search(self, label: int) -> Node:
91120
>>> t = BinarySearchTree()
92121
>>> t.put(8)
93122
>>> 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
96133
97134
>>> node = t.search(3)
98135
Traceback (most recent call last):
@@ -119,8 +156,43 @@ def remove(self, label: int) -> None:
119156
>>> t = BinarySearchTree()
120157
>>> t.put(8)
121158
>>> 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
122179
>>> 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
124196
125197
>>> t.remove(3)
126198
Traceback (most recent call last):
@@ -144,6 +216,9 @@ def remove(self, label: int) -> None:
144216
self._reassign_nodes(node, None)
145217

146218
def _reassign_nodes(self, node: Node, new_children: Node | None) -> None:
219+
"""
220+
Reassigns 'new_children' in place of 'node' in tree.
221+
"""
147222
if new_children:
148223
new_children.parent = node.parent
149224

@@ -156,6 +231,10 @@ def _reassign_nodes(self, node: Node, new_children: Node | None) -> None:
156231
self.root = new_children
157232

158233
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+
"""
159238
if node.left:
160239
lowest_node = self._get_lowest_node(node.left)
161240
else:
@@ -221,6 +300,10 @@ def get_min_label(self) -> int:
221300
>>> t.put(10)
222301
>>> t.get_min_label()
223302
8
303+
>>> t.put(5)
304+
>>> t.put(3)
305+
>>> t.get_min_label()
306+
3
224307
"""
225308
if self.root is None:
226309
raise ValueError("Binary search tree is empty")

0 commit comments

Comments
 (0)