TreeExtract
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:
-
i the i child
-i the i child from the end
{i1,i2,…} the list of children with part numbers i1, i2, … All all children m;;n children 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 allBasic Examples (4)Summary of the most common use cases
Extract the second child of a tree:

https://wolfram.com/xid/0b8c08mzsm-1bllp7

Extract a subtree at any position:

https://wolfram.com/xid/0b8c08mzsm-d4nnz5

Extract a list of subtrees at several positions:

https://wolfram.com/xid/0b8c08mzsm-lwed0q

Perform independent extractions at consecutive levels:

https://wolfram.com/xid/0b8c08mzsm-kntoau

Use the operator form of TreeExtract:

https://wolfram.com/xid/0b8c08mzsm-eg2bvr

Scope (23)Survey of the scope of standard use cases
Part Specifications (9)

https://wolfram.com/xid/0b8c08mzsm-i41ao1

Extract the second child from the end:

https://wolfram.com/xid/0b8c08mzsm-8ipyvr


https://wolfram.com/xid/0b8c08mzsm-8wbmnd

Extract the children at odd positions:

https://wolfram.com/xid/0b8c08mzsm-7wjjil

Use Key to specify position:

https://wolfram.com/xid/0b8c08mzsm-ltupph

For string keys, Key is not needed:

https://wolfram.com/xid/0b8c08mzsm-3w4yi5

Extract subtrees on a range of levels:

https://wolfram.com/xid/0b8c08mzsm-5u34fh

Extract subtrees matching a data pattern:

https://wolfram.com/xid/0b8c08mzsm-tss9wb

Extract subtrees with exactly two children:

https://wolfram.com/xid/0b8c08mzsm-deef30

Extract using different specifications:

https://wolfram.com/xid/0b8c08mzsm-5fbpo1

Single versus Multiple Positions (3)
Extract a single subtree from a tree:

https://wolfram.com/xid/0b8c08mzsm-x1adqg

Extract several subtrees from a tree:

https://wolfram.com/xid/0b8c08mzsm-m10cmf

Extract the subtree at position {1,3}:

https://wolfram.com/xid/0b8c08mzsm-rd8lz0

https://wolfram.com/xid/0b8c08mzsm-e0xkkk

Extract the first and third subtrees of a tree:

https://wolfram.com/xid/0b8c08mzsm-ud4ydg

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

https://wolfram.com/xid/0b8c08mzsm-dinhvj

Extract several subtrees of the tree:

https://wolfram.com/xid/0b8c08mzsm-8g5ojz

Forms Involving All (2)
Extract the children of the second child:

https://wolfram.com/xid/0b8c08mzsm-og6bd1

https://wolfram.com/xid/0b8c08mzsm-08kqor


https://wolfram.com/xid/0b8c08mzsm-5p7bgq

Extract the second child of each child:

https://wolfram.com/xid/0b8c08mzsm-qnijzk

Extract the first and third child of each child:

https://wolfram.com/xid/0b8c08mzsm-pqzvc4

Extract the first and third child of each child separately:

https://wolfram.com/xid/0b8c08mzsm-v09j8h

Extracting using All preserves the data at that level:

https://wolfram.com/xid/0b8c08mzsm-wvyxdv

Associations (5)
Extract a subtree from a tree containing an association by key:

https://wolfram.com/xid/0b8c08mzsm-7a6fuq

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

https://wolfram.com/xid/0b8c08mzsm-yyfies

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

https://wolfram.com/xid/0b8c08mzsm-7v3az9

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

https://wolfram.com/xid/0b8c08mzsm-38ekwa

Extract several subtrees from a tree containing an association:

https://wolfram.com/xid/0b8c08mzsm-n498q5

Extract a nested subtree from a tree containing an association:

https://wolfram.com/xid/0b8c08mzsm-trlr2s

Extract several nested subtrees from a tree containing an association:

https://wolfram.com/xid/0b8c08mzsm-7ed6nf

Spans and Sublists (4)
Extract the third through fifth children of a list:

https://wolfram.com/xid/0b8c08mzsm-hmr7l2

Extract the children at odd positions:

https://wolfram.com/xid/0b8c08mzsm-758f8y

Extract the first three children from each child:

https://wolfram.com/xid/0b8c08mzsm-zkdzib

Extract the first and last child from each child:

https://wolfram.com/xid/0b8c08mzsm-e25f5w

Extract the first child of the second through fourth children:

https://wolfram.com/xid/0b8c08mzsm-d6h1o5

https://wolfram.com/xid/0b8c08mzsm-e9gl4f

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

https://wolfram.com/xid/0b8c08mzsm-6fuaww

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

https://wolfram.com/xid/0b8c08mzsm-osybmb

Extract the first and fifth children of each child:

https://wolfram.com/xid/0b8c08mzsm-3k1dgu

Properties & Relations (10)Properties of the function, and connections to other functions
Construct a tree from the heads in an expression:

https://wolfram.com/xid/0b8c08mzsm-7bni4y

TreeExtract extracts a subtree of a tree:

https://wolfram.com/xid/0b8c08mzsm-kaxijc

This corresponds to extracting a subexpression of an expression:

https://wolfram.com/xid/0b8c08mzsm-d2p7k6

Extract can extract subexpressions directly:

https://wolfram.com/xid/0b8c08mzsm-ir5adp

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

https://wolfram.com/xid/0b8c08mzsm-l4o1qg

https://wolfram.com/xid/0b8c08mzsm-ohdhfy


https://wolfram.com/xid/0b8c08mzsm-ure7nm

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

https://wolfram.com/xid/0b8c08mzsm-8d74iy

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

https://wolfram.com/xid/0b8c08mzsm-v4weu8

https://wolfram.com/xid/0b8c08mzsm-ca36e2

This extends to the empty list:

https://wolfram.com/xid/0b8c08mzsm-oy0vcn

Extracting position {} extracts the whole tree:

https://wolfram.com/xid/0b8c08mzsm-686ovp

TreeExtract extracts subtrees repeatedly if a position is mentioned repeatedly:

https://wolfram.com/xid/0b8c08mzsm-zm6chm

TreeExtract can use the lists of positions returned by TreePosition:

https://wolfram.com/xid/0b8c08mzsm-gvo281


https://wolfram.com/xid/0b8c08mzsm-rcpy5z


https://wolfram.com/xid/0b8c08mzsm-8d7r26

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

https://wolfram.com/xid/0b8c08mzsm-rp2xoz

https://wolfram.com/xid/0b8c08mzsm-w8q6me

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

https://wolfram.com/xid/0b8c08mzsm-tcjl78

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

https://wolfram.com/xid/0b8c08mzsm-31rpzx

https://wolfram.com/xid/0b8c08mzsm-0dq3x


https://wolfram.com/xid/0b8c08mzsm-1sb4z8


https://wolfram.com/xid/0b8c08mzsm-jp9zxr

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

https://wolfram.com/xid/0b8c08mzsm-otyc2w
For {{1,2},{3,4}}, the subtrees at positions {1,2} and {3,4} are extracted:

https://wolfram.com/xid/0b8c08mzsm-0mu3zk

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

https://wolfram.com/xid/0b8c08mzsm-srkutx


https://wolfram.com/xid/0b8c08mzsm-469z01

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

https://wolfram.com/xid/0b8c08mzsm-5m41oj

Possible Issues (2)Common pitfalls and unexpected behavior
TreeExtract[tree,{}] returns an empty list of subtrees because the list of positions is empty:

https://wolfram.com/xid/0b8c08mzsm-jov8lq

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

https://wolfram.com/xid/0b8c08mzsm-m0kroy

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

https://wolfram.com/xid/0b8c08mzsm-ejwcun

https://wolfram.com/xid/0b8c08mzsm-pulhcd

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

https://wolfram.com/xid/0b8c08mzsm-lb4vyf


https://wolfram.com/xid/0b8c08mzsm-uersop

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

https://wolfram.com/xid/0b8c08mzsm-nz4j5f

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
]}
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
]}