How to get graphviz DOT to represent Binary Tree Correctly

3.3k views Asked by At

I have this following DOT code

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;
}

And this is the output graphviz dot output The problem is that I want the 0.21 to be the left child of NODE(0.4) and want that NODE(0.6) , NODE(0.4) to refer 0.21 as two separate nodes instead of one.

NOTE: don't mind the null's they are auto generated. Will fix it later.

I want my Output to be like the following.

enter image description here

What changes should be in the DOT code ?

1

There are 1 answers

0
vaettchen On BEST ANSWER

You have to separate node names and labels.

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 };
}

produces

enter image description here