且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何用java语言画一棵树

更新时间:2021-12-18 04:01:44

树通常是更容易像这样布局的链接结构之一,因为路径通常不会合并"或交叉".

Trees are generally one of the easier linked constructs to lay out like this, because paths generally don't "merge" or "cross".

您可以通过从左到右"遍历树以粗略的表格方式接近它:从根开始,并在区域的左上角绘制其表示.然后,一次遍历其左"分支,在与根相同的列"中依次在较低的行"上绘制这些节点的表示.然后,当您移动到右侧"侧节点时,在同一级别右侧可用的下一个列"中绘制该节点.这将生成树结构的斜坡形图.

You can approach it in a roughly tabular way by traversing the tree "left to right": start at the root, and draw its representation at the top left of the area. Then, traverse its "left" branch one level at a time, drawing those nodes' representations on successively lower "rows", in the same "column" as the root. Then, as you move to the "right"-side nodes, draw that node in the next "column" available to the right on the same level. This will produce a ramp-shaped graph of the tree's structure.

您可以添加一些对每个级别的级别和节点数的预分析,这将允许您通过了解图形所需的最大级别数和数量来将树居中"成粗略的金字塔形状该图的每个级别的节点数.但是,这需要在开始绘制任何内容之前遍历整个图形.

You can add some pre-analysis of the number of levels and nodes at each level, which will allow you to "center" the tree into a rough pyramid shape by knowing the maximum number of levels the graph will require and the number of nodes at each level of that graph. But, that requires traversing the entire graph before you start drawing anything.

至于排列"树的节点,使它们适合最小的区域而没有箭头交叉或重叠,这是一个范围远远超过平均 SO 答案的问题.

As for "arranging" a tree's nodes so they fit in the smallest area without arrows crossing or overlapping, that's a problem with a scope far exceeding the average SO answer.