Skip to main content
drawtree
========

.. image:: https://img.shields.io/pypi/v/drawtree.svg?style=flat-square&label=latest%20version
:target: https://pypi.python.org/pypi/drawtree
:alt: Latest PyPI version


Draw binary tree in plain text

Usage
-----
Draw tree from level order traversal, '#' signifies a path terminator where no node exists below.

.. code-block:: python

from drawtree import draw_level_order
draw_level_order('{3,9,20,#,#,15,7}')

::

3
/ \
9 20
/ \
15 7

Draw random binary search tree

.. code-block:: python

from drawtree import draw_random_bst
draw_random_bst(10)

::

64
/ \
/ \
4 66
\ \
37 70
/ \
8 51
/ \
6 12
\
21

Draw binary search tree from integer array

.. code-block:: python

from drawtree import draw_bst
nums = [55, 30, 10, 5, 2, 20, 15, 25, 40, 35, 70, 60, 80, 75, 95]
draw_bst(nums)

::

55
/ \
/ \
/ \
/ \
30 70
/ \ / \
/ \ / \
/ \ 60 80
10 40 / \
/ \ / / \
/ \ 35 75 95
5 20
/ / \
2 / \
15 25


Command line
------------

Flags:
-p, --preorder interpet sequence as preorder
-b, --balanced auto balance bst
-l, --level-order interpet sequence as level-order

Print a bst:

.. code-block:: bash

$ bst 10 5 8 4 6
$ bst nodes.txt
$ echo "colin eric dave" | bst
$ cat nodes.txt | sort | uniq | bst

Print a balanced bst:

.. code-block:: bash

$ bst -b 10 5 6 9 3
$ bst -b nodes.txt
$ bst -b < nodes.txt

Print a bst from a preorder expression:

.. code-block:: bash

$ bst -p dave colin dan
$ echo "1 2 3 4 5" | bst -p
$ bst -p nodes.txt

Print a binary tree from a level order expression:

.. code-block:: bash

$ bst -l [4,#,7,5,9,#] (leetcode format)
$ bst -l {4 # 7 5}

Print a random bst:

.. code-block:: bash

$ bst (random bst of 10 nodes)
$ bst 5 (random bst of 5 nodes)
$ bst -b 7 (random balanced bst of 7 nodes)


Installation
------------
To install drawtree, simply:

.. code-block:: bash

$ pip install drawtree


Licence
-------
MIT

Reference
---------
`Draw tree <http://web.archive.org/web/20071224095835/http://www.openasthra.com/wp-content/uploads/2007/12/binary_trees1.c>`_

Authors
-------

`drawtree` was written by `Madhusudan Banik <msbanik@gmail.com>`_.

Metadata

Release files for drawtree 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for drawtree 0.1.2
File Size Uploaded
drawtree-0.1.2.tar.gz 5.4 kB Details

Release files / drawtree-0.1.2.tar.gz

Download URL drawtree-0.1.2.tar.gz
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
862d26dfaffaa83d92bb3cefd53982b531bc2402bf3584492f1a6af086cc7ec8
BLAKE2b-256 checksum
How to use checksums
349c4e9bc0c12958ec4299ad204af70ad577b8fe529767629b419b94365e4372
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1.2 This release

1 release file

0.1.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page