Skip to content

Commit 9294347

Browse files
committed
move doctest to module docstring
1 parent 55844f5 commit 9294347

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

Diff for: data_structures/binary_tree/binary_search_tree.py

+23-23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
1-
"""
1+
r"""
22
A binary search Tree
3+
4+
Example
5+
8
6+
/ \
7+
3 10
8+
/ \ \
9+
1 6 14
10+
/ \ /
11+
4 7 13
12+
13+
>>> t = BinarySearchTree()
14+
>>> t.insert(8, 3, 6, 1, 10, 14, 13, 4, 7)
15+
>>> print(" ".join(repr(i.value) for i in t.traversal_tree()))
16+
8 3 1 6 4 7 10 14 13
17+
>>> print(" ".join(repr(i.value) for i in t.traversal_tree(postorder)))
18+
1 4 7 6 3 13 14 10 8
19+
>>> BinarySearchTree().search(6)
20+
Traceback (most recent call last):
21+
...
22+
IndexError: Warning: Tree is empty! please use another.
323
"""
424

525
from collections.abc import Iterable
@@ -177,28 +197,7 @@ def postorder(curr_node: Node | None) -> list[Node]:
177197
return node_list
178198

179199

180-
def binary_search_tree() -> None:
181-
r"""
182-
Example
183-
8
184-
/ \
185-
3 10
186-
/ \ \
187-
1 6 14
188-
/ \ /
189-
4 7 13
190-
191-
>>> t = BinarySearchTree()
192-
>>> t.insert(8, 3, 6, 1, 10, 14, 13, 4, 7)
193-
>>> print(" ".join(repr(i.value) for i in t.traversal_tree()))
194-
8 3 1 6 4 7 10 14 13
195-
>>> print(" ".join(repr(i.value) for i in t.traversal_tree(postorder)))
196-
1 4 7 6 3 13 14 10 8
197-
>>> BinarySearchTree().search(6)
198-
Traceback (most recent call last):
199-
...
200-
IndexError: Warning: Tree is empty! please use another.
201-
"""
200+
def binary_search_tree_example() -> None:
202201
testlist = (8, 3, 6, 1, 10, 14, 13, 4, 7)
203202
t = BinarySearchTree()
204203
for i in testlist:
@@ -230,3 +229,4 @@ def binary_search_tree() -> None:
230229
import doctest
231230

232231
doctest.testmod(verbose=True)
232+
binary_search_tree_example()

0 commit comments

Comments
 (0)