Mathematica: How to make a partial binary tree binary?

In a recent SO discussion, I displayed a binary classification tree that needed to trim vertices 6 and 7:

needs pruning

Below is the code I used:

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] 

If sheets 6 and 7 are cropped on VertexDelete , vertices 8 and 9 are also clipped:

 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 is ready to build a chart, but it has its own mind about how the chart should be laid out:

 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

I would like vertex 1 to appear at the top as the root of the graph.

I played with various GraphLayout settings, but could not find a solution. Any ideas?

+4
source share
1 answer

Perhaps you can use:

 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

Edit

Or, if you want to improve emulation:

 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

+2
source

All Articles