4
在最近SO discussion我显示的需要顶点6和7的一些修剪的二元分类树:Mathematica:如何使部分二叉树显示为二进制?
下面是我使用的代码:
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}]
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
设置,但没有找到解决方案。有任何想法吗?
+1,我也有兴趣对此进行更多的控制。也许当你只有树的时候,最好不要使用通用的图形数据结构,而应该使用特定的树形结构。一个相对较新的(未回答!)数学论坛帖子询问了如何控制哪个分支向左移动,哪个分支正好在树中。有时需要保持订购树叶。 – Szabolcs
@Szabolcs是的,我在这种情况下使用了KaryTree,因为我希望所有的“是”响应都能在左边运行。如果将布局移交给TreeGraph,它会让两个yes分支和一个no分支从Age节点下降。这很难理解为医生做出的一系列决定。当然,您可以逐个设置顶点坐标,但当您拥有多个节点时,这变得太笨拙。 – DavidC