# 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 • In TreeExtract[tree,pos], tree must be a TreeQ object.
• The position specifications used by TreeExtract have the same form as those returned by TreePosition.
• The following positions can be used for pos:
•  i the i child -i the i child from the end {i1,i2,…} the list of children at positions i1, i2, … m;;n children 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 TreeData the data in the tree
• TreeExtract[tree,All] is equivalent to TreeExtract[tree,TreeLevel[{1}]].
• In TreeExtract position specifications, position 0 is equivalent to TreeData.
• TreeExtract[pos][tree] is equivalent to TreeExtract[tree,pos].

# Examples

open allclose all

## Basic Examples(3)

Extract the second child of a tree:

Extract a subtree:

Extract multiple subtrees:

## Scope(9)

Extract the first child:

Extract the second to last child:

Extract a span of children:

Extract using different specifications:

Use the operator form of TreeExtract:

Extract subtrees by level:

Extract subtrees matching a data pattern:

Extract subtrees with exactly two children:

Take a tree containing another tree as data:

Extract a subtree from that data tree:

Alternatively, use 0 to refer to the tree data:

## Properties & Relations(1)

Extract the subtrees given by TreePosition:

## Possible Issues(1)

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: