我正在尝试并排格式化2个(或理想情况下是N个)断开连接的二进制树,我希望格式化为二叉树的“正确”,大致如下:
即使缺少某些节点,其余节点也应保持原样。
我使用this Stack answer中名为tree.gv
的脚本为一棵树生成了此平衡的近似值:
dot one_tree.dot | gvpr -c -f tree.gv | neato -n -Tpdf -o tree.pdf
它不是我想要的方式完美平衡,但它非常可行。
现在我想并排多棵树。这是我尝试过的:
dot trees.dot | neato -n -Goverlap=false -Tpdf -o tree.pdf
正确的并排,但底部的黑色叶子应该向右张开,为他们失踪的兄弟姐妹留出空间。
dot trees.dot | gvpr -c -f tree.gv | neato -n -Tpdf -o tree.pdf
每棵树格式正确,但它们重叠。
当然两者结合起来会有效吗?
dot trees.dot | gvpr -c -f tree.gv | neato -n -Goverlap=false -Tpdf -o tree.pdf
以下是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;
}
答案 0 :(得分:2)
形成树的主要技巧是使用具有高边权的额外不可见节点,然后将其直接放置在父节点下方。外表的顺序很重要。
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;
LowerL;
LowerM [style=invis];
LowerR;
Lower -> LowerR;
Lower -> LowerM [weight=100 style=invis];
Lower -> LowerL;
LowerLM [style=invis];
LowerLR;
LowerRM [style=invis];
LowerRR;
LowerL -> LowerLR;
LowerL -> LowerLM [weight=100 style=invis];
LowerR -> LowerRR;
LowerR -> LowerRM [weight=100 style=invis];
node [fillcolor="red"];
Upper;
UpperR;
UpperM [style=invis];
UpperL;
Upper -> UpperR;
Upper -> UpperM [weight=100 style=invis];
Upper -> UpperL;
}
要防止独立树木或树枝水平重叠,您必须使用群集(请参阅我对Making graphviz trees have parents centred above children的回答)
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"];
graph [style=invis];
subgraph cluster_Lower {
Lower;
LowerL;
LowerM [style=invis];
LowerR;
Lower -> LowerR;
Lower -> LowerM [weight=100 style=invis];
Lower -> LowerL;
LowerLM [style=invis];
LowerLR;
LowerRM [style=invis];
LowerRR;
LowerL -> LowerLR;
LowerL -> LowerLM [weight=100 style=invis];
LowerR -> LowerRR;
LowerR -> LowerRM [weight=100 style=invis];
}
node [fillcolor="red"];
subgraph cluster_Upper {
Upper;
UpperR;
UpperM [style=invis];
UpperL;
Upper -> UpperR;
Upper -> UpperM [weight=100 style=invis];
Upper -> UpperL;
}
}