# Conditional tree vertex rendering?

I use the following code to plot a binary tree

TreePlot[{11 -> 23, 11 -> 24, 23 -> 40, 23 -> 39, 24 -> 30, 24 -> 50,
40 -> 55, 40 -> 45}, VertexLabeling -> All,
PlotStyle -> {FontSize -> 13},
VertexRenderingFunction -> ({EdgeForm[White],
RGBColor[113/255, 190/255, 236/255], Disk[#1, 0.2], White,
Text[#2, #1]} &), AspectRatio -> Automatic] The question is what should I do if I want to change the back color of vertex "11" and "23" to red? Thank you!

• Hi, use TreeGraph with VertexShapeFunction and then SetProperty[{g, 11}, VertexShapeFunction -> ({EdgeForm[White], Red, Disk[#1, 0.2], White, Text[#2, #1]} &)] – Kuba Nov 16 '15 at 12:50
• @Kuba Thanks! Do you know how do I change the font size in TreeGraph? TreeGraph seems not to support PlotStyle directive. – Tony Nov 16 '15 at 13:00

## 3 Answers

ar = ConstantArray[RGBColor[113/255, 190/255, 236/255], 55];
ar[[{11, 23}]] = Red;

TreePlot[{11 -> 23, 11 -> 24, 23 -> 40, 23 -> 39, 24 -> 30, 24 -> 50,
40 -> 55, 40 -> 45},
VertexLabeling -> All,
PlotStyle -> {FontSize -> 13},
VertexRenderingFunction ->
({EdgeForm[White], ar[[#2]], Disk[#1, 0.2], White, Text[#2, #1]} &),
AspectRatio -> Automatic] Here's an alternative approach that does the work in VertexRenderingFunction:

$redDisks = {11, 23}; TreePlot[{11 -> 23, 11 -> 24, 23 -> 40, 23 -> 39, 24 -> 30, 24 -> 50, 40 -> 55, 40 -> 45}, VertexLabeling -> All, PlotStyle -> {FontSize -> 13}, VertexRenderingFunction -> ({EdgeForm[White], If[MemberQ[$redDisks, #2], Red,
RGBColor[113/255, 190/255, 236/255]], Disk[#1, 0.2], White,
Text[#2, #1]} & ), AspectRatio -> Automatic] You can also post-process the output of TreePlot to change the styles of select vertices:

ClearAll[modify]
modify = # /. d : Disk[(Alternatives @@ Cases[#, Text[Alternatives @@ #2, p_] :> p,
{0, Infinity}]), _] :> {#3, d} &;


Examples:

tp = TreePlot[{11 -> 23, 11 -> 24, 23 -> 40, 23 -> 39, 24 -> 30,
24 -> 50, 40 -> 55, 40 -> 45}, VertexLabeling -> All,
PlotStyle -> {FontSize -> 13},
VertexRenderingFunction -> ({EdgeForm[White],
RGBColor[113/255, 190/255, 236/255], Disk[#1, 0.2], White,
Text[#2, #1]} &), AspectRatio -> Automatic];

modify[tp, {11, 23}, Red] Fold[modify[#, #2[], #2[]] &, tp, {{{11, 23}, Red}, {{45, 50}, Purple}}] 