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.

TreeCases[pattern]

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

Details

  • In TreeCases[tree,pattern], tree must be a valid Tree object.
  • TreeCases[pattern][tree] is equivalent to TreeCases[tree,pattern].
  • TreeCases uses standard level specifications:
  • nlevels 1 through n
    Infinitylevels 1 through Infinity
    {n}level n only
    {n1,n2}levels n1 through n2
  • The default value for levelspec in TreeCases is {0,Infinity}.
  • A positive level n+1 consists of all subtrees of tree that are children of subtrees on level n.
  • Level 0 corresponds to the root of the tree.
  • Level -1 corresponds to the leaves.
  • A negative level -(n+1) consists of all subtrees whose children are on levels -1, -2, , -n, with highest level -n.
  • TreeCases traverses subtrees in a left-to-right, depth-first order, with children visited before their parents.

Examples

open allclose all

Basic Examples  (3)

Find the subtrees whose data is an even number:

Find the subtrees in the first level whose data is an even number:

Find the leaves whose data is an even number:

Scope  (6)

Find the subtrees whose data is an odd integer:

Find the subtrees at levels 0, 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 the operator form of TreeCases:

Properties & Relations  (2)

Cases[expr,pattern,levelspec] is equivalent to TreeData/@TreeCases[ExpressionTree[expr,"Arguments"],pattern,levelspec]:

Cases[expr,pattern,levelspec,HeadsTrue] is equivalent to TreeData/@TreeCases[ExpressionTree[expr,"Subexpressions"],pattern,levelspec] for positive levels:

Wolfram Research (2021), TreeCases, Wolfram Language function, https://reference.wolfram.com/language/ref/TreeCases.html.

Text

Wolfram Research (2021), TreeCases, Wolfram Language function, https://reference.wolfram.com/language/ref/TreeCases.html.

BibTeX

@misc{reference.wolfram_2021_treecases, author="Wolfram Research", title="{TreeCases}", year="2021", howpublished="\url{https://reference.wolfram.com/language/ref/TreeCases.html}", note=[Accessed: 22-June-2021 ]}

BibLaTeX

@online{reference.wolfram_2021_treecases, organization={Wolfram Research}, title={TreeCases}, year={2021}, url={https://reference.wolfram.com/language/ref/TreeCases.html}, note=[Accessed: 22-June-2021 ]}

CMS

Wolfram Language. 2021. "TreeCases." Wolfram Language & System Documentation Center. Wolfram Research. 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