问题描述
在尝试使用Graphviz为二叉树创建图时,我遇到了很多问题.显然,对于足够高的树和足够大的nodesep
,结果图倾向于不对称.例如,这是一个点源
While trying to use Graphviz to create graphs for binary trees I've encountered many times a problem; apparently, with a high enough tree and a large enough nodesep
the resulting graph tends not to be symmetric. As an example, here's a dot source
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
编译)
with the corresponding output (compiled with dot -Tpng file.dot > file.png
)
如您所见,45
不在40
和50
之间.我可以在40
和50
之间使用不可见的节点来纠正这种情况,但是结果间距会太宽.
As you can see, 45
isn't placed in the middle between 40
and 50
. I could use invisible nodes between 40
and 50
to correct the situation, but the resulting spacing would be too wide.
我做错什么了吗?有办法纠正这种情况吗?
Am I doing something wrong? Is there a way to correct the situation?
推荐答案
即使它对我没有直接作用,我还是通过汤姆·罗恩(Tom Ron)的建议来查看;提供的脚本对我不起作用,但是链接到该链接的常见问题解答帮助我解决了问题;由于间隔原因,我不想添加不可见节点,但是为不可见节点指定正确的width
属性并因此缩放nodesep
效果很好.
Even though it didn't directly work for me, I'm passing the advice of Tom Ron to look at this answer about binary trees; the provided script didn't work for me, but the faq entry linked there helped me solve the problem; I didn't want to add an invisibile node for spacing reasons, but specifying a correct width
attribute for the invisible nodes and scaling nodesep
consequently works just fine.
以下是更正的来源:
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;
}
}
具有相应的输出
这篇关于如何使表示二叉树的点图更加对称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!