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 allclose allBasic Examples (4)
Find the subtrees whose data is an even number:
Find the inner subtrees whose data is an even number:
Find the first three subtrees whose data is a prime number:
Use the operator form of TreeCases:
Scope (6)
Find the subtrees whose data is an odd integer:
Find the subtrees at levels 1 and 2 whose data is an odd integer:
Find the leaf subtrees whose data is an odd integer:
Find the inner subtrees whose data is an odd integer:
Find the first four subtrees whose data is an odd integer:
Use TreeCases as an operator form in a tree position specification:
Options (3)
TreeTraversalOrder (3)
By default, subtrees are listed in a depth-first order, with parents visited after their children:
Specify a top-down, right-to-left variant:
List subtrees in a breadth-first order, with nodes on the same level from the root visited before the nodes on the next level:
List subtrees in a leaves-first order, with nodes on the same level from the leaves visited before the nodes on the next level:
Properties & Relations (5)
TreeCases extracts the positions given by TreePosition:
TreeCount gives the number of matching subtrees given by TreeCases:
TreeCases[tree,pattern,Infinity] is equivalent to TreeChildren@TreeExtract[tree,TreeCases[pattern]]:
Cases[expr,pattern,levelspec] is equivalent to TreeData/@TreeCases[ExpressionTree[expr,"Subexpressions"],pattern,levelspec]:
Cases[expr,pattern,levelspec,HeadsTrue] is equivalent to TreeData/@TreeCases[ExpressionTree[expr,"Subexpressions",HeadsTrue],pattern,levelspec] for positive levels:
Possible Issues (1)
TreeCases by default starts at level 1, so does not visit the root:
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