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

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

问题描述

在尝试使用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)的建议来查看常见问题解答帮助我解决了问题;由于间隔原因,我不想添加不可见节点,但是为不可见节点指定正确的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天全站免登陆