How do you guys print out a binary tree?

Anthony Liu antonyliu2002 at yahoo.com
Tue Apr 18 01:48:06 EDT 2006


There are many ways to represent a binary tree on an
ascii screen.

       1
      / \
    2     3
   / \   / \
  4   5 6   7  

or

   4---2-----------1
       |           |
       5    6----- 3
                   |
                   7

Suppose I have a function that takes a matrix like
this one:

1   2   3   4   5
0   7   8   9   10
0   0   13  14  15
0   0   0   19  20
0   0   0   0   25

Look at the triangle represented by the non-zero
integers.  This triangle is a binary tree if we take 5
as the root and walk down on both sides.

How do I print out this triangle on an ascii console
and visually present it as a binary tree.

Any suggestions?

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 



More information about the Python-list mailing list