如何让graphviz DOT正确表示二叉树

时间:2016-12-17 02:32:33

标签: graphviz

我有以下DOT代码

digraph BST {
    node [fontname="Arial"];
    1 -> 0.4;
    0.4 -> 0.19;
    null0 [shape=point];
    0 -> null0;
    null1 [shape=point];
    0 -> null1;
    0.4 -> 0.21;
    null2 [shape=point];
    0 -> null2;
    null3 [shape=point];
    0 -> null3;
    1 -> 0.6;
    0.6 -> 0.21;
    0.21 -> 0.09;
    null4 [shape=point];
    0 -> null4;
    null5 [shape=point];
    0 -> null5;
    0.21 -> 0.12;
    null6 [shape=point];
    0 -> null6;
    null7 [shape=point];
    0 -> null7;
    0.6 -> 0.39;
    null8 [shape=point];
    0 -> null8;
    null9 [shape=point];
    0 -> null9;
}

这是输出 graphviz dot output 问题是,我希望0.21成为NODE(0.4)的左子,并希望NODE(0.6) , NODE(0.4)0.21称为两个单独的节点,而不是一个。

注意:不要介意它们是自动生成的。稍后会修复它。

我希望我的输出如下所示。

enter image description here

DOT代码应该有哪些变化?

-Thanks

1 个答案:

答案 0 :(得分:5)

您必须分隔节点名称和标签。

digraph BST {
    node [fontname="Arial" ];
    l1  [ label = "1" ];
    l21 [ label = "0.4" ];
    l22 [ label = "0.6" ];
    l31 [ label = "0.21" ];
    l32 [ label = "0.19" ];
    l33 [ label = "0.21" ];
    l34 [ label = "0.39" ];
    l41 [ label = "0.09" ];
    l42 [ label = "0.12" ];

    l1  -> { l21 l22 };
    l21 -> { l31 l32 };
    l22 -> { l33 l34 };
    l31 -> { l41 l42 };
}

产生

enter image description here