WOLFRAM

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.

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

Details

  • TreeExtract allows extraction of properties of subtrees at any position in a Tree object.
  • For integers i, j, , 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
    "key"the child whose key is "key"
    Key[k]the child with an arbitrary key k
    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
  • 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. »
  • Specifications that extract multiple parts at one level retain the data at that level. »
  • TreeExtract works on trees containing an Association of subtrees, using the same specification for keys as in Part. »
  • TreeExtract[pos][tree] is equivalent to TreeExtract[tree,pos].

Examples

open allclose all

Basic Examples  (4)Summary of the most common use cases

Extract the second child of a tree:

Out[2]=2

Extract a subtree at any position:

Out[1]=1

Extract a list of subtrees at several positions:

Out[2]=2

Perform independent extractions at consecutive levels:

Out[1]=1

Use the operator form of TreeExtract:

Out[1]=1

Scope  (23)Survey of the scope of standard use cases

Part Specifications  (9)

Extract the first child:

Out[2]=2

Extract the second child from the end:

Out[1]=1

Extract all children:

Out[1]=1

Extract the children at odd positions:

Out[1]=1

Use Key to specify position:

Out[1]=1

For string keys, Key is not needed:

Out[2]=2

Extract subtrees on a range of levels:

Out[1]=1

Extract subtrees matching a data pattern:

Out[1]=1

Extract subtrees with exactly two children:

Out[1]=1

Extract using different specifications:

Out[1]=1

Single versus Multiple Positions  (3)

Extract a single subtree from a tree:

Out[2]=2

Extract several subtrees from a tree:

Out[1]=1

Extract the subtree at position {1,3}:

Out[2]=2

Extract the first and third subtrees of a tree:

Out[3]=3

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

Out[4]=4

Extract several subtrees of the tree:

Out[5]=5

Forms Involving All  (2)

Extract the children of the second child:

Out[4]=4

Equivalently:

Out[5]=5

Extract the second child of each child:

Out[6]=6

Extract the first and third child of each child:

Out[7]=7

Extract the first and third child of each child separately:

Out[8]=8

Extracting using All preserves the data at that level:

Out[1]=1

Associations  (5)

Extract a subtree from a tree containing an association by key:

Out[1]=1

If the key is a string, the Key wrapper is optional:

Out[2]=2

Extract a subtree from a tree containing an association by key:

Out[1]=1

Extract a subtree from a tree containing an association by position:

Out[2]=2

Extract several subtrees from a tree containing an association:

Out[1]=1

Extract a nested subtree from a tree containing an association:

Out[1]=1

Extract several nested subtrees from a tree containing an association:

Out[1]=1

Spans and Sublists  (4)

Extract the third through fifth children of a list:

Out[2]=2

Extract the children at odd positions:

Out[1]=1

Extract the first three children from each child:

Out[1]=1

Extract the first and last child from each child:

Out[2]=2

Extract the first child of the second through fourth children:

Out[2]=2

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

Out[3]=3

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

Out[4]=4

Extract the first and fifth children of each child:

Out[5]=5

Properties & Relations  (10)Properties of the function, and connections to other functions

Construct a tree from the heads in an expression:

Out[1]=1

TreeExtract extracts a subtree of a tree:

Out[2]=2

This corresponds to extracting a subexpression of an expression:

Out[3]=3

Extract can extract subexpressions directly:

Out[4]=4

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

Out[2]=2
Out[3]=3

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

Out[1]=1

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

Out[2]=2

This extends to the empty list:

Out[3]=3

Extracting position {} extracts the whole tree:

Out[1]=1

TreeExtract extracts subtrees repeatedly if a position is mentioned repeatedly:

Out[1]=1

TreeExtract can use the lists of positions returned by TreePosition:

Out[1]=1
Out[2]=2
Out[3]=3

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

Out[2]=2

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

Out[3]=3

Specifications that extract multiple parts at one level retain the data at that level:

Out[2]=2
Out[3]=3
Out[4]=4

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:

Out[2]=2

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

Out[3]=3
Out[4]=4

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

Out[5]=5

Possible Issues  (2)Common pitfalls and unexpected behavior

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

Out[2]=2

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

Out[4]=4

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

Out[2]=2

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

Out[3]=3
Out[4]=4

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

Out[5]=5
Wolfram Research (2021), TreeExtract, Wolfram Language function, https://reference.wolfram.com/language/ref/TreeExtract.html (updated 2022).
Wolfram Research (2021), TreeExtract, Wolfram Language function, https://reference.wolfram.com/language/ref/TreeExtract.html (updated 2022).

Text

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

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

CMS

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

Wolfram Language. 2021. "TreeExtract." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2022. 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

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

BibTeX

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

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

BibLaTeX

@online{reference.wolfram_2025_treeextract, organization={Wolfram Research}, title={TreeExtract}, year={2022}, url={https://reference.wolfram.com/language/ref/TreeExtract.html}, note=[Accessed: 04-June-2025 ]}

@online{reference.wolfram_2025_treeextract, organization={Wolfram Research}, title={TreeExtract}, year={2022}, url={https://reference.wolfram.com/language/ref/TreeExtract.html}, note=[Accessed: 04-June-2025 ]}