Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make invisible nodes occupy space in graphviz?

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?

like image 288
Fernando Avatar asked Aug 12 '15 14:08

Fernando


People also ask

What is the difference between node () and edge () methods in GraphViz?

The node () method takes a name identifier as first argument and an optional label . The edge () method takes the names of start node and end node, while edges () takes an iterable of name pairs. Keyword arguments are turned into (node and edge) attributes (see extensive Graphviz docs on available attributes ).

Is it difficult to reposition nodes in GraphViz?

See Render Graphviz graphs directly in your posts Yep, repositioning nodes, edges, and clusters is often difficult. Each of the three has its own challenges.

How does GraphViz handle space and newlines?

In Graphviz, the layout is statically determined by the input, so it is reasonable to treat ordinary space characters as non-breaking. In addition, ignoring tabs and newlines allows the input text to be formatted for easier reading. Each of the HTML elements has a set of optional attributes.

Can I use HTML labels in GraphViz?

The grammar below describes precisely what Graphviz will accept. Although HTML labels are not, strictly speaking, a shape, they can be viewed as a generalization of the record shapes described above. In particular, if a node has set its shape attribute to none or plaintext, the HTML label will be the node’s shape.


1 Answers

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

like image 164
vaettchen Avatar answered Sep 30 '22 14:09

vaettchen