TreePosition

TreePosition[tree,pattern]

gives a list of the positions of subtrees of tree whose data matches pattern.

TreePosition[tree,pattern,levelspec]

finds only matches that appear on levels of tree specified by levelspec.

TreePosition[tree,pattern,levelspec,n]

gives the positions of the first n matches found.

TreePosition[pattern]

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

Details and Options

  • TreePosition lists the positions of data matching any pattern on any range of levels in a Tree object. The list of matching positions can be given in many different orders, including depth-first and breadth-first traversals.
  • TreePosition[tree,pattern] tests all the subtrees of tree in turn to try to find ones with data that match pattern.
  • TreePosition returns a list of positions in a form suitable for use in TreeExtract, TreeReplacePart and TreeDelete.
  • A part specification {} returned by TreePosition represents the whole of tree.
  • TreePosition 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
  • The default value for levelspec in TreePosition is {0,Infinity}.
  • TreePosition traverses subtrees in a left-to-right, depth-first order, yielding lists of indices in lexicographic order.
  • TreePosition[,TreeTraversalOrderorder] allows visiting subtrees in different orders, such as depth-first and breadth-first traversals.
  • TreePosition[pattern][tree] is equivalent to TreePosition[tree,pattern].

Examples

open allclose all

Basic Examples  (4)

Find the positions of the subtrees whose data is an even number:

Find the positions of the inner subtrees whose data is an even number:

Find the first three positions of prime numbers:

Use the operator form of TreePosition:

Scope  (5)

Find the positions of the subtrees whose data is an odd integer:

Find the positions of the subtrees at levels 1 and 2 whose data is an odd integer:

Find the positions of the leaf subtrees whose data is an odd integer:

Find the positions of the inner subtrees whose data is an odd integer:

Find the first four positions of the subtrees whose data is an odd integer:

Options  (3)

TreeTraversalOrder  (3)

By default, subtrees are visited in a depth-first order, with parents visited after their children:

Specify a top-down variant:

This lists the positions in lexicographic order:

Visit subtrees in a breadth-first order, with nodes on the same level from the root visited before the nodes on the next level:

This lists the positions in canonical order:

Visit subtrees in a leaves-first order, with nodes on the same level from the leaves visited before the nodes on the next level:

Applications  (1)

Compute the level with the most nodes:

Properties & Relations  (4)

TreeCases extracts the positions given by TreePosition:

TreeCount gives the number of matching positions given by TreePosition:

Position[expr,pattern,levelspec,HeadsFalse] is equivalent to TreePosition[ExpressionTree[expr,"Subexpressions"],pattern,levelspec]:

Position[expr,pattern,levelspec] is equivalent to TreePosition[ExpressionTree[expr,"Subexpressions",HeadsTrue],pattern,levelspec]-1] for positive levels:

Possible Issues  (1)

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

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

Text

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

CMS

Wolfram Language. 2021. "TreePosition." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2021. https://reference.wolfram.com/language/ref/TreePosition.html.

APA

Wolfram Language. (2021). TreePosition. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/TreePosition.html

BibTeX

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

BibLaTeX

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