views:

3583

answers:

6
A: 

This is difiicult. What do you do when, from your example, the "8" node has a left child? This issue explains why trees are in practice almost always displayed vertically, as in various file managers.

anon
I see, and what about an algorithm for displayin the tree verticaly ?
psicho
That's easy. For a node, display its children with an indent. Apply the same process recursively to each chid after it is displayed, with indent + 1.
anon
usually, trees are not so heigh but quite wide. so it makes sense to print them vertically anyway. That way, you have also more space for node-data to display.
Johannes Schaub - litb
A file system is not a binary tree. (I guess it's closer to a B-Tree, although even that implies more structure than a file system has.) The small stipulation that each node has at most two children makes all the difference in the world for algorithmically drawing the tree.
David Berger
A: 

I think you shouldn't code that yourself, but have a look at Tree::Visualize which seems to be a nice Perl implementation with different possible styles and use/port one of the algorithms there.

schnaader
+9  A: 

Some hints: the spacing between nodes at the same depth, (e.g., 2 and 4 or 3 and 8 in your example), is a function of the depth.

Each printed row consists of all nodes with the same depth, printed from the leftmost node to the rightmost node.

So you need a way to, for example, arrange your nodes in arrays of rows, according to their depth, in order of their left-most-ness.

Starting from the root node, a breadth-first search will visit nodes in the order of depth and left-most-ness.

Spacing between nodes can be found by finding the maximum height of the tree, using some constant width for the deepest nodes, and doubling that width for every lesser depth, so that the width for any depth = ( 1 + maxdepth - currentdepth ) * deepestwidth.

That number gives you the printed "horizontal width" of each node at any particular depth.

A left node is horizontally positioned in the left half of its parent's width, a righ node in the right half. You'll insert dummy spacers for any node that doesn't have parents; an easier way to do this would be to ensure that all leaves are at the same depth as the deepest node, with blank as their value. Obviously, you'll also have to compensate for the width of the values, perhaps by making the greatest depth's width at least as wide as the printed (decimal representaion, presumably) of he largest valued node.

tpdi
+18  A: 

Check out Printing Binary Trees in Ascii

Jonas Elfström
That's a nice link!
anon
very nice indeed!
Johannes Schaub - litb
Exactly what i was looking for, thanx a lot !
psicho
The site is currently down.
gradbot
link does not work! :(
Ajay
Unfortunately it seems so. Couldn't find it Google Cache or at Internet Archive Wayback machine. This could be it, I haven't tried to run it yet: http://datastructuresblog.wordpress.com/2007/12/21/printing-binary-trees-in-ascii/
Jonas Elfström
+1  A: 

Look at the output of pstree command in Linux. It does not produce the output in the exact form that you want, but IMHO it's more readable that way.

Anonymous
+1  A: 

I second litb's recommendation. I had to do this lately to print the VAD tree of a Windows process and I used DOT language (just print out nodes from your binary tree walking function):

http://en.wikipedia.org/wiki/DOT_language

For example, your DOT file would contains:

digraph graphname {
     5 -> 3;
     5 -> 8;
     3 -> 4;
     3 -> 2;
}

You generate the graph with dotty.exe or convert it to PNG using dot.exe.

Martin