tags:

views:

76

answers:

1

Is there a setting in graphviz to generate balanced diagrams like this:

correct diagram

When diagram is more complex like below - it isn't balanced like that above (4 is below **).

not correctly balanced

Code to generate second diagram:

graph
{
  n1 [label="+"];
  n1 -- n2;
  n2 [label="/"];
  n2 -- n3;
  n3 [label="*"];
  n3 -- n4;
  n4 [label="1"];
  n3 -- n5;
  n5 [label="2"];
  n2 -- n6;
  n6 [label="3"];
  n1 -- n7;
  n7 [label="**"];
  n7 -- n8;
  n8 [label="4"];
  n7 -- n9;
  n9 [label="5"];
}
+1  A: 

You can "introduce new, invisible nodes to re-balance the layout." (see http://www.graphviz.org/mywiki/FaqBalanceTree). So your code become :

graph
{
  n1 [label="+"];
  n2 [label="/"];
  n1 -- n2;
  n1b1 [label="", width=.1, style=invis]
  n1 -- n1b1 [style=invis]  
  n1b2 [label="", width=.1, style=invis]
  n1 -- n1b2 [style=invis]  
  n1b3 [label="", width=.1, style=invis]
  n1 -- n1b3 [style=invis]  
  n7 [label="**"];
  n1 -- n7;
  { rank=same n2 -- n1b1 -- n1b2 -- n1b3 -- n7 [style=invis] }

  n3 [label="*"];
  n2 -- n3;
  n2b1 [label="", width=.1, style=invis]
  n2 -- n2b1 [style=invis]
  n6 [label="3"];
  n2 -- n6;
  { rank=same n3 -- n2b1 -- n6 [style=invis] }

  n8 [label="4"];
  n7 -- n8;
  n7b1 [label="", width=.1, style=invis]
  n7 -- n7b1 [style=invis]
  n9 [label="5"];
  n7 -- n9;
  { rank=same n8 -- n7b1 -- n9 [style=invis] }

  n3 -- n4;
  n4 [label="1"];
  n3 -- n5;
  n5 [label="2"];
}

With this output :

alt text

greg
@greg, thanks, it looks great.