如何使表示二叉树的点图更对称? [英] How do I make a dot graph representing a binary tree more symmetric?

查看:15
本文介绍了如何使表示二叉树的点图更对称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试使用 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 并未位于 4050 之间.我可以在 4050 之间使用不可见节点来纠正这种情况,但结果间距太宽了.

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 的建议来查看 这个答案 关于二叉树;提供的脚本对我不起作用,但链接到那里的 faq entry 帮助我解决了问题;由于间距原因,我不想添加不可见节点,但是为不可见节点指定正确的 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;
    } 
} 

带有对应的输出

这篇关于如何使表示二叉树的点图更对称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆