|
| 1 | +BinaryTree: Python Library for Learning Binary Trees |
| 2 | +---------------------------------------------------- |
| 3 | + |
| 4 | +.. image:: https://travis-ci.org/joowani/binarytree.svg?branch=master |
| 5 | + :target: https://travis-ci.org/joowani/binarytree |
| 6 | + :alt: Build Status |
| 7 | + |
| 8 | +.. image:: https://badge.fury.io/py/binarytree.svg |
| 9 | + :target: https://badge.fury.io/py/binarytree |
| 10 | + :alt: Package Version |
| 11 | + |
| 12 | +.. image:: https://img.shields.io/badge/python-2.7%2C%203.4%2C%203.5-blue.svg |
| 13 | + :target: https://github.com/joowani/binarytree |
| 14 | + :alt: Python Versions |
| 15 | + |
| 16 | +.. image:: https://coveralls.io/repos/github/joowani/binarytree/badge.svg?branch=master |
| 17 | + :target: https://coveralls.io/github/joowani/binarytree?branch=master |
| 18 | + :alt: Test Coverage |
| 19 | + |
| 20 | +.. image:: https://img.shields.io/github/issues/joowani/binarytree.svg |
| 21 | + :target: https://github.com/joowani/binarytree/issues |
| 22 | + :alt: Issues Open |
| 23 | + |
| 24 | +.. image:: https://img.shields.io/badge/license-MIT-blue.svg |
| 25 | + :target: https://raw.githubusercontent.com/joowani/binarytree/master/LICENSE |
| 26 | + :alt: MIT License |
| 27 | + |
| 28 | +| |
| 29 | +
|
| 30 | +.. image:: https://cloud.githubusercontent.com/assets/2701938/19216253/5063b602-8d82-11e6-9f54-977bee2147a0.gif |
| 31 | + :alt: Demo GIF |
| 32 | + |
| 33 | +Introduction |
| 34 | +============ |
| 35 | + |
| 36 | +Are you studying binary trees for your next exam, assignment or technical interview? |
| 37 | + |
| 38 | +**BinaryTree** is a minimal Python library which provides you with a simple API |
| 39 | +to generate, visualize and inspect binary trees so you can skip the tedious |
| 40 | +work of mocking up test trees, and dive right into practising your algorithms! |
| 41 | +Heaps and BSTs (binary search trees) are also supported. |
| 42 | + |
| 43 | + |
| 44 | +Installation |
| 45 | +============ |
| 46 | + |
| 47 | +To install a stable version from PyPi_: |
| 48 | + |
| 49 | +.. code-block:: bash |
| 50 | +
|
| 51 | + ~$ pip install binarytree |
| 52 | +
|
| 53 | +
|
| 54 | +To install the latest version directly from GitHub_: |
| 55 | + |
| 56 | +.. code-block:: bash |
| 57 | +
|
| 58 | + ~$ git clone https://github.com/joowani/binarytree.git |
| 59 | + ~$ python binarytree/setup.py install |
| 60 | +
|
| 61 | +You may need to use ``sudo`` depending on your environment setup. |
| 62 | + |
| 63 | +.. _PyPi: https://pypi.python.org/pypi/binarytree |
| 64 | +.. _GitHub: https://github.com/joowani/binarytree |
| 65 | + |
| 66 | + |
| 67 | +Getting Started |
| 68 | +=============== |
| 69 | + |
| 70 | +**BinaryTree** uses the following class to represent a tree node: |
| 71 | + |
| 72 | +.. code-block:: python |
| 73 | +
|
| 74 | + class Node(object): |
| 75 | +
|
| 76 | + def __init__(self, value): |
| 77 | + self.value = value |
| 78 | + self.left = None |
| 79 | + self.right = None |
| 80 | +
|
| 81 | +
|
| 82 | +Generate and pretty-print binary trees: |
| 83 | + |
| 84 | +.. code-block:: python |
| 85 | +
|
| 86 | + from binarytree import tree, bst, heap, pprint |
| 87 | +
|
| 88 | + # Generate random binary trees |
| 89 | + my_tree = tree(height=5, balanced=False) |
| 90 | +
|
| 91 | + # Generate random binary search trees |
| 92 | + my_bst = bst(height=5) |
| 93 | +
|
| 94 | + # Generate random min and max heaps |
| 95 | + my_heap = heap(height=1, max=True) |
| 96 | +
|
| 97 | + # Pretty print the binary trees in stdout |
| 98 | + pprint(my_tree) |
| 99 | + pprint(my_bst) |
| 100 | + pprint(my_heap) |
| 101 | +
|
| 102 | +
|
| 103 | +`List representations`_ are also supported: |
| 104 | + |
| 105 | +.. _List representations: |
| 106 | + https://en.wikipedia.org/wiki/Binary_tree#Arrays |
| 107 | + |
| 108 | + |
| 109 | +.. code-block:: python |
| 110 | +
|
| 111 | + from heapq import heapify |
| 112 | + from binarytree import tree, convert, pprint |
| 113 | +
|
| 114 | + my_list = [7, 3, 2, 6, 9, 4, 1, 5, 8] |
| 115 | +
|
| 116 | + # Convert the list into a tree structure |
| 117 | + my_tree = convert(my_list) |
| 118 | +
|
| 119 | + # Convert the list into a heap structure |
| 120 | + heapify(my_list) |
| 121 | + my_tree = convert(my_list) |
| 122 | +
|
| 123 | + # Convert the tree back to a list |
| 124 | + my_list = convert(my_tree) |
| 125 | +
|
| 126 | + # Pretty-printing also works on lists |
| 127 | + pprint(my_list) |
| 128 | +
|
| 129 | +
|
| 130 | +Inspect a tree to quickly see its various properties: |
| 131 | + |
| 132 | +.. code-block:: python |
| 133 | +
|
| 134 | + from binarytree import tree, inspect |
| 135 | +
|
| 136 | + my_tree = tree(height=10) |
| 137 | +
|
| 138 | + result = inspect(my_tree) |
| 139 | + print(result['height']) |
| 140 | + print(result['is_bst']) |
| 141 | + print(result['is_height_balanced']) |
| 142 | + print(result['is_max_heap']) |
| 143 | + print(result['is_min_heap']) |
| 144 | + print(result['is_weight_balanced']) |
| 145 | + print(result['leaf_count']) |
| 146 | + print(result['max_leaf_depth']) |
| 147 | + print(result['max_value']) |
| 148 | + print(result['min_leaf_depth']) |
| 149 | + print(result['min_value']) |
| 150 | + print(result['node_count']) |
| 151 | +
|
| 152 | +
|
| 153 | +Import the `Node` class directly to build your own trees: |
| 154 | + |
| 155 | +.. code-block:: python |
| 156 | +
|
| 157 | + from binarytree import Node, pprint |
| 158 | +
|
| 159 | + root = Node(1) |
| 160 | + root.left = Node(2) |
| 161 | + root.right = Node(3) |
| 162 | + root.left.left = Node(4) |
| 163 | + root.left.right = Node(5) |
| 164 | +
|
| 165 | + pprint(root) |
| 166 | +
|
| 167 | +
|
| 168 | +If the default `Node` class does not meet your requirements, you can define |
| 169 | +and use your own custom node specification: |
| 170 | + |
| 171 | +.. code-block:: python |
| 172 | +
|
| 173 | + from binarytree import setup, tree, pprint |
| 174 | +
|
| 175 | + # Define your own null/sentinel value (default: None) |
| 176 | + null = -1 |
| 177 | +
|
| 178 | + # Define own node class (default: binarytree.Node) |
| 179 | + class MyNode(object): |
| 180 | +
|
| 181 | + def __init__(self, data, left, right): |
| 182 | + self.data = data |
| 183 | + self.l_child = left |
| 184 | + self.r_child = right |
| 185 | +
|
| 186 | + # Call setup in the beginning to apply the custom specification |
| 187 | + setup( |
| 188 | + node_init_func=lambda v: MyNode(v, null, null), |
| 189 | + node_class=MyNode, |
| 190 | + null_value=null, |
| 191 | + value_attr='data', |
| 192 | + left_attr='l_child', |
| 193 | + right_attr='r_child' |
| 194 | + ) |
| 195 | + my_custom_tree = tree() |
| 196 | + pprint(my_custom_tree) |
0 commit comments