How to make invisible nodes occupy space in graphviz?
Asked Answered
W

2

19

I want to plot a binary tree using graphviz, and it is important that the left child of a node appear to the left (duh) of the right child. If there is no left child, I want an empty space on the left, to make it visually clear that the right child is the right child. I want to do the same if there is no right child (on the right there should be an empty space).

For instance, I want something like:

A                     A
  \     instead of    |
    B                 B

I can make sure Graphviz will place the left child before the right by using ordering = "out", but if there is no left child, then the right child might appear right below its parent.

If I add dummy nodes where a child is missing, I get the correct layout, but the dummy nodes are then on the picture (and I don't want them). I tried using style = "invis" for the dummy nodes and edges connecting to them, but then it is as if they didn't exist for graphviz. How can I get around this problem?

Warpath answered 12/8, 2015 at 14:28 Comment(0)
E
25

Better late than never...

You probably left your dummy node empty (""). If you give it a reasonably sized label, it works fine:

graph Test 
{
    node[ shape = plaintext ];

    C [ label = "C", style = invis ];

    A -- B;
    A -- C [ style = invis ]; 
}

A good example to demonstrate that an MWE saves time and effort...

enter image description here

Enslave answered 31/10, 2015 at 15:14 Comment(2)
Note that this only works if according to the layout B is already aligned on the right side of A, i.e., if the three nodes are aligned in a triangle shape. In this minimal example that's the case, but in others the right child might still be exactly below A, and B's brother might be on it's left. So making it invisible doesn't help.Tendance
@Tendance - that sounds like a different question that would get, of course, a different answer?Enslave
E
0

It gets better if you use two filled invisible nodes, so the node you want to see will definetely stay at the side, and not straight under its ancestor.

Emmuela answered 19/11, 2017 at 18:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.