Binarytree: Python Library for Studying Binary Trees
Are you studying binary trees for your next exam, assignment or technical interview?
Binarytree is a Python library which lets you generate, visualize, inspect and
manipulate binary trees. Skip the tedious
work of setting up test data, and dive straight into practising your algorithms.
Heaps and
binary search trees are also supported.
Self-balancing search trees like red-black
or AVL will be added in the future.
Check out the documentation for more details.
Binarytree can be used with Graphviz and
Jupyter Notebooks as well:
Requirements
Python 3.7+
Installation
Install via pip:
pip install binarytree --upgrade
For conda users:
conda install binarytree -c conda-forge
Getting Started
Binarytree uses the following class to represent a node:
class Node:
def __init__(self, value, left=None, right=None):
self.value = value
self.left = left
self.right = right
Generate and pretty-print various types of binary trees:
from binarytree import tree, bst, heap
my_tree = tree(height=3, is_perfect=False)
my_bst = bst(height=3, is_perfect=True)
my_heap = heap(height=3, is_max=True, is_perfect=False)
print(my_tree)
print(my_bst)
print(my_heap)
Generate trees with letter values instead of numbers:
from binarytree import tree
my_tree = tree(height=3, is_perfect=False, letters=True)
print(my_tree)
Build your own trees:
from binarytree import Node
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.right = Node(4)
print(root)
Inspect tree properties:
from binarytree import Node
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
print(root)
assert root.height == 2
assert root.is_balanced is True
assert root.is_bst is False
assert root.is_complete is True
assert root.is_max_heap is False
assert root.is_min_heap is True
assert root.is_perfect is False
assert root.is_strict is True
assert root.leaf_count == 3
assert root.max_leaf_depth == 2
assert root.max_node_value == 5
assert root.min_leaf_depth == 1
assert root.min_node_value == 1
assert root.size == 5
assert root.properties == {
'height': 2,
'is_balanced': True,
'is_bst': False,
'is_complete': True,
'is_max_heap': False,
'is_min_heap': True,
'is_perfect': False,
'is_strict': True,
'leaf_count': 3,
'max_leaf_depth': 2,
'max_node_value': 5,
'min_leaf_depth': 1,
'min_node_value': 1,
'size': 5
}
print(root.leaves)
print(root.levels)
Compare and clone trees:
from binarytree import tree
original = tree()
clone = original.clone()
original.equals(clone)
Use level-order (breadth-first)
indexes to manipulate nodes:
from binarytree import Node
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.right = Node(4)
root.left.right.left = Node(5)
print(root)
root.pprint(index=True)
print(root[9])
root[4] = Node(6, left=Node(7), right=Node(8))
root.pprint(index=True)
del root[1]
root.pprint(index=True)
Traverse trees using different algorithms:
from binarytree import Node
root = Node(1)
root.left = Node(2)
root.right = Node(3)
root.left.left = Node(4)
root.left.right = Node(5)
print(root)
print(root.inorder)
print(root.preorder)
print(root.postorder)
print(root.levelorder)
print(list(root))
Convert to list representations:
from binarytree import build
values = [7, 3, 2, 6, 9, None, 1, 5, 8]
root = build(values)
print(root)
print(root.values)
Binarytree supports another representation which is more compact but without
the indexing properties
(this method is often used in Leetcode):
from binarytree import build, build2, Node
root = Node(1)
root.left = Node(2)
root.left.left = Node(3)
root.left.left.left = Node(4)
root.left.left.right = Node(5)
print(root)
print(root.values)
print(root.values2)
tree1 = build(root.values)
tree2 = build2(root.values2)
assert tree1.equals(tree2) is True
Check out the documentation for more details.