Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tree visualization algorithm

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?

like image 717
MrProper Avatar asked Dec 03 '11 14:12

MrProper


1 Answers

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.

like image 57
mbeckish Avatar answered Sep 21 '22 16:09

mbeckish