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.
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 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 all close allBasic Examples (4)
Extract the second child of a tree:
TreeExtract[[image], {2}]Extract a subtree at any position:
TreeExtract[[image], {3, 3}]Extract a list of subtrees at several positions:
TreeExtract[[image], {{1}, {2, 1}, {3, 3, 1}}]Perform independent extractions at consecutive levels:
TreeExtract[[image], {All, 2}]Use the operator form of TreeExtract:
TreeExtract[2][[image]]Scope (23)
Part Specifications (9)
TreeExtract[[image], 1]Extract the second child from the end:
TreeExtract[[image], -2]TreeExtract[[image], All]Extract the children at odd positions:
TreeExtract[[image], 1 ;; -1 ;; 2]Use Key to specify position:
TreeExtract[[image], Key["b"]]For string keys, Key is not needed:
TreeExtract[[image], "b"]Extract subtrees on a range of levels:
TreeExtract[[image], TreeLevel[3]]Extract subtrees matching a data pattern:
TreeExtract[[image], TreeCases[_ ? OddQ]]Extract subtrees with exactly two children:
TreeExtract[[image], TreeSelect[Length[TreeChildren[#]] == 2&]]Extract using different specifications:
TreeExtract[[image], {TreeLevel[{2}], TreeCases[_ ? OddQ]}]Single versus Multiple Positions (3)
Extract a single subtree from a tree:
TreeExtract[[image], {3}]Extract several subtrees from a tree:
TreeExtract[[image], {{1}, {4}, {3}}]Extract the subtree at position {1,3}:
tree = [image];TreeExtract[tree, {1, 3}]Extract the first and third subtrees of a tree:
TreeExtract[tree, {{1}, {3}}]Extract a length-one list of positions from the tree:
TreeExtract[tree, {{1, 3}}]Extract several subtrees of the tree:
TreeExtract[tree, {{1, 2}, {3, 3}, {2, 1}}]Forms Involving All (2)
Extract the children of the second child:
tree = [image];TreeExtract[tree, {2}]TreeExtract[tree, {2, All}]Extract the second child of each child:
TreeExtract[tree, {All, 2}]Extract the first and third child of each child:
TreeExtract[tree, {All, {1, 3}}]Extract the first and third child of each child separately:
TreeExtract[tree, {{All, 1}, {All, 3}}]Extracting using All preserves the data at that level:
TreeExtract[[image], {All, 1}]Associations (5)
Extract a subtree from a tree containing an association by key:
TreeExtract[[image], {Key[expr]}]If the key is a string, the Key wrapper is optional:
TreeExtract[[image], {"str"}]Extract a subtree from a tree containing an association by key:
TreeExtract[[image], {Key[1]}]Extract a subtree from a tree containing an association by position:
TreeExtract[[image], {1}]Extract several subtrees from a tree containing an association:
TreeExtract[[image], {{1}, {Key[1]}}]Extract a nested subtree from a tree containing an association:
TreeExtract[[image], {Key[3], 1}]Extract several nested subtrees from a tree containing an association:
TreeExtract[[image], {{Key[3], 1}, {3, 1}}]Spans and Sublists (4)
Extract the third through fifth children of a list:
TreeExtract[[image], {3 ;; 5}]Extract the children at odd positions:
TreeExtract[[image], {1 ;; -1 ;; 2}]Extract the first three children from each child:
TreeExtract[[image], {All, 1 ;; 3}]Extract the first and last child from each child:
TreeExtract[[image], {All, {1, -1}}]Extract the first child of the second through fourth children:
tree = [image];TreeExtract[tree, {2 ;; 4, 1}]Extract the second child of the first, third and fourth children:
TreeExtract[tree, {{1, 3, 4}, 2}]Extract the first, second and fifth children of the last three children:
TreeExtract[tree, {-3 ;; -1, {1, 2, 5}}]Extract the first and fifth children of each child:
TreeExtract[tree, {All, {1, 5}}]Properties & Relations (10)
Construct a tree from the heads in an expression:
ExpressionTree[1[2[3, 4], 5[6, 7]], "Heads"]TreeExtract extracts a subtree of a tree:
TreeExtract[%, {1}]This corresponds to extracting a subexpression of an expression:
TreeExpression[%, "Heads"]Extract can extract subexpressions directly:
Extract[1[2[3, 4], 5[6, 7]], {1}]If pos is not a list, pos and {pos} are equivalent specifications:
tree = [image];TreeExtract[tree, 3] === TreeExtract[tree, {3}]TreeExtract[tree, 3 ;; 5] === TreeExtract[tree, {3 ;; 5}]For a list of integers {i,j,…}, TreeExtract[tree,{i,j,…}] is equivalent to …@TreeExtract[j]@TreeExtract[i]@tree:
TreeExtract[[image], {2, 3}] === TreeExtract[3]@TreeExtract[2]@[image]TreeExtract[tree,{{i1,j1,…},{i2,j2,…},…}] is equivalent to {TreeExtract[tree,{i1,j1,…}],TreeExtract[tree,{i2,j2,…}],…}:
tree = [image];TreeExtract[tree, {{1 ;; 2, 1}, {2 ;; 3, 2}}] === {TreeExtract[tree, {1 ;; 2, 1}], TreeExtract[tree, {2 ;; 3, 2}]}This extends to the empty list:
TreeExtract[tree, {}] === {}Extracting position {} extracts the whole tree:
TreeExtract[[image], {{}}]TreeExtract extracts subtrees repeatedly if a position is mentioned repeatedly:
TreeExtract[[image], {{1}, {3}, {1}}]TreeExtract can use the lists of positions returned by TreePosition:
tree = NestTree[Range[3# - 1, 3# + 1]&, 1, 2]TreePosition[tree, _ ? PrimeQ]TreeExtract[tree, %]In TreeExtract[tree,{pos1,pos2,…},h], h wraps the result of extracting the subtrees at each of the posi:
tree = [image];TreeExtract[tree, {{1, 2}, {3, 3}, {2, 1}}, h]In TreeExtract[tree,pos,h], h wraps the result of extracting the subtree at position pos:
TreeExtract[tree, {1, 2}, h]Specifications that extract multiple parts at one level retain the data at that level:
tree = [image];TreeExtract[tree, {All, 1}]TreeExtract[tree, {1 ;; 3, 1}]TreeExtract[tree, {{2, 4}, 1}]TreeExtract[tree,{p1,p2,…}] treats {p1,p2,…} as a list of individual position specifications if all the pi are lists:
tree = [image];For {{1,2},{3,4}}, the subtrees at positions {1,2} and {3,4} are extracted:
TreeExtract[tree, {{1, 2}, {3, 4}}]If any pi is not a list, {p1,p2,…} is treated as a list of part specifications:
TreeExtract[tree, {{1, 2}, 3}]TreeExtract[tree, {1, {3, 4}}]For {{{1,2},{3,4}}}, parts 3 and 4 of parts 1 and 2 are extracted:
TreeExtract[tree, {{{1, 2}, {3, 4}}}]Possible Issues (2)
TreeExtract[tree,{}] returns an empty list of subtrees because the list of positions is empty:
TreeExtract[[image], {}]TreeExtract[tree,{{}}] extracts the subtree at position {}, which is the whole tree:
TreeExtract[[image], {{}}]A list of lists in the second argument is always interpreted as a list of position specifications:
tree = [image];TreeExtract[tree, {{1, 2}}]As a result, specifications that are equivalent in Part may be treated differently by TreeExtract:
Part[TreeChildren[tree], {1, 2}] === Part[TreeChildren[tree], 1 ;; 2]TreeExtract[tree, {{1, 2}}] === TreeExtract[tree, {1 ;; 2}]Use a list of lists and remove the extra list at the end if consistency between Part and TreeExtract is needed:
First@TreeExtract[tree, {{{1, 2}}}] === First@TreeExtract[tree, {{1 ;; 2}}]Related Guides
Text
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.
APA
Wolfram Language. (2021). TreeExtract. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/TreeExtract.html
BibTeX
@misc{reference.wolfram_2026_treeextract, author="Wolfram Research", title="{TreeExtract}", year="2022", howpublished="\url{https://reference.wolfram.com/language/ref/TreeExtract.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_treeextract, organization={Wolfram Research}, title={TreeExtract}, year={2022}, url={https://reference.wolfram.com/language/ref/TreeExtract.html}, note=[Accessed: 13-June-2026]}