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.
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 all close allBasic Examples (4)
Find the positions of the subtrees whose data is an even number:
TreePosition[[image], _ ? EvenQ]Find the positions of the inner subtrees whose data is an even number:
TreePosition[[image], _ ? EvenQ, {0, -2}]Find the first three positions of prime numbers:
TreePosition[[image], _ ? PrimeQ, All, 3]Use the operator form of TreePosition:
TreePosition[9][[image]]Scope (5)
Find the positions of the subtrees whose data is an odd integer:
TreePosition[[image], _ ? OddQ]Find the positions of the subtrees at levels 1 and 2 whose data is an odd integer:
TreePosition[[image], _ ? OddQ, 2]Find the positions of the leaf subtrees whose data is an odd integer:
TreePosition[[image], _ ? OddQ, {-1}]Find the positions of the inner subtrees whose data is an odd integer:
TreePosition[[image], _ ? OddQ, {0, -2}]Find the first four positions of the subtrees whose data is an odd integer:
TreePosition[[image], _ ? OddQ, All, 4]Options (3)
TreeTraversalOrder (3)
By default, subtrees are visited in a depth-first order, with parents visited after their children:
TreePosition[[image], _, TreeTraversalOrder -> "DepthFirst"]TreePosition[[image], _, TreeTraversalOrder -> {"DepthFirst", "TopDown"}]This lists the positions in lexicographic order:
LexicographicSort[%]Visit subtrees in a breadth-first order, with nodes on the same level from the root visited before the nodes on the next level:
TreePosition[[image], _, TreeTraversalOrder -> "BreadthFirst"]This lists the positions in canonical order:
Sort[%]Visit subtrees in a leaves-first order, with nodes on the same level from the leaves visited before the nodes on the next level:
TreePosition[[image], _, TreeTraversalOrder -> "LeavesFirst"]Applications (1)
Properties & Relations (4)
TreeCases extracts the positions given by TreePosition:
tree = [image];TreeCases[tree, _ ? PrimeQ]TreePosition[tree, _ ? PrimeQ]TreeExtract[tree, %]TreeCount gives the number of matching positions given by TreePosition:
TreePosition[[image], x]TreeCount[[image], x]Position[expr,pattern,levelspec,HeadsFalse] is equivalent to TreePosition[ExpressionTree[expr,"Subexpressions"],pattern,levelspec]:
Position[{{1, 4, a, 0}, {b, 3, 2, 2}, {c, c, 5, 5}}, _, All, Heads -> False]ExpressionTree[{{1, 4, a, 0}, {b, 3, 2, 2}, {c, c, 5, 5}}, "Subexpressions"]TreePosition[%, _, All]Position[expr,pattern,levelspec] is equivalent to TreePosition[ExpressionTree[expr,"Subexpressions",HeadsTrue],pattern,levelspec]-1 for positive levels:
Position[{1, 2, f[a, b]}, _, {0, Infinity}]ExpressionTree[{1, 2, f[a, b]}, "Subexpressions", Heads -> True]TreePosition[%, _, {0, Infinity}]% - 1Possible Issues (1)
TreePosition by default starts at level 1, so does not visit the root:
TreePosition[[image], _, 1]TreePosition[[image], _, {0, 1}]Related Guides
Text
Wolfram Research (2021), TreePosition, Wolfram Language function, https://reference.wolfram.com/language/ref/TreePosition.html (updated 2021).
CMS
Wolfram Language. 2021. "TreePosition." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2021. https://reference.wolfram.com/language/ref/TreePosition.html.
APA
Wolfram Language. (2021). TreePosition. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/TreePosition.html
BibTeX
@misc{reference.wolfram_2026_treeposition, author="Wolfram Research", title="{TreePosition}", year="2021", howpublished="\url{https://reference.wolfram.com/language/ref/TreePosition.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_treeposition, organization={Wolfram Research}, title={TreePosition}, year={2021}, url={https://reference.wolfram.com/language/ref/TreePosition.html}, note=[Accessed: 13-June-2026]}