TreeSelect[tree,crit]
picks out all subtrees treei of tree for which crit[treei] is True.
TreeSelect[tree,crit,n]
picks out the first n subtrees for which crit[treei] is True.
TreeSelect[tree,crit,levelspec,n]
picks out subtrees on levels specified by levelspec.
TreeSelect[crit]
represents an operator form of TreeSelect that can be applied to a tree.
TreeSelect
TreeSelect[tree,crit]
picks out all subtrees treei of tree for which crit[treei] is True.
TreeSelect[tree,crit,n]
picks out the first n subtrees for which crit[treei] is True.
TreeSelect[tree,crit,levelspec,n]
picks out subtrees on levels specified by levelspec.
TreeSelect[crit]
represents an operator form of TreeSelect that can be applied to a tree.
Details and Options
- TreeSelect lists the subtrees satisfying any criterion on any range of levels in a Tree object. The list of results can be given in many different orders, including depth-first and breadth-first traversals.
- TreeSelect[tree,crit] tests all the subtrees of tree in turn to try to find ones satisfying the criterion crit.
- TreeSelect 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 - TreeSelect traverses subtrees in a left-to-right, depth-first order, with children visited before their parents.
- TreeSelect[…,TreeTraversalOrderorder] allows visiting subtrees in different orders, such as depth-first and breadth-first traversals.
- TreeSelect[crit][tree] is equivalent to TreeSelect[tree,crit].
Examples
open all close allBasic Examples (4)
Select the subtrees whose root node has an even number as data:
TreeSelect[[image], EvenQ @* TreeData]Get the first six leaves of a tree:
TreeSelect[[image], TreeLeafQ, 6]Get the first two leaves on the second level:
TreeSelect[[image], TreeLeafQ, {2}, 2]Use the operator form of TreeSelect:
TreeSelect[Length[TreeChildren[#]] == 3&][[image]]Scope (3)
Select all subtrees with three children:
TreeSelect[[image], Length[TreeChildren[#]] == 3&]Select the first two subtrees with two children:
TreeSelect[[image], Length[TreeChildren[#]] == 2&, 2]Use TreeSelect as an operator form in a tree position specification:
TreeExtract[[image], {TreeSelect[Length[TreeChildren[#]] == 3&], 3}]Options (3)
TreeTraversalOrder (3)
By default, subtrees are listed in a depth-first order, with parents visited after their children:
TreeSelect[[image], True&, TreeTraversalOrder -> "DepthFirst"]Specify a top-down, right-to-left variant:
TreeSelect[[image], True&, 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:
TreeSelect[[image], True&, 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:
TreeSelect[[image], True&, TreeTraversalOrder -> "LeavesFirst"]Properties & Relations (1)
TreeSelect[tree,crit,Infinity,Infinity] is equivalent to TreeChildren@TreeExtract[tree,TreeSelect[crit]]:
TreeSelect[[image], EvenQ @* TreeData, Infinity, Infinity]% === TreeChildren@TreeExtract[[image], TreeSelect[EvenQ @* TreeData]]Possible Issues (1)
TreeSelect by default starts at level 1, so does not visit the root:
TreeSelect[[image], True&, 1, Infinity]TreeSelect[[image], True&, {0, 1}, Infinity]Related Guides
Text
Wolfram Research (2021), TreeSelect, Wolfram Language function, https://reference.wolfram.com/language/ref/TreeSelect.html (updated 2024).
CMS
Wolfram Language. 2021. "TreeSelect." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2024. https://reference.wolfram.com/language/ref/TreeSelect.html.
APA
Wolfram Language. (2021). TreeSelect. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/TreeSelect.html
BibTeX
@misc{reference.wolfram_2026_treeselect, author="Wolfram Research", title="{TreeSelect}", year="2024", howpublished="\url{https://reference.wolfram.com/language/ref/TreeSelect.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_treeselect, organization={Wolfram Research}, title={TreeSelect}, year={2024}, url={https://reference.wolfram.com/language/ref/TreeSelect.html}, note=[Accessed: 13-June-2026]}