ExpressionTree

ExpressionTree[expr]

gives a Tree object from the structure of the expression expr.

ExpressionTree[expr,struct]

gives a Tree object interpreting subexpressions of expr as specified by struct.

Details

  • ExpressionTree gives a Tree object.
  • Possible forms for struct include:
  • "HeadTrees"trees corresponding to heads of subexpressions of the expression
    "Heads"heads of subexpressions of the expression
    "Arguments"arguments of subexpressions of the expression by level
    "Subexpressions"subexpressions of the expression by level
    "Atoms"atomic subexpressions of the expression at level -1
  • Held versions of these structure for struct include:
  • "HeldHeadTrees"trees corresponding to held heads of subexpressions of the expression
    "HeldHeads"held heads of subexpressions of the expression
    "HeldArguments"held arguments of subexpressions of the expression by level
    "HeldSubexpressions"held subexpressions of the expression by level
    "HeldAtoms"held atomic subexpressions of the expression at level -1

Examples

open allclose all

Basic Examples  (3)

Construct a tree from the structure of an expression:

Specify the structure to use for the nodes' data:

Transform an unevaluated expression into a tree:

Scope  (8)

Basic  (3)

Transform nested lists into a tree:

Transform any symbolic expression:

Specify the structure to use for the data:

"HeadTrees"  (1)

ExpressionTree[head[arg1,arg2,],"HeadTrees"] gives a tree with root containing ExpressionTree[head,"HeadTrees"] and with children given by the ExpressionTree[argi,"HeadTrees"]:

ExpressionTree[atom,"HeadTrees"] gives a leaf containing the atomic expression atom:

If AtomQ[head] gives True, then ExpressionTree[head[arg1,arg2,],"HeadTrees"] gives a tree containing head rather than ExpressionTree[head,"HeadTrees"]:

ExpressionTree[head[],"HeadTrees"] still gives a leaf containing ExpressionTree[head,"HeadTrees"] even if head is an atomic expression:

"Heads"  (1)

ExpressionTree[head[arg1,arg2,],"Heads"] gives a tree with root containing head and children given by the ExpressionTree[argi,"Heads"]:

ExpressionTree[atom,"Heads"] gives a leaf containing the atomic expression atom:

ExpressionTree[head[],"Heads"] gives a leaf containing head[] rather than head.

"Arguments"  (1)

ExpressionTree[head[arg1,arg2,],"Arguments"] gives a tree with root containing head[arg1,arg2,] and children given by the ExpressionTree[argi,"Arguments"]:

ExpressionTree[atom,"Arguments"] gives a leaf containing the atomic expression atom:

"Subexpressions"  (1)

ExpressionTree[expr0[expr1,expr2,],"Subexpressions"] gives a tree with root containing expr0[expr1,expr2,] and children given by the ExpressionTree[expri,"Subexpressions"]:

ExpressionTree[atom,"Subexpressions"] gives a leaf containing the atomic expression atom:

"Atoms"  (1)

ExpressionTree[expr0[expr1,expr2,],"Atoms"] gives a tree with children given by the ExpressionTree[expri,"Atoms"] with leaves containing the atomic subexpressions of expr by level:

ExpressionTree[atom,"Atoms"] gives a leaf containing the atomic expression atom:

Properties & Relations  (9)

Construct a tree from an expression:

Get back the original expression:

With the default structure "HeadTrees", ExpressionTree constructs a tree with subtrees containing data giving the tree structure of the heads of subexpressions:

The structure "Heads" gives a tree representing the same data as "HeadTrees", but with data containing the heads of subexpressions themselves:

ExpressionTree[expr,"Heads"] gives output similar to TreeForm[expr]:

The structure "Arguments" gives a tree with the same shape as the one given by "Heads", but with data containing the arguments of subexpressions rather than their heads:

The subtrees of the tree on level n correspond to the parts of the expression on level n, excluding heads:

The structure "Subexpressions" gives a tree extending the one given by "Arguments", with subtrees corresponding to each subexpression, including heads:

The subtrees of the tree on level n correspond to the parts of the expression on level n, including heads:

The structure "Atoms" gives a tree with the same shape as the one given by "Subexpressions", but storing data only in the leaf nodes:

Depth[expr] is equivalent to TreeDepth[ExpressionTree[expr,"Arguments"]]+1:

Depth[expr,HeadsTrue] is equivalent to TreeDepth[ExpressionTree[expr,"Subexpressions"]]+1:

ExpressionTree evaluates subexpressions before constructing the tree:

Use Unevaluated to defer evaluation of subexpressions until after the tree is constructed:

Held structures ensure that subexpressions are wrapped in HoldForm to prevent all evaluation:

Show the HoldForm wrappers:

Possible Issues  (2)

ExpressionTree[expr[],"Heads"] gives a leaf containing expr[], not expr:

This prevents ambiguity if expr is atomic:

ExpressionTree[expr[],"HeadTrees"] gives a leaf containing ExpressionTree[expr,"HeadTrees"]:

If expr is atomic, then expr[arg1,arg2,] will give a tree with root containing expr instead:

Neat Examples  (1)

Construct a tree from the structure of a complicated antiderivative:

Wolfram Research (2021), ExpressionTree, Wolfram Language function, https://reference.wolfram.com/language/ref/ExpressionTree.html.

Text

Wolfram Research (2021), ExpressionTree, Wolfram Language function, https://reference.wolfram.com/language/ref/ExpressionTree.html.

BibTeX

@misc{reference.wolfram_2021_expressiontree, author="Wolfram Research", title="{ExpressionTree}", year="2021", howpublished="\url{https://reference.wolfram.com/language/ref/ExpressionTree.html}", note=[Accessed: 26-September-2021 ]}

BibLaTeX

@online{reference.wolfram_2021_expressiontree, organization={Wolfram Research}, title={ExpressionTree}, year={2021}, url={https://reference.wolfram.com/language/ref/ExpressionTree.html}, note=[Accessed: 26-September-2021 ]}

CMS

Wolfram Language. 2021. "ExpressionTree." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/ExpressionTree.html.

APA

Wolfram Language. (2021). ExpressionTree. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ExpressionTree.html