Tree 点代码:确定左和右子项
我想用圆点做一棵红黑的树。 但是我的节点的位置总是有问题 这是我到目前为止的代码(由于放置问题,尚未完成): 所以我得到的是: 但我想要的是: 如您所见,我的代码切换节点15和40,以及45和NILTree 点代码:确定左和右子项,tree,graphviz,dot,red-black-tree,Tree,Graphviz,Dot,Red Black Tree,我想用圆点做一棵红黑的树。 但是我的节点的位置总是有问题 这是我到目前为止的代码(由于放置问题,尚未完成): 所以我得到的是: 但我想要的是: 如您所见,我的代码切换节点15和40,以及45和NIL 即使我更改了代码中的顺序,结果也不会改变。看起来添加具有相同秩的显式(不可见)关系可以解决问题。 在这种情况下: {rank=same; R1;B4;} R1->B4 [style="invis"] {rank=same; R5;N1;} N1->R5 [style="invi
即使我更改了代码中的顺序,结果也不会改变。看起来添加具有相同秩的显式(不可见)关系可以解决问题。 在这种情况下:
{rank=same; R1;B4;}
R1->B4 [style="invis"]
{rank=same; R5;N1;}
N1->R5 [style="invis"];
节点在图形上的显示顺序取决于它们的定义顺序 因此,如果希望在(40)之前渲染节点(15),则必须先定义(15),然后定义(40): 另一种解决方案是使用不可见边和
rank=same
属性来定位节点,正如@albert所建议的那样,但我建议只有在用尽了上述所有自然定位方法后才使用这种方法
在这种情况下,您将丢失整洁的样式分组,因为您必须至少为(15)和[NIL]手动定义样式。但这是较小的罪恶
综上所述,我的代码解决方案:
digraph G {
node [style=filled, color=black, shape=circle, width=.6
fontname=Helvetica, fontweight=bold, fontcolor=white,
fontsize=24, fixedsize=true];
B1[label = 35];
R1[color=red label = 15];
B2[label = 10];
B3[label = 25];
N1[label = "NIL", shape=record, width=.4,height=.25 fontsize=16];
B4[label = 40];
R2[color=red label = 5];
R3[color=red label = 20];
R4[color=red label = 30];
R5[color=red label = 45];
N2[label = "NIL", shape=record, width=.4,height=.25 fontsize=16];
N3[label = "NIL", shape=record, width=.4,height=.25 fontsize=16];
N4[label = "NIL", shape=record, width=.4,height=.25 fontsize=16];
N5[label = "NIL", shape=record, width=.4,height=.25 fontsize=16];
N6[label = "NIL", shape=record, width=.4,height=.25 fontsize=16];
N7[label = "NIL", shape=record, width=.4,height=.25 fontsize=16];
N8[label = "NIL", shape=record, width=.4,height=.25 fontsize=16];
N9[label = "NIL", shape=record, width=.4,height=.25 fontsize=16];
N10[label = "NIL", shape=record, width=.4,height=.25 fontsize=16];
B1 -> B4;
B1 -> R1;
R1 -> B2;
R1 -> B3;
B4 -> N1;
B4 -> R5;
}
结果:
R1[label = 15];
B4[label = 40];
digraph G {
node [style=filled, color=black, shape=circle, width=.6
fontname=Helvetica, fontweight=bold, fontcolor=white,
fontsize=24, fixedsize=true];
B1[label = 35];
R1[color=red label = 15];
B2[label = 10];
B3[label = 25];
N1[label = "NIL", shape=record, width=.4,height=.25 fontsize=16];
B4[label = 40];
R2[color=red label = 5];
R3[color=red label = 20];
R4[color=red label = 30];
R5[color=red label = 45];
N2[label = "NIL", shape=record, width=.4,height=.25 fontsize=16];
N3[label = "NIL", shape=record, width=.4,height=.25 fontsize=16];
N4[label = "NIL", shape=record, width=.4,height=.25 fontsize=16];
N5[label = "NIL", shape=record, width=.4,height=.25 fontsize=16];
N6[label = "NIL", shape=record, width=.4,height=.25 fontsize=16];
N7[label = "NIL", shape=record, width=.4,height=.25 fontsize=16];
N8[label = "NIL", shape=record, width=.4,height=.25 fontsize=16];
N9[label = "NIL", shape=record, width=.4,height=.25 fontsize=16];
N10[label = "NIL", shape=record, width=.4,height=.25 fontsize=16];
B1 -> B4;
B1 -> R1;
R1 -> B2;
R1 -> B3;
B4 -> N1;
B4 -> R5;
}