You can use ExpressionGraph
to draw the tree
expr = ConstantArray[x, Reverse @ Range[4]];
ExpressionGraph[expr, GraphLayout -> "LayeredEmbedding", ImageSize -> Large]
epxr2 = ConstantArray[x, Reverse @ Range[5]];
ExpressionGraph[expr2, GraphLayout -> "LayeredEmbedding", ImageSize -> 700,
VertexSize -> Medium, AspectRatio -> 1/2]
Define a function that constructs a permutation tree with edge labels:
ClearAll[rule, permutationTree]
rule = # /. x : {___Integer} /; Length[x] > 1 :>
(Reverse /@ Subsets[Reverse@x, {Length[x] - 1}]) &;
permutationTree[n_, opts : OptionsPattern[Graph]] :=
Module[{eg = ExpressionGraph[ConstantArray[x, Reverse@Range[n]],
opts, GraphLayout -> "LayeredEmbedding",
ImageSize -> 700, VertexSize -> Medium, AspectRatio -> 1/2],
edgelabels},
edgelabels = Thread[First @ Last @ Reap@
BreadthFirstScan[eg, 1, {"FrontierEdge" -> Sow}] ->
Flatten@NestList[rule, Range[n], n - 1]] ;
SetProperty[eg, EdgeLabels -> edgelabels]]
Examples:
permutationTree[3]
permutationTree[4]
permutationTree[4, GraphLayout -> "RadialEmbedding",
AspectRatio -> 1, EdgeLabelStyle -> Large]
permutationTree[5, ImageSize -> 900]
Alternatively, you can use TreeForm
:
TreeForm[expr, ImageSize -> Large, VertexLabeling -> False]
Note: For versions older than v12.0, replace ExpressionGraph
with GraphComputation`ExpressionGraph
. (See also this answer.)
GraphPlot
toTreePlot
in the source code gives the requested output $\endgroup$