Binary Tree Library
The Binary Tree Library is an example of using Sphinx to generate a document.
The library has the following tree data structures:
AVL Tree
Binary Search Tree
The library also provides the tree traversal feature to traverse binary trees.
- Binary Tree Traversal
In-order
Reversed In-order
Pre-order
Post-order
Level-order
Requirements
The Binary Tree Library requires Python 3.13 or newer.
Installation
Install from Github
git clone https://github.com/burpeesDaily/python-sample-code.git
cd python-sample-code
pip install .
Examples
from trees import tree_exceptions
from trees.binary_trees import red_black_tree
from trees.binary_trees import traversal
class Map:
def __init__(self):
self._rbt = red_black_tree.RBTree()
def __setitem__(self, key, value):
self._rbt.insert(key=key, data=value)
def __getitem__(self, key):
return self._rbt.search(key=key).data
def __delitem__(self, key):
self._rbt.delete(key=key)
def __iter__(self):
return traversal.inorder_traverse(tree=self._rbt)
if __name__ == "__main__":
# Initialize the Map instance.
contacts = Map()
# Add some items.
contacts["Mark"] = "mark@email.com"
contacts["John"] = "john@email.com"
contacts["Luke"] = "luke@email.com"
contacts["Matthew"] = "matthew@email.com"
contacts["john"] = "john@email.com"
# Iterate the items.
for contact in contacts:
print(contact)
# Delete one item.
del contacts["john"]
# Check the deleted item.
try:
print(contacts["john"])
except tree_exceptions.KeyNotFoundError:
print("john does not exist")