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:
  • nlevels 1 through n
    Alllevels 0 through Infinity
    Infinitylevels 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 allclose all

Basic Examples  (4)

Select the subtrees whose root node has an even number as data:

Get the first six leaves of a tree:

Get the first two leaves on the second level:

Use the operator form of TreeSelect:

Scope  (3)

Select all subtrees with three children:

Select the first two subtrees with two children:

Use TreeSelect 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  (1)

TreeSelect[tree,crit,Infinity,Infinity] is equivalent to TreeChildren@TreeExtract[tree,TreeSelect[crit]]:

Possible Issues  (1)

TreeSelect by default starts at level 1, so does not visit the root:

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

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_2024_treeselect, author="Wolfram Research", title="{TreeSelect}", year="2024", howpublished="\url{https://reference.wolfram.com/language/ref/TreeSelect.html}", note=[Accessed: 21-November-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_treeselect, organization={Wolfram Research}, title={TreeSelect}, year={2024}, url={https://reference.wolfram.com/language/ref/TreeSelect.html}, note=[Accessed: 21-November-2024 ]}