TreeCases[tree,pattern]
gives a list of subtrees of tree with data matching pattern.
TreeCases[tree,pattern,levelspec]
gives a list of all subtrees of tree on levels specified by levelspec with data that matches the pattern.
TreeCases[tree,pattern,levelspec,n]
gives the first n subtrees in tree with data that matches the pattern.
TreeCases
TreeCases[tree,pattern]
gives a list of subtrees of tree with data matching pattern.
TreeCases[tree,pattern,levelspec]
gives a list of all subtrees of tree on levels specified by levelspec with data that matches the pattern.
TreeCases[tree,pattern,levelspec,n]
gives the first n subtrees in tree with data that matches the pattern.
Details and Options
- TreeCases lists the subtrees with data matching any pattern on any range of levels in a Tree object. The list of matches can be given in many different orders, including depth-first and breadth-first traversals.
- TreeCases[tree,pattern] tests all the subtrees of tree in turn to try to find ones with data that matches pattern.
- TreeCases 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 TreeCases is {0,Infinity}.
- TreeCases traverses subtrees in a left-to-right, depth-first order, with children visited before their parents.
- TreeCases[…,TreeTraversalOrderorder] allows visiting subtrees in different orders, such as depth-first and breadth-first traversals.
- TreeCases[pattern][tree] is equivalent to TreeCases[tree,pattern].
Examples
open all close allBasic Examples (4)
Find the subtrees whose data is an even number:
TreeCases[[image], _ ? EvenQ]Find the inner subtrees whose data is an even number:
TreeCases[[image], _ ? EvenQ, {0, -2}]Find the first three subtrees whose data is a prime number:
TreeCases[[image], _ ? PrimeQ, All, 3]Use the operator form of TreeCases:
TreeCases[_ ? PrimeQ][[image]]Scope (6)
Find the subtrees whose data is an odd integer:
TreeCases[[image], _ ? OddQ]Find the subtrees at levels 1 and 2 whose data is an odd integer:
TreeCases[[image], _ ? OddQ, 2]Find the leaf subtrees whose data is an odd integer:
TreeCases[[image], _ ? OddQ, {-1}]Find the inner subtrees whose data is an odd integer:
TreeCases[[image], _ ? OddQ, {0, -2}]Find the first four subtrees whose data is an odd integer:
TreeCases[[image], _ ? OddQ, All, 4]Use TreeCases as an operator form in a tree position specification:
TreeExtract[[image], {TreeCases[_ ? OddQ]}]Options (3)
TreeTraversalOrder (3)
By default, subtrees are listed in a depth-first order, with parents visited after their children:
TreeCases[[image], _, TreeTraversalOrder -> "DepthFirst"]Specify a top-down, right-to-left variant:
TreeCases[[image], _, TreeTraversalOrder -> {"DepthFirst", "TopDown", "RightLeft"}]List subtrees in a breadth-first order, with nodes on the same level from the root visited before the nodes on the next level:
TreeCases[[image], _, TreeTraversalOrder -> "BreadthFirst"]List subtrees in a leaves-first order, with nodes on the same level from the leaves visited before the nodes on the next level:
TreeCases[[image], _, TreeTraversalOrder -> "LeavesFirst"]Properties & Relations (5)
TreeCases extracts the positions given by TreePosition:
tree = [image];TreeCases[tree, _ ? PrimeQ]TreePosition[tree, _ ? PrimeQ]TreeExtract[tree, %]TreeCount gives the number of matching subtrees given by TreeCases:
TreeCases[[image], x]TreeCount[[image], x]TreeCases[tree,pattern,Infinity] is equivalent to TreeChildren@TreeExtract[tree,TreeCases[pattern]]:
TreeCases[[image], _ ? EvenQ, Infinity]% === TreeChildren@TreeExtract[[image], TreeCases[_ ? EvenQ]]Cases[expr,pattern,levelspec] is equivalent to TreeData/@TreeCases[ExpressionTree[expr,"Subexpressions"],pattern,levelspec]:
Cases[{{1, 4, a, 0}, {b, 3, 2, 2}, {c, c, 5, 5}}, _Integer, 2]ExpressionTree[{{1, 4, a, 0}, {b, 3, 2, 2}, {c, c, 5, 5}}, "Subexpressions"]TreeData /@ TreeCases[%, _Integer, 2]Cases[expr,pattern,levelspec,HeadsTrue] is equivalent to TreeData/@TreeCases[ExpressionTree[expr,"Subexpressions",HeadsTrue],pattern,levelspec] for positive levels:
Cases[{1, 2, f[a, b]}, _, {0, Infinity}, Heads -> True]ExpressionTree[{1, 2, f[a, b]}, "Subexpressions", Heads -> True]TreeData /@ TreeCases[%, _, {0, Infinity}]Possible Issues (1)
TreeCases by default starts at level 1, so does not visit the root:
TreeCases[[image], _, 1]TreeCases[[image], _, {0, 1}]See Also
TreePosition TreeCount TreeLevel TreeSelect TreeMap Tree Cases
Related Guides
Text
Wolfram Research (2021), TreeCases, Wolfram Language function, https://reference.wolfram.com/language/ref/TreeCases.html (updated 2021).
CMS
Wolfram Language. 2021. "TreeCases." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2021. https://reference.wolfram.com/language/ref/TreeCases.html.
APA
Wolfram Language. (2021). TreeCases. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/TreeCases.html
BibTeX
@misc{reference.wolfram_2026_treecases, author="Wolfram Research", title="{TreeCases}", year="2021", howpublished="\url{https://reference.wolfram.com/language/ref/TreeCases.html}", note=[Accessed: 15-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_treecases, organization={Wolfram Research}, title={TreeCases}, year={2021}, url={https://reference.wolfram.com/language/ref/TreeCases.html}, note=[Accessed: 15-June-2026]}