TreeExpression

TreeExpression[tree]

gives the expression corresponding to the tree tree.

TreeExpression[tree,struct]

interprets the data of the descendants according to struct.

Details

  • In TreeExpression[tree], tree must be 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
  • Possible structures for the expression f[a,b][g[1,2],x,y] include:
  • TreeExpression[ExpressionTree[expr,struct],struct] gives expr.

Examples

open allclose all

Basic Examples  (1)

Transform a tree with two leaves into an expression:

Specify the structure to use:

Scope  (2)

Transform a leaf into an expression:

Transform a tree with multiple levels:

Properties & Relations  (7)

The expression constructed by TreeExpression is evaluated before being returned:

Construct a tree from an expression:

Get back the original expression:

Transform a tree into an expression:

Transform the resulting expression back into a tree:

Construct a tree from the heads in an expression:

Use TreeFold to insert a parent node above each subtree:

This corresponds to mapping on the arguments in an expression:

Map maps on the arguments in an expression by default:

Construct a tree from the atoms in an expression:

Use TreeFold to insert a sibling node before each subtree:

This corresponds to mapping on the subexpressions in an expression:

Map maps on the subexpressions in an expression with HeadsTrue:

Construct a tree from the heads in an expression:

TreeMap maps on the data of subtrees in a tree:

This corresponds to mapping on the heads and leaves in an expression:

Map can map on the heads and leaves directly:

Construct a tree from the atoms in an expression:

TreeMap can map on the data of just the leaves in a tree:

This corresponds to mapping on the atoms in an expression:

Map can map on the atoms in an expression directly:

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

Text

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

CMS

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

APA

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

BibTeX

@misc{reference.wolfram_2021_treeexpression, author="Wolfram Research", title="{TreeExpression}", year="2021", howpublished="\url{https://reference.wolfram.com/language/ref/TreeExpression.html}", note=[Accessed: 22-May-2022 ]}

BibLaTeX

@online{reference.wolfram_2021_treeexpression, organization={Wolfram Research}, title={TreeExpression}, year={2021}, url={https://reference.wolfram.com/language/ref/TreeExpression.html}, note=[Accessed: 22-May-2022 ]}