# TreePosition TreePosition[tree,pattern]

gives a list of the positions of subtrees of tree whose data matches pattern.

TreePosition[tree,pattern,levelspec]

finds only matches that appear on levels of tree specified by levelspec.

TreePosition[tree,pattern,levelspec,n]

gives the positions of the first n matches found.

TreePosition[pattern]

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

# Details and Options • TreePosition lists the positions of data matching any pattern on any range of levels in a Tree object. The list of matching positions can be given in many different orders, including depth-first and breadth-first traversals.
• TreePosition[tree,pattern] tests all the subtrees of tree in turn to try to find ones with data that match pattern.
• TreePosition returns a list of positions in a form suitable for use in TreeExtract, TreeReplacePart and TreeDelete.
• A part specification {} returned by TreePosition represents the whole of tree.
• TreePosition uses standard level specifications as in TreeLevel:
•  n levels 1 through n All levels 0 through Infinity Infinity levels 1 through Infinity {n} level n only {n1,n2} levels n1 through n2
• The default value for levelspec in TreePosition is {0,Infinity}.
• TreePosition traverses subtrees in a left-to-right, depth-first order, yielding lists of indices in lexicographic order.
• TreePosition[,TreeTraversalOrderorder] allows visiting subtrees in different orders, such as depth-first and breadth-first traversals.
• TreePosition[pattern][tree] is equivalent to TreePosition[tree,pattern].

# Examples

open allclose all

## Basic Examples(4)

Find the positions of the subtrees whose data is an even number:

Find the positions of the inner subtrees whose data is an even number:

Find the first three positions of prime numbers:

Use the operator form of TreePosition:

## Scope(5)

Find the positions of the subtrees whose data is an odd integer:

Find the positions of the subtrees at levels 1 and 2 whose data is an odd integer:

Find the positions of the leaf subtrees whose data is an odd integer:

Find the positions of the inner subtrees whose data is an odd integer:

Find the first four positions of the subtrees whose data is an odd integer:

## Options(3)

### TreeTraversalOrder(3)

By default, subtrees are visited in a depth-first order, with parents visited after their children:

Specify a top-down variant:

This lists the positions in lexicographic order:

Visit subtrees in a breadth-first order, with nodes on the same level from the root visited before the nodes on the next level:

This lists the positions in canonical order:

Visit subtrees in a leaves-first order, with nodes on the same level from the leaves visited before the nodes on the next level:

## Applications(1)

Compute the level with the most nodes:

## Properties & Relations(4)

TreeCases extracts the positions given by TreePosition:

TreeCount gives the number of matching positions given by TreePosition: