Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions data_structures/binary_tree/binary_search_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
/ \ /
4 7 13

>>> t = BinarySearchTree()
>>> t.insert(8, 3, 6, 1, 10, 14, 13, 4, 7)
>>> t = BinarySearchTree().insert(8, 3, 6, 1, 10, 14, 13, 4, 7)
>>> print(" ".join(repr(i.value) for i in t.traversal_tree()))
8 3 1 6 4 7 10 14 13

Expand Down Expand Up @@ -40,7 +39,16 @@
>>> testlist = (8, 3, 6, 1, 10, 14, 13, 4, 7)
>>> t = BinarySearchTree()
>>> for i in testlist:
... t.insert(i)
... t.insert(i) # doctest: +ELLIPSIS
BinarySearchTree(root=8)
BinarySearchTree(root={'8': (3, None)})
BinarySearchTree(root={'8': ({'3': (None, 6)}, None)})
BinarySearchTree(root={'8': ({'3': (1, 6)}, None)})
BinarySearchTree(root={'8': ({'3': (1, 6)}, 10)})
BinarySearchTree(root={'8': ({'3': (1, 6)}, {'10': (None, 14)})})
BinarySearchTree(root={'8': ({'3': (1, 6)}, {'10': (None, {'14': (13, None)})})})
BinarySearchTree(root={'8': ({'3': (1, {'6': (4, None)})}, {'10': (None, {'14': ...
BinarySearchTree(root={'8': ({'3': (1, {'6': (4, 7)})}, {'10': (None, {'14': (13, ...

Prints all the elements of the list in order traversal
>>> print(t)
Expand Down Expand Up @@ -84,7 +92,7 @@

from collections.abc import Iterable, Iterator
from dataclasses import dataclass
from typing import Any
from typing import Any, Self


@dataclass
Expand Down Expand Up @@ -145,7 +153,18 @@ def __reassign_nodes(self, node: Node, new_children: Node | None) -> None:
self.root = new_children

def empty(self) -> bool:
return self.root is None
"""
Returns True if the tree does not have any element(s).
False if the tree has element(s).

>>> BinarySearchTree().empty()
True
>>> BinarySearchTree().insert(1).empty()
False
>>> BinarySearchTree().insert(8, 3, 6, 1, 10, 14, 13, 4, 7).empty()
False
"""
return not self.root

def __insert(self, value) -> None:
"""
Expand Down Expand Up @@ -173,9 +192,10 @@ def __insert(self, value) -> None:
parent_node = parent_node.right
new_node.parent = parent_node

def insert(self, *values) -> None:
def insert(self, *values) -> Self:
for value in values:
self.__insert(value)
return self

def search(self, value) -> Node | None:
if self.empty():
Expand Down