2011-07-05 48 views
4

在最近SO discussion我显示的需要顶点6和7的一些修剪的二元分类树:Mathematica:如何使部分二叉树显示为二进制?

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被修剪为​​,顶点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设置,但没有找到解决方案。有任何想法吗?

+0

+1,我也有兴趣对此进行更多的控制。也许当你只有树的时候,最好不要使用通用的图形数据结构,而应该使用特定的树形结构。一个相对较新的(未回答!)数学论坛帖子询问了如何控制哪个分支向左移动,哪个分支正好在树中。有时需要保持订购树叶。 – Szabolcs

+0

@Szabolcs是的,我在这种情况下使用了KaryTree,因为我希望所有的“是”响应都能在左边运行。如果将布局移交给TreeGraph,它会让两个yes分支和一个no分支从Age节点下降。这很难理解为医生做出的一系列决定。当然,您可以逐个设置顶点坐标,但当您拥有多个节点时,这变得太笨拙。 – DavidC

回答

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

+0

是的,TreePlot解决了这个问题,无需调整显示器。这似乎是这个工作更好的工具。 – DavidC