在尝试使用 Graphviz 为二叉树创建图形时,我遇到了很多次问题;显然,有足够高的树和足够大的nodesep
结果图往往不是对称的。例如,这是一个点源
digraph G {
nodesep=0.8;
ranksep=0.5;
{node[style=invis,label=""]; cx_30;
}
{rank=same; 20; 45; cx_30}
{rank=same; 10; 25;}
{rank=same; 40; 50}
30 -> 20;
30 -> 45;
20 -> 10;
20 -> 25;
45 -> 40;
45 -> 50;
{edge[style=invis];
//Distantiate nodes
30 -> cx_30;
20 -> cx_30 -> 45;
//Force ordering between childs
10:e -> 25:w;
40:e -> 50:w;
}
}
带有相应的输出(使用
dot -Tpng file.dot > file.png
编译)如您所见,
45
没有放在 40
之间的中间和 50
.我可以在 40
之间使用隐形节点和 50
来纠正这种情况,但由此产生的间距太宽了。难道我做错了什么?有没有办法纠正这种情况?
最佳答案
即使它对我没有直接作用,我还是通过了 Tom Ron 的建议去查看 this answer关于二叉树;提供的脚本对我不起作用,但 faq entry链接那里帮助我解决了问题;由于间距原因,我不想添加不可见节点,而是指定了正确的 width
不可见节点和缩放的属性 nodesep
因此工作得很好。
这是一个更正的来源:
digraph G {
nodesep=0.4; //was 0.8
ranksep=0.5;
{node[style=invis,label=""]; cx_30;
}
{node[style=invis, label="", width=.1]; ocx_45; ocx_20;
}
{rank=same; 20; 45; cx_30}
{rank=same; 10; 25; ocx_20}
{rank=same; 40; 50; ocx_45}
30 -> 20;
30 -> 45;
20 -> 10;
20 -> 25;
45 -> 40;
45 -> 50;
{edge[style=invis];
//Distantiate nodes
30 -> cx_30;
20 -> cx_30 -> 45;
//Force ordering between children
45 -> ocx_45;
40 -> ocx_45 -> 50;
20 -> ocx_20;
10 -> ocx_20 -> 25;
}
}
与相应的输出
关于layout - 如何使表示二叉树的点图更对称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23429600/