evaluates f applied to the data in each subtree of tree in turn.


applies f to the data on levels of tree specified by levelspec.


applies f to the element elem of subtrees on levels specified by levelspec.


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


  • Scanning a tree is also known as tree traversal or tree search. The subtrees can be visited in many different orders, including depth-first and breadth-first traversals.
  • Scanning a tree is typically used to carry out an operation on subtrees where the operation has a "side effect", such as making an assignment.
  • TreeScan allows a function to be applied to elements of subtrees on any range of levels in a Tree object.
  • TreeScan[f,tree] discards the results of applying f to the data of tree. Unlike TreeMap, TreeScan does not build up a new tree to return.
  • You can use Throw to exit from TreeScan.
  • TreeScan[f,tree,levelspec] applies f to the data on the given levels. This can also be expressed as TreeScan[f,tree,levelspec"Data"].
  • TreeScan[f,tree,levelspec"Subtree"] applies f to the subtree itself, rather than its data.
  • TreeScan[f,tree,levelspec"OriginalSubtree"] applies f to the original subtree, rather than the subtree already modified by f.
  • TreeScan 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 TreeScan is {0,Infinity}.
  • TreeScan traverses subtrees in a left-to-right, depth-first order, with children visited before their parents.
  • TreeScan[,TreeTraversalOrderorder] allows visiting subtrees in different orders, such as depth-first and breadth-first traversals.
  • TreeScan[f][tree] is equivalent to TreeScan[f,tree].


open allclose all

Basic Examples  (4)

Scan the data in a tree:

Scan data up to the first level:

Scan the subtrees:

Use the operator form of TreeScan:

Scope  (7)

Level Specifications  (6)

Scan all levels (default):

Scan levels 1 through 2:

Scan only on level 2:

Scan levels 0 through 2:

Scan all levels, starting at level 1:

Scan also level 0:

Negative levels:

Scan the leaves:

Positive and negative levels can be mixed:

Scan the internal subtrees:

Elements  (1)

Scan the data (default):

Scan the subtrees:

Scan the original subtrees:

Generalizations & Extensions  (1)

Throw works inside TreeScan:

Options  (3)

TreeTraversalOrder  (3)

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

Specify a top-down, right-to-left variant:

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

Visit 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  (4)

Children are visited before their parents:

TreeScan does the same as TreeMap but without returning a result:

Construct a tree from the heads in an expression:

TreeScan scans the data of subtrees of a tree:

Scan can scan the heads and leaves directly:

Use Sow and Reap to collect results:

Possible Issues  (1)

TreeScan by default starts at level 1, so does not apply the function to the whole tree:

Wolfram Research (13), TreeScan, Wolfram Language function,


Wolfram Research (13), TreeScan, Wolfram Language function,


Wolfram Language. 13. "TreeScan." Wolfram Language & System Documentation Center. Wolfram Research.


Wolfram Language. (13). TreeScan. Wolfram Language & System Documentation Center. Retrieved from


@misc{reference.wolfram_2021_treescan, author="Wolfram Research", title="{TreeScan}", year="13", howpublished="\url{}", note=[Accessed: 24-January-2022 ]}


@online{reference.wolfram_2021_treescan, organization={Wolfram Research}, title={TreeScan}, year={13}, url={}, note=[Accessed: 24-January-2022 ]}