Assumption: you want each node displayed in such a way that it is centered over its child nodes.
To achieve this, calculate the width of each node, which I define as the amount of horizontal space needed to display this node subtree, so that it does not overlap with its left or right sibling subtrees.
It leads to:
width = 1 + sum(widths of children nodes)
So, take the first step in depth through the tree to calculate each node width. To display, do a width traversal to draw a tree level by level.
This is a rough idea of ββhow to do this. You might want to adjust the width calculation depending on how you want to render the tree.
mbeckish Dec 03 '11 at 16:07 2011-12-03 16:07
source share