TreeCount
Details
- TreeCount gives the number of subtrees with data matching any pattern on any range of levels in a Tree object.
- TreeCount 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 TreeCount 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.
- TreeCount traverses subtrees in a left-to-right, depth-first order, with children visited before their parents.
- TreeCount[pattern][tree] is equivalent to TreeCount[tree,pattern].
Examples
open all close allBasic Examples (3)
Count the number of subtrees whose data is an even number:
TreeCount[[image], _ ? EvenQ]Count the inner subtrees whose data is an even number:
TreeCount[[image], _ ? EvenQ, {0, -2}]Use the operator form of TreeCount:
TreeCount[_ ? PrimeQ][[image]]Scope (5)
Count the subtrees whose data is an odd integer:
TreeCount[[image], _ ? OddQ]Count the subtrees at levels 1 and 2 whose data is an odd integer:
TreeCount[[image], _ ? OddQ, 2]Count the leaf subtrees whose data is an odd integer:
TreeCount[[image], _ ? OddQ, {-1}]Count the inner subtrees whose data is an odd integer:
TreeCount[[image], _ ? OddQ, {0, -2}]Use the operator form of TreeCount:
TreeCount[_ ? OddQ][[image]]Properties & Relations (4)
TreeCount gives the number of matching subtrees given by TreeCases:
TreeCases[[image], x]TreeCount[[image], x]TreeSize[tree] is equivalent to TreeCount[tree,_]:
tree = RandomTree[8]TreeSize[tree]TreeCount[tree, _]Count[expr,pattern,levelspec] is equivalent to TreeCount[ExpressionTree[expr,"Subexpressions"],pattern,levelspec]:
Count[{{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"]TreeCount[%, _Integer, 2]Count[expr,pattern,levelspec,HeadsTrue] is equivalent to TreeCount[ExpressionTree[expr,"Subexpressions",HeadsTrue],pattern,levelspec] for positive levels:
Count[{1, 2, f[a, b]}, _, {0, Infinity}, Heads -> True]ExpressionTree[{1, 2, f[a, b]}, "Subexpressions", Heads -> True]TreeCount[%, _, {0, Infinity}]Possible Issues (1)
TreeCount by default starts at level 1, so does not visit the root:
TreeCount[[image], _, 2]TreeCount[[image], _, {0, 2}]See Also
Related Guides
History
Text
Wolfram Research (2021), TreeCount, Wolfram Language function, https://reference.wolfram.com/language/ref/TreeCount.html.
CMS
Wolfram Language. 2021. "TreeCount." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/TreeCount.html.
APA
Wolfram Language. (2021). TreeCount. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/TreeCount.html
BibTeX
@misc{reference.wolfram_2026_treecount, author="Wolfram Research", title="{TreeCount}", year="2021", howpublished="\url{https://reference.wolfram.com/language/ref/TreeCount.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_treecount, organization={Wolfram Research}, title={TreeCount}, year={2021}, url={https://reference.wolfram.com/language/ref/TreeCount.html}, note=[Accessed: 13-June-2026]}