Tree visualization algorithm
Asked Answered
D

4

8

Is there some algorithm for tree data structure visualization? I tried googling, but couldnt find any. I'm pretty sure there has to be some algorithm for this not that simple task. Or anyone has some ideas?

Defendant answered 3/12, 2011 at 14:36 Comment(3)
Are you looking for something like Graphviz? graphviz.orgEvitaevitable
Are you sure you're looking for an algorithm or a service that displays it for you?Unipolar
I have to visualise tree in my project therefore I need algorithm.Defendant
T
9

Assumption: you want each node to be displayed such that it is centered above its child nodes.

To achieve this, calculate the width of each node, which I define as the amount of horizontal space required to display this node's entire subtree, such that it doesn't overlap with its left or right siblings' subtrees.

This leads to:

width = 1 + sum(widths of children's nodes)

So, do a depth-first traversal through the tree to calculate each node's width. To display, do a breadth-first traversal to draw the tree level by level.

This is the rough idea of how to go about it. You might want to tweak the width calculation depending on the details of how you would like to render the tree.

Transduction answered 3/12, 2011 at 16:7 Comment(0)
M
4

Tree-mapping is probably what you are looking for. Graphviz is good for visualizing graph structures not specialized for tree structures. I could not find it again but I remember have read in a scientific article that treemaps (I think the voronoi) are optimal to represent tree structures, regarding the place they consume and the area can be used to represent some unit (like byte size for example).

Here are some alternatives.

Here is a good list of articles and other information about the topic.

Malapropism answered 3/12, 2011 at 16:41 Comment(0)
S
1

You can use the DOT language with graphviz for example.

Sigismond answered 3/12, 2011 at 14:41 Comment(0)
T
0

You can also print the tree left-to-right i.e. root at the very left, the first level right to that and so on. You'll find the tree printed with each level on it's own 'column'. The algorithm is somewhat like this:

print(node, spaces):
    if node has left child:
        print(left_child, spaces + '    ')
    print spaces + node + '\n'
    if node has right child:
        print(right_child, spaces + '    ')

This algorithm will print one tree node per line. Each level of the tree will be indented to the right by some spaces. This algorithm will print the items in ascending order, but the descending order may be achieved by processing the right child first.

Trash answered 7/1, 2012 at 20:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.