0

I've used TreeForm, and I appreciate that the syntax is fairly short, especially in comparison with TreeGraph.

Is it possible specify a different colors in TreePlot for coloring a section of the nodes and the paths to those nodes?

dionys
  • 4,321
  • 1
  • 19
  • 46
TraceKira
  • 363
  • 1
  • 8

3 Answers3

10

Anyway, Treegraph offers a lot of flexibility:

nodes = {RandomInteger[#] , # + 1} & /@ Range[0, 30];
rn = Range@Length@nodes;
crules = Rule @@@ Partition[Riffle[rn, ColorData[15, "ColorList"]], 2];
g = TreeGraph[UndirectedEdge @@@ nodes, VertexSize -> 0.4,  VertexStyle -> crules];
HighlightGraph[g, PathGraph@FindShortestPath[g, 1, 30], GraphHighlightStyle -> "Dashed"]

Mathematica graphics

Terse code is nice, flexibility is wonderful.

Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453
4
framedWithColor[color_] := Function[{position, label},
  {Text[Framed[label, Background -> color], position]}
];

TreeForm[
  {{1, 2}, {3, 4}},
  VertexRenderingFunction -> framedWithColor[Pink]
]

enter image description here

0

The terminal nodes of TreeForm are apparently all wrapped in an insidious HoldForm such that one cannot easily give them separate colors.

That is:

ii = 0; 
TreeForm[Nest[{#, ++ii} &, ii, 3],
  VertexRenderingFunction -> ({Black, 
     Text[#2, #1, 
      Background -> (Hue[
         Switch[#2, 0, 0.1, HoldForm@1, 0.2, 2, 0.3, 3, 0.4, List, 
          0.8, _, 0]])]} &)]

treeform bug

berniethejet
  • 1,417
  • 1
  • 12
  • 13