|
20 | 20 | Traceback (most recent call last):
|
21 | 21 | ...
|
22 | 22 | 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 |
23 | 51 | """
|
24 | 52 |
|
25 | 53 | from collections.abc import Iterable
|
@@ -197,36 +225,7 @@ def postorder(curr_node: Node | None) -> list[Node]:
|
197 | 225 | return node_list
|
198 | 226 |
|
199 | 227 |
|
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 |
| - |
228 | 228 | if __name__ == "__main__":
|
229 | 229 | import doctest
|
230 | 230 |
|
231 | 231 | doctest.testmod(verbose=True)
|
232 |
| - binary_search_tree_example() |
0 commit comments