I'm trying to format 2 (or ideally N) disconnected binary trees side-by-side, and I want the formatting to be "proper" for a binary tree, roughly like so:
Even if some nodes are missing, the rest should stay exactly where they are.
I generated an approximation of this balancing for one tree with a script called tree.gv
from this Stack answer:
dot one_tree.dot | gvpr -c -f tree.gv | neato -n -Tpdf -o tree.pdf
It's not perfectly balanced the way I would like, but it's very workable.
Now I want multiple trees side by side. Here's what I've tried:
dot trees.dot | neato -n -Goverlap=false -Tpdf -o tree.pdf
Properly side-by-side, but the bottom black leaves should be splayed right, leaving room for their missing siblings.
dot trees.dot | gvpr -c -f tree.gv | neato -n -Tpdf -o tree.pdf
Each tree is properly formatted, but they overlap.
Surely combining the two will work?
dot trees.dot | gvpr -c -f tree.gv | neato -n -Goverlap=false -Tpdf -o tree.pdf
Here is trees.dot
:
digraph BST {
graph [center=true, margin=0.01, nodesep=0.1, ranksep=0.3, width=1,ratio=.25];
node [fontname="Arial",style=filled,color="0.0 0.0 0.0",fixedsize=true,width=0.15,shape=circle,label=""];
node [margin=0.01,fillcolor="lightgrey"];
edge [dir=none];
node [fillcolor="black"];
Lower;
Lower -> LowerR;
Lower -> LowerL;
LowerL -> LowerLR;
LowerR -> LowerRR;
node [fillcolor="red"];
Upper;
Upper -> UpperR;
Upper -> UpperL;
}
The main trick to form trees is to use an extra invisible node with high edge weight that is then placed directly below the parent node. The order of appearence counts.
To prevent independent trees or tree branches from horizontal overlapping you have to use clusters (see my answer to Making graphviz trees have parents centred above children)