Skip to content
Closed
Show file tree
Hide file tree
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
105 changes: 94 additions & 11 deletions data_structures/binary_tree/binary_search_tree_recursive.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,14 @@ def empty(self) -> None:
Empties the tree

>>> t = BinarySearchTree()
>>> assert t.root is None
>>> t.put(8)
>>> assert t.root is not None
>>> t.put(10)
>>> t.put(5)
>>> t.root is None
False
>>> t.empty()
>>> t.root is None
True
"""
self.root = None

Expand All @@ -49,6 +54,9 @@ def is_empty(self) -> bool:
>>> t.put(8)
>>> t.is_empty()
False
>>> t.remove(8)
>>> t.is_empty()
True
"""
return self.root is None

Expand All @@ -58,16 +66,37 @@ def put(self, label: int) -> None:

>>> t = BinarySearchTree()
>>> t.put(8)
>>> assert t.root.parent is None
>>> assert t.root.label == 8
>>> t.root.parent is None
True
>>> t.root.label == 8
True

>>> t.put(10)
>>> assert t.root.right.parent == t.root
>>> assert t.root.right.label == 10
>>> t.root.right.parent == t.root
True
>>> t.root.right.label == 10
True

>>> t.put(3)
>>> assert t.root.left.parent == t.root
>>> assert t.root.left.label == 3
>>> t.root.left.parent == t.root
True
>>> t.root.left.label == 3
True

>>> t.put(5)
>>> t.root.left.right.parent == t.root.left
True
>>> t.root.left.right.label == 5
True

>>> inorder = [node.label for node in t.inorder_traversal()]
>>> inorder == sorted(inorder)
True

>>> t.put(5)
Traceback (most recent call last):
...
ValueError: Node with label 5 already exists
"""
self.root = self._put(self.root, label)

Expand All @@ -91,8 +120,16 @@ def search(self, label: int) -> Node:
>>> t = BinarySearchTree()
>>> t.put(8)
>>> t.put(10)
>>> node = t.search(8)
>>> assert node.label == 8
>>> t.put(5)
>>> t.put(7)
>>> t.search(8).label
8
>>> t.search(5).label
5
>>> t.search(7).label
7
>>> t.search(10).label
10

>>> node = t.search(3)
Traceback (most recent call last):
Expand All @@ -119,8 +156,43 @@ def remove(self, label: int) -> None:
>>> t = BinarySearchTree()
>>> t.put(8)
>>> t.put(10)
>>> t.put(5)
>>> t.put(3)
>>> t.put(7)
>>> t.put(9)
>>> t.put(15)

>>> t.exists(3)
True
>>> t.remove(3)
>>> t.exists(3)
False

>>> t.exists(5)
True
>>> t.remove(5)
>>> t.exists(5), t.exists(7)
(False, True)

>>> t.exists(8)
True
>>> t.remove(8)
>>> assert t.root.label == 10
>>> t.exists(8), t.exists(7), t.exists(10)
(False, True, True)
>>> t.root.label
9

>>> t.remove(10)
>>> t.remove(15)
>>> t.remove(9)
>>> t.exists(9)
False
>>> t.root.label
7

>>> t.remove(7)
>>> t.root == None
True

>>> t.remove(3)
Traceback (most recent call last):
Expand All @@ -144,6 +216,9 @@ def remove(self, label: int) -> None:
self._reassign_nodes(node, None)

def _reassign_nodes(self, node: Node, new_children: Node | None) -> None:
"""
Reassigns 'new_children' in place of 'node' in tree.
"""
if new_children:
new_children.parent = node.parent

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

def _get_lowest_node(self, node: Node) -> Node:
"""
Removes node with minimum label from tree, reassigning
children if necessary and returns it.
"""
if node.left:
lowest_node = self._get_lowest_node(node.left)
else:
Expand Down Expand Up @@ -221,6 +300,10 @@ def get_min_label(self) -> int:
>>> t.put(10)
>>> t.get_min_label()
8
>>> t.put(5)
>>> t.put(3)
>>> t.get_min_label()
3
"""
if self.root is None:
raise ValueError("Binary search tree is empty")
Expand Down
10 changes: 9 additions & 1 deletion data_structures/linked_list/singly_linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@ def insert_nth(self, index: int, data: Any) -> None:
>>> linked_list.insert_nth(3, "fifth")
>>> linked_list
first -> fourth -> second -> fifth -> third
>>> linked_list.insert_nth(-1, 'first')
Traceback (most recent call last):
...
IndexError: list index out of range
>>> linked_list.insert_nth(6, 'sixth')
Traceback (most recent call last):
...
IndexError: list index out of range
"""
if not 0 <= index <= len(self):
raise IndexError("list index out of range")
Expand All @@ -227,7 +235,7 @@ def print_list(self) -> None: # print every node data
>>> linked_list.insert_tail("first")
>>> linked_list.insert_tail("second")
>>> linked_list.insert_tail("third")
>>> linked_list
>>> linked_list.print_list()
first -> second -> third
"""
print(self)
Expand Down