TreeExpression

TreeExpression[tree]

gives an expression from the structure of the Tree object tree.

TreeExpression[tree,struct]

gives an expression with data and subtrees of tree interpreted as specified by struct.

Details and Options

  • TreeExpression can construct lists, arrays, rules, XML and arbitrary expressions from Tree objects as specified.
  • Possible forms for struct for arbitrary expressions include: »
  • "HeadTrees"trees corresponding to heads of subexpressions of the expression by level
    "Heads"heads of subexpressions of the expression by level
    "Subexpressions"subexpressions of the expression by level
    "Atoms"atomic subexpressions of the expression at level -1
    Nullsubexpressions of the expression by level with no data
    Nonethe expression itself
  • Options of TreeExpression include:
  • Heads include heads of expressions and their parts as subtrees
  • Possible structures for the expression f[a,b][g[1,2],x,y] include:
  • Versions of these structures with the option setting HeadsTrue include: »
  • Held forms for struct for arbitrary expressions include:
  • "HeldHeadTrees"trees corresponding to held heads of subexpressions of the expression by level
    "HeldHeads"held heads 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
  • Possible forms for struct for common types of expressions include:
  • "XML"tags, attributes and elements of an XML object
  • Possible forms for struct for nested lists and associations include: »
  • "List"elements of nested lists
    "Association"elements of nested associations
    "Dataset"headers and data of a dataset
  • Possible forms for struct for nested rules include: »
  • "Rules"left-hand sides and leaves of nested rules
    "JSON"names and values of a JSON object, using names as data
    "RawJSON"names and values of a JSON object, using names as keys
    "FileSystem"directories and files in a file system

Examples

open allclose all

Basic Examples  (4)

Construct an expression from the structure of a tree:

Include heads:

Specify the structure to use:

Construct symbolic XML from a tree:

Construct a held expression from a tree:

Construct nested associations from a tree:

Scope  (14)

Arbitrary Expressions  (6)

"HeadTrees"  (1)

If TreeQ[data] gives False, then TreeExpression[Tree[data,{tree1,tree2,}],"HeadTrees"] gives an expression with head data and with arguments given by the TreeExpression[treei,"HeadTrees"]:

If TreeQ[data] gives True, then TreeExpression[Tree[data,{tree1,tree2,}],"HeadTrees"] gives an expression with head TreeExpression[data,"HeadTrees"] rather than data:

Include heads:

TreeExpression[Tree[data,None],"HeadTrees"] gives data:

"Heads"  (1)

TreeExpression[Tree[data,{tree1,tree2,}],"Heads"] gives an expression with head data and with arguments given by the TreeExpression[treei,"Heads"]:

Include heads:

TreeExpression[Tree[data,None],"Heads"] gives data:

"Subexpressions"  (1)

TreeExpression[Tree[,{tree1,tree2,}],"Subexpressions"] gives an expression with arguments given by the TreeExpression[treei,"Subexpressions"]:

Include heads:

TreeExpression[Tree[data,None],"Subexpressions"] gives data:

"Atoms"  (1)

TreeExpression[Tree[,{tree1,tree2,}],"Atoms"] gives an expression with arguments given by the TreeExpression[treei,"Atoms"]:

Include heads:

TreeExpression[Tree[data,None],"Atoms"] gives data:

Null  (1)

TreeExpression[Tree[,{tree1,tree2,}],Null] gives an expression with arguments given by the TreeExpression[treei,Null]:

Include heads:

TreeExpression[Tree[,None],Null] gives Null:

None  (1)

TreeExpression[Tree[data,],None] gives data:

Common Expressions  (1)

"XML"  (1)

Construct symbolic XML from a tree:

Nested Lists and Associations  (3)

"List"  (1)

Construct nested lists from a tree:

"Association"  (1)

Construct nested associations from a tree:

"Dataset"  (1)

Construct a dataset from a tree:

Nested Rules  (4)

"Rules"  (1)

Construct nested rules from a tree:

"JSON"  (1)

Construct JSON rules from a tree:

"RawJSON"  (1)

Construct JSON associations from a tree:

"FileSystem"  (1)

Construct a nested association with file names as keys from a tree:

Options  (2)

Heads  (2)

With the default option setting HeadsFalse, heads of expressions are not included as part of any level:

With HeadsTrue, heads are included just like other elements of expressions:

Structures for arbitrary expressions do not include heads by default:

Include heads:

Properties & Relations  (7)

Transform a tree into an expression:

Transform the resulting expression back into a tree:

Construct a tree from an expression:

Get back the original expression:

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:

The expression constructed by TreeExpression is evaluated before being returned:

Held structures wrap the expression in HoldComplete to prevent evaluation:

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

Text

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

CMS

Wolfram Language. 2021. "TreeExpression." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2022. 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_2024_treeexpression, author="Wolfram Research", title="{TreeExpression}", year="2022", howpublished="\url{https://reference.wolfram.com/language/ref/TreeExpression.html}", note=[Accessed: 17-November-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_treeexpression, organization={Wolfram Research}, title={TreeExpression}, year={2022}, url={https://reference.wolfram.com/language/ref/TreeExpression.html}, note=[Accessed: 17-November-2024 ]}