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

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

以下是我使用的代码:

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}]

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]

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

我玩过各种GraphLayout设置但没有找到解决方案.有任何想法吗?

最佳答案 也许你可以使用:

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]

编辑

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

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}]} &)]  
点赞