TreeExtract

TreeExtract[tree,pos]

extracts the subtree of tree at the position specified by pos.

TreeExtract[tree,{pos1,pos2,}]

extracts a list of subtrees of tree.

TreeExtract[tree,pos,h]

extracts subtrees of tree, applying h to each subtree.

TreeExtract[pos]

represents an operator form of TreeExtract that can be applied to a tree.

Details

  • TreeExtract allows extraction of properties of subtrees or parts of the data at any position in a Tree object.
  • TreeExtract[tree,{i,j,}] extracts the subtree at position {i,j,}.
  • TreeExtract can use the position specifications returned by TreePosition as well as extended forms as used in functions such as TreeInsert and TreeReplacePart. »
  • An individual position specification pos can be given as {part1,part2,}, where part specifications parti include:
  • ithe i^(th) child
    -ithe i^(th) child from the end
    {i1,i2,}the list of children with part numbers i1, i2,
    Allall children
    m;;nchildren m through n
    TreeLevel[levelspec]the subtrees on levels specified by levelspec
    TreeCases[pattern]the subtrees with data matching pattern
    TreeSelect[crit]the subtrees for which crit gives True
    TreeDatathe data in the tree
  • In TreeExtract[tree,{p1,p2,}], {p1,p2,} is interpreted as a list of individual position specifications if all the pi are lists. Otherwise, {p1,p2,} will be taken to be an individual position specification. »
  • In TreeExtract position specifications, part 0 is equivalent to TreeData. »
  • TreeExtract[pos][tree] is equivalent to TreeExtract[tree,pos].

Examples

open allclose all

Basic Examples  (4)

Extract the second child of a tree:

Extract a subtree at any position:

Extract multiple subtrees:

Extract the data from multiple subtrees:

Use the operator form of TreeExtract:

Extract using different specifications:

Scope  (16)

Part Specifications  (8)

Extract the first child:

Extract the second child from the end:

Extract all children:

Extract the children at odd positions:

Extract subtrees on a range of levels:

Extract subtrees matching a data pattern:

Extract subtrees with exactly two children:

Extract the data of a tree element:

Single versus Multiple Positions  (3)

Extract a single subtree from a tree:

Extract several subtrees from a tree:

Extract the subtree at position {1,3}:

Extract the first and third subtrees of a tree:

Extract a length-one list of positions from the tree:

Extract several subtrees of the tree:

Forms Involving All  (1)

Extract the children of the second child:

Extract the second child of each child:

Extract the first and third child of each child as a matrix:

Extract the first and third child of each child separately, effectively creating a matrix:

Spans and Sublists  (4)

Extract the third through fifth children of a list:

Extract the children at odd positions:

Extract the first three children from each child:

Extract the first and last child from each child:

Extract the first child of the second through fourth children:

Extract the second child of the first, third and fourth children:

Extract the first, second and fifth children of the last three children:

Extract the first and fifth children of each child:

Properties & Relations  (13)

Construct a tree from the heads in an expression:

TreeExtract extracts a subtree of a tree:

This corresponds to extracting a subexpression of an expression:

Extract can extract subexpressions directly:

If pos is not a list, pos and {pos} are equivalent specifications:

For a list of integers {i,j,}, TreeExtract[tree,{i,j,}] is equivalent to @TreeExtract[j]@TreeExtract[i]@tree:

TreeExtract[tree,{{i1,j1,},{i2,j2,},}] is equivalent to {TreeExtract[tree,{i1,j1,}],TreeExtract[tree,{i2,j2,}],}:

This extends to the empty list:

Extracting position {} extracts the whole tree:

TreeExtract extracts subtrees repeatedly if a position is mentioned repeatedly:

TreeExtract can use the lists of positions returned by TreePosition:

Take a tree containing another tree as data:

Extract a subtree from that data tree:

Equivalently, use 0 to refer to the tree data:

Extract a subexpression of the data:

TreeExtract[tree,0] is equivalent to TreeData[tree]:

TreeExtract[tree,All] is equivalent to TreeChildren[tree]:

In TreeExtract[tree,{pos1,pos2,},h], h wraps the result of extracting the subtrees at each of the posi:

In TreeExtract[tree,pos,h], h wraps the result of extracting the subtree at position pos:

Extract using All preserves the head at that level:

TreeExtract using All always returns a list:

TreeExtract[tree,{p1,p2,}] treats {p1,p2,} as a list of individual position specifications if all the pi are lists:

For {{1,2},{3,4}}, the subtrees at positions {1,2} and {3,4} are extracted:

If any pi is not a list, {p1,p2,} is treated as a list of part specifications:

For {{{1,2},{3,4}}}, parts 3 and 4 of parts 1 and 2 are extracted:

Possible Issues  (2)

TreeExtract[tree,{}] returns an empty list of subtrees because the list of positions is empty:

TreeExtract[tree,{{}}] extracts the subtree at position {}, which is the whole tree:

A list of lists in the second argument is always interpreted as a list of position specifications:

As a result, specifications that are equivalent in Part may be treated differently by TreeExtract:

Use a list of lists and remove the extra list at the end if consistency between Part and TreeExtract is needed:

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

Text

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

CMS

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

APA

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

BibTeX

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

BibLaTeX

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