Skip to content

Commit 8510e1f

Browse files
committed
all tests to doctest
1 parent 9294347 commit 8510e1f

File tree

1 file changed

+28
-29
lines changed

1 file changed

+28
-29
lines changed

Diff for: data_structures/binary_tree/binary_search_tree.py

+28-29
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,34 @@
2020
Traceback (most recent call last):
2121
...
2222
IndexError: Warning: Tree is empty! please use another.
23+
24+
Other example:
25+
26+
>>> testlist = (8, 3, 6, 1, 10, 14, 13, 4, 7)
27+
>>> t = BinarySearchTree()
28+
>>> for i in testlist:
29+
... t.insert(i)
30+
31+
Prints all the elements of the list in order traversal
32+
>>> print(t)
33+
{'8': ({'3': (1, {'6': (4, 7)})}, {'10': (None, {'14': (13, None)})})}
34+
35+
Test existence
36+
>>> t.search(6) is not None
37+
True
38+
>>> t.search(-1) is not None
39+
False
40+
41+
>>> t.get_max().value
42+
14
43+
>>> t.get_min().value
44+
1
45+
>>> t.empty()
46+
False
47+
>>> for i in testlist:
48+
... t.remove(i)
49+
>>> t.empty()
50+
True
2351
"""
2452

2553
from collections.abc import Iterable
@@ -197,36 +225,7 @@ def postorder(curr_node: Node | None) -> list[Node]:
197225
return node_list
198226

199227

200-
def binary_search_tree_example() -> None:
201-
testlist = (8, 3, 6, 1, 10, 14, 13, 4, 7)
202-
t = BinarySearchTree()
203-
for i in testlist:
204-
t.insert(i)
205-
206-
# Prints all the elements of the list in order traversal
207-
print(t)
208-
209-
if t.search(6) is not None:
210-
print("The value 6 exists")
211-
else:
212-
print("The value 6 doesn't exist")
213-
214-
if t.search(-1) is not None:
215-
print("The value -1 exists")
216-
else:
217-
print("The value -1 doesn't exist")
218-
219-
if not t.empty():
220-
print("Max Value: ", t.get_max().value) # type: ignore
221-
print("Min Value: ", t.get_min().value) # type: ignore
222-
223-
for i in testlist:
224-
t.remove(i)
225-
print(t)
226-
227-
228228
if __name__ == "__main__":
229229
import doctest
230230

231231
doctest.testmod(verbose=True)
232-
binary_search_tree_example()

0 commit comments

Comments
 (0)