Skip to content

jdmcpeek/pretty-print-binary-tree

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 

Repository files navigation

Pretty-prints a binary tree with child fields left and right. Each node contains a data field, which is printed.

Run python tree.py and check out the results.

The tree may be of any depth, but usually after 6 levels it's too wide for most screens.

Looks best when the nodes' printed values are under 3 characters long (especially on leaf nodes).

# prep the tree...
# 
# layer 1
root = Node('A')

# layer 2
root.left = Node('B')
root.right = Node('C')

# layer 3
root.left.left = Node('D')
root.left.right = Node('E')

# add a sub-tree ('X' nodes) using the createTree method
root.left.right.right = Node.createTree(2)

root.right.left = Node('F')
root.right.right = Node('G')

# layer 3
root.left.left.left = Node('H')
root.left.left.right = Node('I')
root.left.right.left = Node('J')
root.right.right.left = Node('N')
root.right.right.right = Node('O')

root.prettyPrint()
              ______________A______________ 
             /                             \
      ______B______                   ______C______ 
     /             \                 /             \
  __D__           __E__             F             __G__ 
 /     \         /     \                         /     \
H       I       J       X                       N       O 
                       / \                                
                      X  XX                                

About

Simple python module that pretty-prints a binary tree to the command line. Useful for testing.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages