{"id":13595480,"url":"https://github.com/joowani/binarytree","last_synced_at":"2025-05-14T15:11:01.832Z","repository":{"id":40990862,"uuid":"68668338","full_name":"joowani/binarytree","owner":"joowani","description":"Python Library for Studying Binary Trees","archived":false,"fork":false,"pushed_at":"2023-10-04T20:59:57.000Z","size":249,"stargazers_count":1816,"open_issues_count":4,"forks_count":172,"subscribers_count":45,"default_branch":"main","last_synced_at":"2025-04-11T05:12:53.133Z","etag":null,"topics":["algorithm","binary-search-tree","binary-tree","bst","data-structures","heap","heaps","interview-practice","python","python-2","python-3"],"latest_commit_sha":null,"homepage":"http://binarytree.readthedocs.io","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/joowani.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2016-09-20T03:01:51.000Z","updated_at":"2025-03-17T11:37:35.000Z","dependencies_parsed_at":"2024-01-16T22:20:05.174Z","dependency_job_id":"22083511-c241-4601-b220-581fc164ca52","html_url":"https://github.com/joowani/binarytree","commit_stats":{"total_commits":57,"total_committers":9,"mean_commits":6.333333333333333,"dds":"0.21052631578947367","last_synced_commit":"74e0c0bf204a0a2789c45a07264718f963db37fe"},"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joowani%2Fbinarytree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joowani%2Fbinarytree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joowani%2Fbinarytree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/joowani%2Fbinarytree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/joowani","download_url":"https://codeload.github.com/joowani/binarytree/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248345267,"owners_count":21088244,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithm","binary-search-tree","binary-tree","bst","data-structures","heap","heaps","interview-practice","python","python-2","python-3"],"created_at":"2024-08-01T16:01:50.778Z","updated_at":"2025-04-11T05:12:57.193Z","avatar_url":"https://github.com/joowani.png","language":"Python","readme":"# Binarytree: Python Library for Studying Binary Trees\n\n![Build](https://github.com/joowani/binarytree/workflows/Build/badge.svg)\n![CodeQL](https://github.com/joowani/binarytree/workflows/CodeQL/badge.svg)\n[![codecov](https://codecov.io/gh/joowani/binarytree/branch/main/graph/badge.svg?token=C2X2OMPL65)](https://codecov.io/gh/joowani/binarytree)\n[![PyPI version](https://badge.fury.io/py/binarytree.svg)](https://badge.fury.io/py/binarytree)\n[![GitHub license](https://img.shields.io/github/license/joowani/binarytree?color=brightgreen)](https://github.com/joowani/binarytree/blob/main/LICENSE)\n![Python version](https://img.shields.io/badge/python-3.7%2B-blue)\n\nAre you studying binary trees for your next exam, assignment or technical interview?\n\n**Binarytree** is a Python library which lets you generate, visualize, inspect and\nmanipulate [binary trees](https://en.wikipedia.org/wiki/Binary_tree). Skip the tedious\nwork of setting up test data, and dive straight into practising your algorithms.\n[Heaps](https://en.wikipedia.org/wiki/Heap_(data_structure)) and\n[binary search trees](https://en.wikipedia.org/wiki/Binary_search_tree) are also supported.\nSelf-balancing search trees like [red-black](https://en.wikipedia.org/wiki/Red%E2%80%93black_tree)\nor [AVL](https://en.wikipedia.org/wiki/AVL_tree) will be added in the future.\n\nCheck out the [documentation](http://binarytree.readthedocs.io) for more details.\n\n![IPython Demo](gifs/demo.gif)\n\nBinarytree can be used with [Graphviz](https://graphviz.org) and\n[Jupyter Notebooks](https://jupyter.org) as well:\n\n![Jupyter Demo](gifs/jupyter.gif)\n\n## Requirements\n\nPython 3.7+\n\n## Installation\n\nInstall via [pip](https://pip.pypa.io):\n\n```shell\npip install binarytree --upgrade\n```\n\nFor [conda](https://docs.conda.io) users:\n\n```shell\nconda install binarytree -c conda-forge\n```\n\n## Getting Started\n\nBinarytree uses the following class to represent a node:\n\n```python\nclass Node:\n\n    def __init__(self, value, left=None, right=None):\n        self.value = value  # The node value (float/int/str)\n        self.left = left    # Left child\n        self.right = right  # Right child\n```\n\nGenerate and pretty-print various types of binary trees:\n\n```python\nfrom binarytree import tree, bst, heap\n\n# Generate a random binary tree and return its root node.\nmy_tree = tree(height=3, is_perfect=False)\n\n# Generate a random BST and return its root node.\nmy_bst = bst(height=3, is_perfect=True)\n\n# Generate a random max heap and return its root node.\nmy_heap = heap(height=3, is_max=True, is_perfect=False)\n\n# Pretty-print the trees in stdout.\nprint(my_tree)\n#\n#        _______1_____\n#       /             \\\n#      4__          ___3\n#     /   \\        /    \\\n#    0     9      13     14\n#         / \\       \\\n#        7   10      2\n#\nprint(my_bst)\n#\n#            ______7_______\n#           /              \\\n#        __3__           ___11___\n#       /     \\         /        \\\n#      1       5       9         _13\n#     / \\     / \\     / \\       /   \\\n#    0   2   4   6   8   10    12    14\n#\nprint(my_heap)\n#\n#              _____14__\n#             /         \\\n#        ____13__        9\n#       /        \\      / \\\n#      12         7    3   8\n#     /  \\       /\n#    0    10    6\n#\n```\nGenerate trees with letter values instead of numbers:\n\n```python\nfrom binarytree import tree\n\nmy_tree = tree(height=3, is_perfect=False, letters=True)\n\nprint(my_tree)\n#\n#          ____H____\n#         /         \\\n#      __E__         F__\n#     /     \\       /   \\\n#    M       G     J     B\n#     \\     /     /     / \\\n#      O   L     D     I   A\n#\n```\n\n\nBuild your own trees:\n\n```python\nfrom binarytree import Node\n\nroot = Node(1)\nroot.left = Node(2)\nroot.right = Node(3)\nroot.left.right = Node(4)\n\nprint(root)\n#\n#      __1\n#     /   \\\n#    2     3\n#     \\\n#      4\n#\n```\n\nInspect tree properties:\n\n```python\nfrom binarytree import Node\n\nroot = Node(1)\nroot.left = Node(2)\nroot.right = Node(3)\nroot.left.left = Node(4)\nroot.left.right = Node(5)\n\nprint(root)\n#\n#        __1\n#       /   \\\n#      2     3\n#     / \\\n#    4   5\n#\nassert root.height == 2\nassert root.is_balanced is True\nassert root.is_bst is False\nassert root.is_complete is True\nassert root.is_max_heap is False\nassert root.is_min_heap is True\nassert root.is_perfect is False\nassert root.is_strict is True\nassert root.leaf_count == 3\nassert root.max_leaf_depth == 2\nassert root.max_node_value == 5\nassert root.min_leaf_depth == 1\nassert root.min_node_value == 1\nassert root.size == 5\n\n# See all properties at once.\nassert root.properties == {\n    'height': 2,\n    'is_balanced': True,\n    'is_bst': False,\n    'is_complete': True,\n    'is_max_heap': False,\n    'is_min_heap': True,\n    'is_perfect': False,\n    'is_strict': True,\n    'leaf_count': 3,\n    'max_leaf_depth': 2,\n    'max_node_value': 5,\n    'min_leaf_depth': 1,\n    'min_node_value': 1,\n    'size': 5\n}\n\nprint(root.leaves)\n# [Node(3), Node(4), Node(5)]\n\nprint(root.levels)\n# [[Node(1)], [Node(2), Node(3)], [Node(4), Node(5)]]\n```\n\nCompare and clone trees:\n```python\nfrom binarytree import tree\n\noriginal = tree()\n\n# Clone the binary tree.\nclone = original.clone()\n\n# Check if the trees are equal.\noriginal.equals(clone)\n```\n\n\nUse [level-order (breadth-first)](https://en.wikipedia.org/wiki/Tree_traversal#Breadth-first_search) \nindexes to manipulate nodes:\n\n```python\nfrom binarytree import Node\n\nroot = Node(1)                  # index: 0, value: 1\nroot.left = Node(2)             # index: 1, value: 2\nroot.right = Node(3)            # index: 2, value: 3\nroot.left.right = Node(4)       # index: 4, value: 4\nroot.left.right.left = Node(5)  # index: 9, value: 5\n\nprint(root)\n#\n#      ____1\n#     /     \\\n#    2__     3\n#       \\\n#        4\n#       /\n#      5\n#\nroot.pprint(index=True)\n#\n#       _________0-1_\n#      /             \\\n#    1-2_____        2-3\n#            \\\n#           _4-4\n#          /\n#        9-5\n#\nprint(root[9])\n# Node(5)\n\n# Replace the node/subtree at index 4.\nroot[4] = Node(6, left=Node(7), right=Node(8))\nroot.pprint(index=True)\n#\n#       ______________0-1_\n#      /                  \\\n#    1-2_____             2-3\n#            \\\n#           _4-6_\n#          /     \\\n#        9-7     10-8\n#\n\n# Delete the node/subtree at index 1.\ndel root[1]\nroot.pprint(index=True)\n#\n#    0-1_\n#        \\\n#        2-3\n```\n\nTraverse trees using different algorithms:\n\n```python\nfrom binarytree import Node\n\nroot = Node(1)\nroot.left = Node(2)\nroot.right = Node(3)\nroot.left.left = Node(4)\nroot.left.right = Node(5)\n\nprint(root)\n#\n#        __1\n#       /   \\\n#      2     3\n#     / \\\n#    4   5\n#\nprint(root.inorder)\n# [Node(4), Node(2), Node(5), Node(1), Node(3)]\n\nprint(root.preorder)\n# [Node(1), Node(2), Node(4), Node(5), Node(3)]\n\nprint(root.postorder) \n# [Node(4), Node(5), Node(2), Node(3), Node(1)]\n\nprint(root.levelorder) \n# [Node(1), Node(2), Node(3), Node(4), Node(5)]\n\nprint(list(root)) # Equivalent to root.levelorder\n# [Node(1), Node(2), Node(3), Node(4), Node(5)]\n```\n\nConvert to [list representations](https://en.wikipedia.org/wiki/Binary_tree#Arrays):\n\n```python\nfrom binarytree import build\n\n# Build a tree from list representation\nvalues = [7, 3, 2, 6, 9, None, 1, 5, 8]\nroot = build(values)\nprint(root)\n#\n#            __7\n#           /   \\\n#        __3     2\n#       /   \\     \\\n#      6     9     1\n#     / \\\n#    5   8\n#\n\n# Go back to list representation\nprint(root.values) \n# [7, 3, 2, 6, 9, None, 1, 5, 8]\n```\n\nBinarytree supports another representation which is more compact but without\nthe [indexing properties](https://en.wikipedia.org/wiki/Binary_tree#Arrays) \n(this method is often used in [Leetcode](https://leetcode.com/)):\n\n```python\nfrom binarytree import build, build2, Node\n\n# First let's create an example tree.\nroot = Node(1)\nroot.left = Node(2)\nroot.left.left = Node(3)\nroot.left.left.left = Node(4)\nroot.left.left.right = Node(5)\nprint(root)\n#\n#           1\n#          /\n#       __2\n#      /\n#     3\n#    / \\\n#   4   5\n\n# First representation is already shown above.\n# All \"null\" nodes in each level are present.\nprint(root.values)\n# [1, 2, None, 3, None, None, None, 4, 5]\n\n# Second representation is more compact but without the indexing properties.\nprint(root.values2)\n# [1, 2, None, 3, None, 4, 5]\n\n# Build trees from the list representations\ntree1 = build(root.values)\ntree2 = build2(root.values2)\nassert tree1.equals(tree2) is True\n```\n\nCheck out the [documentation](http://binarytree.readthedocs.io) for more details.\n","funding_links":[],"categories":["Python","Education"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoowani%2Fbinarytree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjoowani%2Fbinarytree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjoowani%2Fbinarytree/lists"}