The Binary Tree Library is a Python sample project used by shunsvineyard.info. It is an example for Sphinx and My Python Coding Style.
Although it is a sample project, the Binary Tree Library is a usable tree data structure library, and has the following tree data structures:
- AVL Tree
- Binary Search Tree
- Red Black Tree
- Threaded Binary Trees
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
 
 
The Binary Tree Library requires Python 3.7 or newer. The key Python 3.7 feature used in the project is dataclass.
Install from Github
git clone https://github.com/shunsvineyard/python-sample-code.git
cd python-sample-code
pip install .
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["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")The Binary Tree Library provides a command line tool to simulate tree data structures.
tree-cli
It will show the interactive prompt. Use help to list all the available commands
Welcome to the Tree CLI. Type help or ? to list commands.
tree> help
Documented commands (type help <topic>):
========================================
build  delete  destroy  detail  exit  help  insert  search  traverse