使用graphviz可视化二叉树,AVL,B树,红黑树,森林

几年前就知道graphviz,那时候我试着用它来画流程图,但是效果不理想。直到最近重学数据结构时,将graphviz与代码结合起来才显现出它的强大,特别是复杂的树类数据结构,如果没有好的可视化方法,调试代码将会非常耗时。

在线graphviz画图:https://edotor.net

AVL

avl.svg

digraph {
 splines=false;
 node [style=filled,color=lightblue;];

 node0x10063b100[label="40 (h3)"]
 node0x10063b100 -> node0x10063aaf0
 node0x10063aaf0 -> node0x10063b100
 node0x10063aaf0[label="36 (h1)"]
 node0x10063aaf0 -> node0x10063ab20
 node0x10063ab20 -> node0x10063aaf0
 node0x10063ab20[label="27 (h0)"]
 rChild0x10063aaf0 [label="Null"][style = dotted]
 node0x10063aaf0 -> rChild0x10063aaf0[style = dotted]
 node0x10063b100 -> node0x10070b9c0
 node0x10070b9c0 -> node0x10063b100
 node0x10070b9c0[label="46 (h2)"]
 node0x10070b9c0 -> node0x100406ba0
 node0x100406ba0 -> node0x10070b9c0
 node0x100406ba0[label="41 (h0)"]
 node0x10070b9c0 -> node0x10063ab50
 node0x10063ab50 -> node0x10070b9c0
 node0x10063ab50[label="58 (h1)"]
 node0x10063ab50 -> node0x10063abb0
 node0x10063abb0 -> node0x10063ab50
 node0x10063abb0[label="53 (h0)"]
 node0x10063ab50 -> node0x10063b0d0
 node0x10063b0d0 -> node0x10063ab50
 node0x10063b0d0[label="69 (h0)"]
}

B树

btree.svg

digraph {
 splines=false;
 node [shape = record,height=.1,style=filled,color=lightblue;];

 node0x10055a2e0 [label = " <node268> 268"]
 node0x10055a2e0:<node268>:sw -> node0x10055a060
 node0x10055a060 [label = " <node53> 53| <node56> 56| <node196> 196"]
 node0x10055a060:<node53>:sw -> node0x100559a80
 node0x100559a80 [label = " <node51> 51| <node52> 52"]
 node0x10055a060:<node53>:se -> node0x10055a310
 node0x10055a310 [label = " <node54> 54| <node55> 55"]
 node0x10055a060:<node56>:se -> node0x10055a240
 node0x10055a240 [label = " <node57> 57| <node152> 152"]
 node0x10055a060:<node196>:se -> node0x100559b10
 node0x100559b10 [label = " <node249> 249| <node266> 266"]
 node0x10055a2e0:<node268>:se -> node0x10055a290
 node0x10055a290 [label = " <node310> 310| <node468> 468"]
 node0x10055a290:<node310>:sw -> node0x10055a0b0
 node0x10055a0b0 [label = " <node299> 299| <node300> 300"]
 node0x10055a290:<node310>:se -> node0x10055a1b0
 node0x10055a1b0 [label = " <node315> 315| <node423> 423"]
 node0x10055a290:<node468>:se -> node0x10055a160
 node0x10055a160 [label = " <node484> 484| <node528> 528"]
}

其实B树中的节点就相当于二叉树中的节点组成的“超级节点”:
graph.svg

digraph {
 splines=false;
 node [style=filled,color=lightblue;];

 nodetop[label="";style=invis]
 nodetop->node0x10050b7f0
 node0x10050b7f0[label=""]
 node0x10050b7f0 -> node0x10064a070
 node0x10064a070[label=""]
 node0x10064a070 -> node0x100649ac0
 node0x100649ac0[label=""]
 node0x100649ac0 -> node0x100649a60
 node0x100649a60[label=""]
 node0x100649a60 -> node0x100649a30
 node0x100649a30[label="";style=invis]
 node0x100649a60 -> node0x100649a90
 node0x100649a90[label="";style=invis]
 node0x100649ac0 -> node0x10064a010
 node0x10064a010[label=""]
 node0x10064a010 -> node0x100649af0
 node0x100649af0[label="";style=invis]
 node0x10064a010 -> node0x10064a040
 node0x10064a040[label="";style=invis]
 node0x10064a070 -> node0x10064a130
 node0x10064a130[label=""]
 node0x10064a130 -> node0x10064a0d0
 node0x10064a0d0[label=""]
 node0x10064a0d0 -> node0x10064a0a0
 node0x10064a0a0[label="";style=invis]
 node0x10064a0d0 -> node0x10064a100
 node0x10064a100[label="";style=invis]
 node0x10064a130 -> node0x10064a190
 node0x10064a190[label=""]
 node0x10064a190 -> node0x10064a160
 node0x10064a160[label="";style=invis]
 node0x10064a190 -> node0x10050b7c0
 node0x10050b7c0[label="";style=invis]
 node0x10050b7f0 -> node0x10050bf00
 node0x10050bf00[label=""]
 node0x10050bf00 -> node0x10050b8b0
 node0x10050b8b0[label=""]
 node0x10050b8b0 -> node0x10050b850
 node0x10050b850[label=""]
 node0x10050b850 -> node0x10050b820
 node0x10050b820[label="";style=invis]
 node0x10050b850 -> node0x10050b880
 node0x10050b880[label="";style=invis]
 node0x10050b8b0 -> node0x10050b910
 node0x10050b910[label=""]
 node0x10050b910 -> node0x10050b8e0
 node0x10050b8e0[label="";style=invis]
 node0x10050b910 -> node0x10050bed0
 node0x10050bed0[label="";style=invis]
 node0x10050bf00 -> node0x10050bfc0
 node0x10050bfc0[label=""]
 node0x10050bfc0 -> node0x10050bf60
 node0x10050bf60[label=""]
 node0x10050bf60 -> node0x10050bf30
 node0x10050bf30[label="";style=invis]
 node0x10050bf60 -> node0x10050bf90
 node0x10050bf90[label="";style=invis]
 node0x10050bfc0 -> node0x100730680
 node0x100730680[label=""]
 node0x100730680 -> node0x100730650
 node0x100730650[label="";style=invis]
 node0x100730680 -> node0x1007306b0
 node0x1007306b0[label="";style=invis]

subgraph cluster0 {
    style="rounded"
    bgcolor="#E3E3E5"
    node0x10050b7f0
    node0x10064a070
    node0x10050bf00
}
subgraph cluster1 {
    style="rounded"
    bgcolor="#E3E3E5"
    node0x100649ac0
    node0x100649a60
    node0x10064a010
}
subgraph cluster2 {
    style="rounded"
    bgcolor="#E3E3E5"
    node0x10064a130
    node0x10064a0d0
    node0x10064a190
}
subgraph cluster3 {
    style="rounded"
    bgcolor="#E3E3E5"
    node0x10050b8b0
    node0x10050b850
    node0x10050b910
}
subgraph cluster4 {
    style="rounded"
    bgcolor="#E3E3E5"
    node0x10050bfc0
    node0x10050bf60
    node0x100730680
}
}

红黑树

redblack.svg

digraph {
 splines=false;
 node [style=filled,color=lightblue;];

 node0x100447e00 [label="41 (h1)",color=black,fontcolor=white]
 node0x100447e00 -> node0x100447920
 node0x100447920 -> node0x100447e00
 node0x100447920 [label="36 (h0)",color=red,fontcolor=white]
 node0x100447920 -> node0x100447950
 node0x100447950 -> node0x100447920
 node0x100447950 [label="27 (h0)",color=black,fontcolor=white]
 node0x100447950 -> node0x1004479b0
 node0x1004479b0 -> node0x100447950
 node0x1004479b0 [label="6 (h-1)",color=red,fontcolor=white]
 rChild0x100447950 [label="Null"][style = dotted]
 node0x100447950 -> rChild0x100447950[style = dotted]
 node0x100447920 -> node0x100447dd0
 node0x100447dd0 -> node0x100447920
 node0x100447dd0 [label="40 (h0)",color=black,fontcolor=white]
 node0x100447dd0 -> node0x100447e30
 node0x100447e30 -> node0x100447dd0
 node0x100447e30 [label="39 (h-1)",color=red,fontcolor=white]
 rChild0x100447dd0 [label="Null"][style = dotted]
 node0x100447dd0 -> rChild0x100447dd0[style = dotted]
 node0x100447e00 -> node0x100447980
 node0x100447980 -> node0x100447e00
 node0x100447980 [label="58 (h0)",color=red,fontcolor=white]
 node0x100447980 -> node0x1004479e0
 node0x1004479e0 -> node0x100447980
 node0x1004479e0 [label="53 (h0)",color=black,fontcolor=white]
 node0x100447980 -> node0x100447da0
 node0x100447da0 -> node0x100447980
 node0x100447da0 [label="69 (h0)",color=black,fontcolor=white]
 lChild0x100447da0 [label="Null"][style = dotted]
 node0x100447da0 -> lChild0x100447da0[style = dotted]
 node0x100447da0 -> node0x100447e60
 node0x100447e60 -> node0x100447da0
 node0x100447e60 [label="76 (h-1)",color=red,fontcolor=white]
}

森林

forest.svg

digraph {
 node [shape="box",style=filled,color="#8139e5ff"]
 edge [color="#8139e5ff",minlen="10"]
 node0x10070e2a0[label=""]
 node0x10070e2a0->node0x10070e340[dir="both"]
 {rank=same; node0x10070e2a0;node0x10070e340}
 node [shape= ellipse,style=filled,color=lightblue]
 edge [color=lightblue,minlen="1"];
 node0x10070e2a0->node0x10070e280
 node0x10070e280[label=" ^ "]
 node0x10070e280 -> node0x1006500c0
 node0x1006500c0 -> node0x10070e280
 node0x1006500c0[label=" e "]
 node0x10070e280 -> node0x10070e0a0
 node0x10070e0a0 -> node0x10070e280
 node0x10070e0a0[label=" ^ "]
 node0x10070e0a0 -> node0x100650520
 node0x100650520 -> node0x10070e0a0
 node0x100650520[label=" s "]
 node0x10070e0a0 -> node0x10070df10
 node0x10070df10 -> node0x10070e0a0
 node0x10070df10[label=" ^ "]
 node0x10070df10 -> node0x100450f40
 node0x100450f40 -> node0x10070df10
 node0x100450f40[label=" ^ "]
 node0x100450f40 -> node0x10051f3e0
 node0x10051f3e0 -> node0x100450f40
 node0x10051f3e0[label=" ^ "]
 node0x10051f3e0 -> node0x1006502a0
 node0x1006502a0 -> node0x10051f3e0
 node0x1006502a0[label=" k "]
 node0x10051f3e0 -> node0x100450ea0
 node0x100450ea0 -> node0x10051f3e0
 node0x100450ea0[label=" ^ "]
 node0x100450ea0 -> node0x10064f7b0
 node0x10064f7b0 -> node0x100450ea0
 node0x10064f7b0[label=" H "]
 node0x100450ea0 -> node0x10064f940
 node0x10064f940 -> node0x100450ea0
 node0x10064f940[label=" M "]
 node0x100450f40 -> node0x100450ef0
 node0x100450ef0 -> node0x100450f40
 node0x100450ef0[label=" ^ "]
 node0x100450ef0 -> node0x10064fb20
 node0x10064fb20 -> node0x100450ef0
 node0x10064fb20[label=" S "]
 node0x100450ef0 -> node0x10064f030
 node0x10064f030 -> node0x100450ef0
 node0x10064f030[label=" 0 "]
 node0x10070df10 -> node0x100650020
 node0x100650020 -> node0x10070df10
 node0x100650020[label=" c "]
 
 node [shape="box",style=filled,color="#8139e5ff"]
 edge [color="#8139e5ff",minlen="10"]
 node0x10070e340[label=""]
 node [shape= ellipse,style=filled,color=lightblue]
 edge [color=lightblue,minlen="1"];
 node0x10070e340->node0x10070e320
 node0x10070e320[label=" ^ "]
 node0x10070e320 -> node0x10070e190
 node0x10070e190 -> node0x10070e320
 node0x10070e190[label=" ^ "]
 node0x10070e190 -> node0x1006503e0
 node0x1006503e0 -> node0x10070e190
 node0x1006503e0[label=" o "]
 node0x10070e190 -> node0x100650570
 node0x100650570 -> node0x10070e190
 node0x100650570[label=" t "]
 node0x10070e320 -> node0x10070e1e0
 node0x10070e1e0 -> node0x10070e320
 node0x10070e1e0[label=" ^ "]
 node0x10070e1e0 -> node0x10070dfb0
 node0x10070dfb0 -> node0x10070e1e0
 node0x10070dfb0[label=" ^ "]
 node0x10070dfb0 -> node0x100450fe0
 node0x100450fe0 -> node0x10070dfb0
 node0x100450fe0[label=" ^ "]
 node0x100450fe0 -> node0x100650160
 node0x100650160 -> node0x100450fe0
 node0x100650160[label=" g "]
 node0x100450fe0 -> node0x100650340
 node0x100650340 -> node0x100450fe0
 node0x100650340[label=" m "]
 node0x10070dfb0 -> node0x1006501b0
 node0x1006501b0 -> node0x10070dfb0
 node0x1006501b0[label=" h "]
 node0x10070e1e0 -> node0x10064ff80
 node0x10064ff80 -> node0x10070e1e0
 node0x10064ff80[label=" a "]
}

graphviz语法

n - north 北,指图形正上方
w - west 西,指图形正左方
s - south 南,指图形正下方
e - east 东,指图形正右方
ne - northeast 东北,指图形右上角
nw - northwest 西北,指图形左上角
se - southeast 东南,指图形右下方
sw - southwest 西南,指图形左下方
c - Center 中心

posted @ 2020-06-26 10:29:48
评论加载中...

发表评论