Mathematica:如何使部分二叉树显示为二进制?

时间:2011-07-05 11:51:42

标签: wolfram-mathematica

在最近的SO discussion中,我展示了一个需要修剪顶点6和7的二元分类树:

needs pruning

以下是我使用的代码:

KaryTree[9, 2, 
 VertexLabels -> {1 -> "Blood pressure > 91 ?", 2 -> "Age > 62.5?", 
   4 -> "Sinus tachycardia ?", 8 -> "< 30 days"}, 
 EdgeLabels -> {1 \[UndirectedEdge] 2 -> "yes", 
   1 \[UndirectedEdge] 3 -> "no", 2 \[UndirectedEdge] 4 -> "yes", 
   2 \[UndirectedEdge] 5 -> "no", 4 \[UndirectedEdge] 8 -> "yes", 
   4 \[UndirectedEdge] 9 -> "no"}, ImagePadding -> 20]

如果叶子6和7被VertexDelete修剪,顶点8和9也会被剪裁:

VertexDelete[
   KaryTree[7, 2, 
     VertexLabels -> {1 -> "Blood pressure > 91 ?", 2 -> "Age > 62.5?", 
                      4 -> "Has sinus tachycardia ?"}, 
     EdgeLabels -> {1 \[UndirectedEdge] 2 -> "yes", 
                    1 \[UndirectedEdge] 3 -> "no", 2 \[UndirectedEdge] 4 -> "yes", 
                    2 \[UndirectedEdge] 5 -> "no"}, ImagePadding -> 20], {6, 7}]

VertexDelete

TreeGraph愿意绘制图表,但它对如何布置图表有自己的想法:

TreeGraph[{1 \[UndirectedEdge] 2, 1 \[UndirectedEdge] 3, 
           2 \[UndirectedEdge] 4, 2 \[UndirectedEdge] 5, 4 \[UndirectedEdge] 6,
           4 \[UndirectedEdge] 7}, 
   VertexLabels -> {1 -> "Blood pressure > 91 ?", 2 -> "Age > 62.5?", 
                    4 -> "Has sinus tachycardia ?", 6 -> "< 30 days"}, 
   EdgeLabels -> {1 \[UndirectedEdge] 2 -> "yes", 
                  1 \[UndirectedEdge] 3 -> "no", 2 \[UndirectedEdge] 4 -> "yes", 
                  2 \[UndirectedEdge] 5 -> "no", 4 \[UndirectedEdge] 6 -> "yes", 
                  4 \[UndirectedEdge] 7 -> "no"}, ImagePadding -> 20]

TreeGraph

我希望顶点1显示在顶部作为图形的根。

我玩过各种GraphLayout设置但没有找到解决方案。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

也许您可以使用:

vertex = {1 -> "Blood pressure > 91 ?",   2 -> "Age > 62.5?", 
          4 -> "Has sinus tachycardia ?", 6 -> "< 30 days"}; 

TreePlot[{{1 -> 2, yes}, {1 -> 3, no},  {2 -> 4, yes}, 
          {2 -> 5, no},  {4 -> 6, yes}, {4 -> 7, no}} /. vertex, Top, 
         1 /. vertex, VertexLabeling -> True]

enter image description here

修改

或者,如果你想要更好的模拟:

gr = Graphics[
     List[Hue[0.6`, 0.2`, 0.8`], EdgeForm[Opacity[0.7`]], 
      Disk[List[2.5021729686848975, 1.6681153124565984], 0.02965727689850835]], 
     Rule[ImageSize, List[13.`, Automatic]]] ;

vertex = {1 -> "Blood pressure > 91 ?",   2 -> "Age > 62.5?", 
          4 -> "Has sinus tachycardia ?", 6 -> "< 30 days",
          3 -> "",  5 -> "  ", 7 -> "   "};

TreePlot[{{1 -> 2, yes}, {1 -> 3, no }, {2 -> 4, yes}, 
          {2 -> 5,  no}, {4 -> 6, yes}, {4 -> 7, no}}/. vertex, Top, 1/. vertex, 
 VertexLabeling -> True, 
 PlotStyle -> Hue[0.6`, 0.7`, 0.5`], 
 VertexRenderingFunction -> ({Inset[gr, #1], Inset[#2, #1, {-1.3, -1}]} &)]  

enter image description here