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:

                                                       Ragnar
                                                         / \
                                                        /   \
                                                       /     \
                                                      /       \
                                                     /         \
                                                    /           \
                                                   /             \
                                                  /               \
                                                 /                 \
                                                /                   \
                                               /                     \
                                              /                       \
                                             /                         \
                                            /                           \
                                           /                             \
                                          /                               \
                                         /                                 \
                                        /                                   \
                                       /                                     \
                                      /                                       \
                                     /                                         \
                                    /                                           \
                                   /                                             \
                                  /                                               \
                                 /                                                 \
                                /                                                   \
                               /                                                     \
                              /                                                       \
                             /                                                         \
                            /                                                           \
                           /                                                             \
                      Lagertha                                                          Floki
                         / \                                                             / \
                        /   \                                                           /   \
                       /     \                                                         /     \
                      /       \                                                       /       \
                     /         \                                                     /         \
                    /           \                                                   /           \
                   /             \                                                 /             \
                  /               \                                               /               \
                 /                 \                                             /                 \
                /                   \                                           /                   \
               /                     \                                         /                     \
              /                       \                                       /                       \
             /                         \                                     /                         \
            /                           \                                   /                           \
           /                             \                                 /                             \
        Rollo                           Bjorn                         Gunnhild                          Ubbe
         /                                                                                               /
        /                                                                                               /
       /                                                                                               /
      /                                                                                               /
     /                                                                                               /
    /                                                                                               /
   /                                                                                               /
Harald                                                                                          Ivar
                                                                                                   \
                                                                                                    \
                                                                                                     \
                                                                                                  Hvitserk
                                                                                                     /
                                                                                                Torstein

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:

                                                       Ragnar
                                                      /       \
                                                  /               \
                                              /                       \
                                          /                               \
                                      /                                       \
                                  /                                               \
                              /                                                       \
                      Lagertha                                                          Floki
                      /       \                                                       /       \
                  /               \                                               /               \
              /                       \                                       /                       \
        Rollo                           Bjorn                         Gunnhild                          Ubbe
      /                                                                                               /
Harald                                                                                          Ivar
                                                                                                    \
                                                                                                  Hvitserk
                                                                                                     /
                                                                                                Torstein

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.0.tar.gz (21.7 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.0-py3-none-any.whl (19.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: vert_tree-1.0.tar.gz
  • Upload date:
  • Size: 21.7 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.0.tar.gz
Algorithm Hash digest
SHA256 297783015377dbad9a13cf717cd658cc452f4bfd91010ae46f91994637558941
MD5 8295536d54b3b1d56964be65fddfb55b
BLAKE2b-256 07f8127eb81d2cbaaa114a24b2a56144019c7ae27667036b6cadafaf2ba4a2a3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: vert_tree-1.0-py3-none-any.whl
  • Upload date:
  • Size: 19.3 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9699d62ab41cc0698937555e90fb649a92ee78005531267e2b7438009e1b8089
MD5 b4f909d263b9f35931ebc58e8aa7fd04
BLAKE2b-256 60e105ddbdcdbd112bf414729a9a9fbe7c4ce6c4a13c85bb6e3a84843fd30f18

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