({{, (RefLink[TreeForm, paclet:ref/TreeForm])[expr]
displays expr as a tree with different levels at different depths. }, {, (RefLink[TreeForm, paclet:ref/TreeForm])[expr,n]
displays expr as a tree only down to level n.}})

OpenersNotesSection(Spacer1)Details and Options

With a notebook front end, (RefLink[TreeForm, paclet:ref/TreeForm]) displays as a graphical tree in (RefLink[StandardForm, paclet:ref/StandardForm]) and (RefLink[TraditionalForm, paclet:ref/TraditionalForm]).

(RefLink[TreeForm, paclet:ref/TreeForm]) takes the same options as (RefLink[TreePlot, paclet:ref/TreePlot]).

(RefLink[VertexLabeling, paclet:ref/VertexLabeling])->(RefLink[Automatic, paclet:ref/Automatic]) does not display expression fragments at each node, but still gives subtree expression as tooltips.

(RefLink[VertexLabeling, paclet:ref/VertexLabeling])->(RefLink[False, paclet:ref/False]) does not give expression fragments or tooltips.

OpenersPrimaryExamplesSection(Spacer1)Examples  (34)

OpenersExampleSection0(Spacer1)Basic Examples  (3)

A symbolic expression formatted as a tree:

TreeForm[a+b^2+c^3+d]

Show the tree form for the first two levels in the expression:

p=HornerForm[1+x+x^2+x^3,x]

TreeForm[p,2]

A graphics expression formatted as a tree:

//TreeForm

OpenersExampleSection1(Spacer1)Scope  (9)

A formatted symbolic expression with mathematical constants:

TreeForm[(E^(n I π))]

A formatted expression with symbolic, exact and inexact values:

TreeForm[(1/3) (Sqrt[10])(E^((.25)^x))]

A nested list:

TreeForm[{1,{2,{3}}}]

An expression containing subscripted variables:

TreeForm[(Subscript[x, 1])+(Subscript[x, 2])]

A formatted expression with a special superscript and (RefLink[OverBar, paclet:ref/OverBar]):

TreeForm[(SuperDagger[x])+(OverBar[y])]

A graphic object:

//TreeForm

A (RefLink[Series, paclet:ref/Series]) expression:

ser=Series[Sin[x],{x,0,5}]

TreeForm[ser]

An expression containing (RefLink[Hold, paclet:ref/Hold]):

TreeForm[Hold[1+2+3]]

Limit the levels shown:

TreeForm[Nest[{#,{#,{#}}}&,x,2], 4]

OpenersExampleSection2(Spacer1)Options  (14)

OpenersExampleSubsection0(Spacer1)AspectRatio  (2)

By default a suitable aspect ratio is calculated for good visual appearance:

TreeForm[Nest[{#,#,#}&,x,2]]

Change the aspect ratio:

TreeForm[Nest[{#,#,#}&,x,2],AspectRatio->.3]

OpenersExampleSubsection1(Spacer1)DirectedEdges  (1)

Show directed edges:

TreeForm[Integrate[1/(1+1/(1+x)),x],DirectedEdges-> True]

OpenersExampleSubsection2(Spacer1)EdgeRenderingFunction  (2)

Draw edges using blue arrows set back by 30%:

TreeForm[D[x Exp[x],x],EdgeRenderingFunction->({Blue,Arrow[#,.3]}&)]

Draw vertices only:

TreeForm[D[x Exp[x],x], EdgeRenderingFunction->None]

OpenersExampleSubsection3(Spacer1)LayerSizeFunction  (1)

Draw a tree with the first level of height 1, the second level 2, etc.:

TreeForm[Nest[{#,#}&,x,4],LayerSizeFunction->(#&)]

OpenersExampleSubsection4(Spacer1)PlotRangePadding  (1)

Use different (RefLink[PlotRangePadding, paclet:ref/PlotRangePadding]) around the drawing:

Table[Framed[TreeForm[D[x Cos[x] Sin[x],x],PlotRangePadding->p],FrameMargins->0],{p,{0,Scaled[0.2]}}]

OpenersExampleSubsection5(Spacer1)PlotStyle  (3)

Specify an overall style for the drawing:

Table[TreeForm[Cos[Exp[x]],PlotStyle->ps],{ps,{Red,PointSize[Medium],Dashed,Directive[Red,PointSize[Medium],Dashed]}}]

(RefLink[PlotStyle, paclet:ref/PlotStyle]) can be combined with (RefLink[VertexRenderingFunction, paclet:ref/VertexRenderingFunction]), which has higher priority:

TreeForm[Cos[Exp[x]],PlotStyle->Directive[PointSize[Large],Red],VertexRenderingFunction->Function[{p,l},{Green,Point[p]}]]

(RefLink[PlotStyle, paclet:ref/PlotStyle]) can be combined with (RefLink[EdgeRenderingFunction, paclet:ref/EdgeRenderingFunction]), which has higher priority:

TreeForm[Cos[Exp[x]],PlotStyle->Directive[Dashed,Red],EdgeRenderingFunction->Function[{p,vl,el},{Green,Line[p]}]]

OpenersExampleSubsection6(Spacer1)VertexLabeling  (1)

Display subtree expressions as tooltips:

TreeForm[Nest[{#,{#},#}&,x,3],VertexLabeling->Automatic]

OpenersExampleSubsection7(Spacer1)VertexRenderingFunction  (2)

Show no vertices:

TreeForm[D[Sin[Cos[x]],x],VertexRenderingFunction->None]

Render vertices using a predefined graphic:

gr = ;

TreeForm[Nest[{#,{#}}&,x,3],VertexRenderingFunction-> (Inset[gr,#1]&)]

OpenersExampleSubsection8(Spacer1)VertexCoordinateRules  (1)

Explicitly specify all vertex coordinates:

TreeForm[x^2+x^4,VertexCoordinateRules->{{0,2},{0,1},{0,0},{2,-1},{2,1},{1,3},{3,1}}]

OpenersExampleSection3(Spacer1)Applications  (1)

Visualize the Wolfram Axiom for Boolean algebra as a tree:

TreeForm[f[f[f[p,q],r],f[p,f[f[p,r],p]]]==r]

OpenersExampleSection4(Spacer1)Properties & Relations  (4)

(RefLink[FullForm, paclet:ref/FullForm]) gives a linear expression similar to (RefLink[TreeForm, paclet:ref/TreeForm]):

FullForm[1/(a+b)*x^2]

TreeForm[1/(a+b)*x^2]

(RefLink[OutputForm, paclet:ref/OutputForm]) of (RefLink[TreeForm, paclet:ref/TreeForm]) gives a textual display of the expression:

TreeForm[1/(a+b)*x^2]//OutputForm

Use (RefLink[TreePlot, paclet:ref/TreePlot]) to plot a tree graph:

TreePlot[Table[i->RandomInteger[{0,i-1}],{i,50}]]

Use (RefLink[GraphPlot, paclet:ref/GraphPlot]) or (RefLink[GraphPlot3D, paclet:ref/GraphPlot3D]) for general undirected graphs:

GraphPlot[Table[i->Mod[i^2,150],{i,0,150}]]

GraphPlot3D[Table[i->Mod[i^2,150],{i,0,150}]]

Use (RefLink[LayeredGraphPlot, paclet:ref/LayeredGraphPlot]) for hierarchical-style drawing of directed graphs:

LayeredGraphPlot[Table[RandomInteger[{0,50}]->i,{i,50}]]

OpenersExampleSection5(Spacer1)Possible Issues  (1)

Compound heads are not laid out as trees:

TreeForm[g[a][b]]

OpenersExampleSection6(Spacer1)Neat Examples  (2)

A complete binary tree:

Nest[{#,#}&,x,4]

TreeForm[%]

A complete ternary tree:

Nest[{#,#,#}&,x,3]

TreeForm[%]

A symmetric tree:

TreeForm[Nest[{#,{#},#}&,x,5],VertexLabeling->Automatic]

An asymmetric tree:

TreeForm[Nest[{{#},#,#}&,x,5],VertexLabeling->Automatic]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[FullForm, paclet:ref/FullForm])  ▪  (RefLink[MatrixForm, paclet:ref/MatrixForm])  ▪  (RefLink[TreePlot, paclet:ref/TreePlot])  ▪  (RefLink[Graph, paclet:ref/Graph])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Expressions as Trees, paclet:tutorial/ExpressionsAsTrees])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Expression Structure, paclet:guide/ExpressionStructure])}, {▪(RefLinkPlain[Expressions, paclet:guide/Expressions])}, {▪(RefLinkPlain[Graph Visualization, paclet:guide/GraphVisualization])}})}})

({{(Spacer1), ({{(Spacer1)Related Links}, {(WebLink[DocumentationFooterIconsRelatedLinks-EIWL.png(Spacer1)An Elementary Introduction to the Wolfram Language, https://www.wolfram.com/language/elementary-introduction/33-expressions-and-their-structure.html])(WebLink[: Expressions and Their Structure, https://www.wolfram.com/language/elementary-introduction/33-expressions-and-their-structure.html])}})}})