Skip to main content

Small library to visually display binary trees in terminal or curses pad.

Project description

vert_tree

This simple library displays conventional Python binary trees in an intuitive, vertical manner to either the terminal or a curses pad (useful for large trees!).

A compatible Tree representation class:

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

and example tree:

root = TreeNode("a")
root.left = TreeNode("b")
root.right = TreeNode("c")

terminal display

using our simple example tree, we can display it to terminal, attempt to mirror it, and display it again:

from vert_tree import TerminalDisplay

def mirror(node):
    if (node == None):
        return
    else:
        temp = node
        mirror(node.left)
        mirror(node.right)
        temp = node.left
        node.left = node.right
        node.right = temp

terminal = TerminalDisplay()
terminal.display_vert_tree(root)
mirror(root)
print('after mirroring...')
terminal.display_vert_tree(root)

execution output:

$ python test.py
    a
   / \
  b   c
after mirroring...
    a
   / \
  c   b

curses display

terminal display is restricted by the width of the current terminal. Should a tree be too large, it can be displayed vertically in a curses pad instead.

the basic format is similar to our terminal display:

from vert_tree import CursesDisplay
curses_display = CursesDisplay()
curses_display.display_vert_tree(root)

except when executed a curses window will allow for scrolling around the displayed tree. Arrow keys will move the screen and 'q' will exit the window.

Additionally, a curses display can be configured to exit automatically after a number of seconds by passing a timeout argument to the CursesDisplay constructor.

from vert_tree import CursesDisplay
curses_display = CursesDisplay(5)
curses_display.display_vert_tree(root)

in the above case the curses window will exit automatically after 5 seconds.

arrow spacing

some trees with considerable depth may take up a lot of space if we print consistent edge lines. For example:

                        SnCKkZLvPBMe
                             / \
                            /   \
                           /     \
                          /       \
                         /         \
                        /           \
                       /             \
                      /               \
                     /                 \
                    /                   \
                   /                     \
                  /                       \
                 /                         \
                /                           \
               /                             \
     ajjUMZsoDBUgCsWpwW                bjnIUXLjqqWHQS
             / \                             / \
            /   \                           /   \
           /     \                         /     \
          /       \                       /       \
         /         \                     /         \
        /           \                   /           \
       /             \                 /             \
    hKqi     lKPwIcaDXsPtwlruMAX   oxKHLI           MHdg
     /                                 \
    /                                   \
   /                                     \
dENnQtEJbRNCWxmJmgnM                     QyF
                                           \
                                      PjhmeuYPGZRyf

by passing the arrow_spacing argument to either Terminal or Curses Displays, we can eliminate some of the depth by spacing out the edge representations to only print slashes every 'n' number of chars. For instance the same tree can be displayed in a much more compact format:

terminal.display_vert_tree(root, arrow_spacing=4)

output:

                        SnCKkZLvPBMe
                          /       \
                      /               \
                  /                       \
     ajjUMZsoDBUgCsWpwW                bjnIUXLjqqWHQS
          /       \                       /       \
    hKqi     lKPwIcaDXsPtwlruMAX   oxKHLI           MHdg
    /                                   \
dENnQtEJbRNCWxmJmgnM                     QyF
                                           \
                                      PjhmeuYPGZRyf

limitations

  1. TerminalDisplay can only handle Trees that can fit within the current terminal screen width. If the tree is too wide it will exit.
  2. CursesDisplay can handle LARGE trees (up to 32767x32767 on my system) but the underlying curses lib will eventually crash given a large enough tree.
  3. TreeNode vals can be of any length, the displays will attempt to center them to the extent that they can. Any collision with the bounds of the display or other TreeNode val's will result in the longer val getting trimmed.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

vert_tree-1.1.tar.gz (21.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

vert_tree-1.1-py3-none-any.whl (19.4 kB view details)

Uploaded Python 3

File details

Details for the file vert_tree-1.1.tar.gz.

File metadata

  • Download URL: vert_tree-1.1.tar.gz
  • Upload date:
  • Size: 21.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for vert_tree-1.1.tar.gz
Algorithm Hash digest
SHA256 cb259dd76b5fa74e1ffc2bdb8a83dd23b37f5ed840bc57e0707fb3d8692c0967
MD5 43d61d33382ee2f1a0e5fbe53442270a
BLAKE2b-256 f9b76f50bc7c25c395639ccae5117b9226fc69ab90b7901ecfe9893982a048d3

See more details on using hashes here.

File details

Details for the file vert_tree-1.1-py3-none-any.whl.

File metadata

  • Download URL: vert_tree-1.1-py3-none-any.whl
  • Upload date:
  • Size: 19.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

File hashes

Hashes for vert_tree-1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 475d5acf3e7ffeba4eb256214f2d904186cb1859a289ad5a89db3c5db7e028ec
MD5 c6e684fd6fcd96524db42288716e189b
BLAKE2b-256 01e7c0b52e4f50ef369656d3504901193c5a63712fc8d3cf29dd22c82c6c55cc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page