Tree visualization algorithm

前端 未结 4 825
清酒与你
清酒与你 2020-12-15 01:14

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 simp

4条回答
  •  情深已故
    2020-12-15 02:03

    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.

提交回复
热议问题