A library for displaying a binary tree in plain text.
Project description
treevis
A library for displaying binary trees in plain text.
Features
- No tree implementation is provided. The library accepts any object that conforms to the node interface.
- Automatically calculates margins
- Can display multiple trees side-by-side like this:
_____7_______ _____7_______ / \ / \ __3_ ___17______ __3_ ___19___ / \ / \ / \ / \ 1 5 11 _25 1 5 11 _25 / \ / \ / \ / \ / \ / \ / \ / \ 0 2 4 6 9 15 _20 27 0 2 4 6 9 15 20 27 / 19 - Supports node coloring.
Usage
Implement a tree node and construct a tree in any way you prefer.
A tree node must have value, left, right as its properties.
Optionally you may use a color property to assign a color to a node. Should be useful when working with Red-Black trees, and in general when it is needed to mark certain nodes with colors. Color must be an integer in range 0 - 255. For convenience there are pre-defined colors in the library.
class Node:
def __init__(self, value, left, right, color=None):
self.value = value
self.left = left
self.right = right
self.color = color
Call draw function on the root node.
from treevis import draw, Colors
from node import Node # your node implementation
root = Node(5, Node(2, color=Colors.Red), Node(7, color=Colors.Gray), color=Colors.Blue)
draw(root)
Observe the result in terminal.
5
/ \
4 3
draw function accepts any number of root nodes.
If multiple tree nodes are passed to draw function, the trees will be displayed side-by-side:
draw(root, root, root)
would result in:
5 5 5
/ \ / \ / \
4 3 4 3 4 3
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file treevis-0.1.0.tar.gz.
File metadata
- Download URL: treevis-0.1.0.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.27.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b3730831950ee29d70f2b697537fd42e7b18245a4733012530d5fc37ec1380f8
|
|
| MD5 |
6dda8daa0561a9e9c1aa10bf0633f1c9
|
|
| BLAKE2b-256 |
c8bdb5922bd8d1e963c262ad68e240bfee4c82b5424cdfa86a92b230b610bba0
|
File details
Details for the file treevis-0.1.0-py2.py3-none-any.whl.
File metadata
- Download URL: treevis-0.1.0-py2.py3-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.27.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1576b80c25d650fef7d32c61ee7f71bd36a560c20e62f4f0170e954b0222186e
|
|
| MD5 |
0bf13da8a960642ddc49b462008afd41
|
|
| BLAKE2b-256 |
5fe097f8594571d38e72f74260cdbde5bf415a8c1aa5027abd6c2801169956f4
|