applies f to the data at the position specified by pos in tree.


applies f to the data at several positions.


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


  • TreeMapAt applies a function to parts of the data at any position in a Tree object.
  • TreeMapAt[f,tree,{i,j,}] applies the function f to the data at position {i,j,}.
  • TreeMapAt can use the position specifications returned by TreePosition as well as extended forms as used in functions such as TreeInsert and TreeReplacePart. »
  • An individual position specification pos can be given as {part1,part2,}, where part specifications parti include:
  • ithe i^(th) child
    -ithe i^(th) child from the end
    {i1,i2,}the list of children with part numbers i1, i2,
    Allall children
    m;;nchildren m through n
    TreeLevel[levelspec]the subtrees on levels specified by levelspec
    TreeCases[pattern]the subtrees with data matching pattern
    TreeSelect[crit]the subtrees for which crit gives True
    TreeDatathe data in the tree
  • In TreeMapAt[f,tree,{p1,p2,}], {p1,p2,} is interpreted as a list of individual position specifications if all the pi are lists. Otherwise, {p1,p2,} will be taken to be an individual position specification. »
  • In TreeMapAt position specifications, part 0 is equivalent to TreeData. »
  • TreeMapAt applies f repeatedly to a particular position if that position is mentioned more than once in the list of positions. »
  • TreeMapAt[f,pos][tree] is equivalent to TreeMapAt[f,tree,pos].


open allclose all

Basic Examples  (4)

Map f at the data of the second child of a tree:

Map at any position:

Map at multiple positions:

Use the operator form of TreeMapAt:

Map using different specifications:

Scope  (8)

Part Specifications  (8)

Map at the first child:

Map at the second child from the end:

Map at all children:

Map at odd positions:

Map on a range of levels:

Map at data matching a pattern:

Map at subtrees with exactly two children:

Map inside the data of a tree:

Properties & Relations  (11)

TreeMap[f,tree,levelspec] is equivalent to TreeMapAt[f,tree,TreeLevel[levelspec]]:

Construct a tree from the heads in an expression:

TreeMapAt maps at the data of subtrees of a tree:

This corresponds to mapping at the heads and leaves in an expression:

MapAt can map at the heads and leaves directly:

If pos is not a list, pos and {pos} are equivalent specifications:

TreeMapAt[f,tree,{{i1,j1,},{i2,j2,},}] is equivalent to @TreeMapAt[f,{i2,j2,}]@TreeMapAt[f,{i1,j1,}]@tree:

This extends to the empty list:

Mapping at position {} applies the function to the data of the root element:

TreeMapAt applies f repeatedly if a position is mentioned repeatedly:

TreeMapAt can use the lists of positions returned by TreePosition:

This is the data returned by TreeExtract:

Take a tree containing another tree as data:

Map at a subtree of that data tree:

Equivalently, use 0 to refer to the tree data:

Map at a subexpression of the data:

Mapping at a position is equivalent to mapping at the data at that position:

If the position already refers to the data, that data itself is used:

Specifying an additional 0 goes deeper in the data:

TreeMapAt[f,tree,{p1,p2,}] treats {p1,p2,} as a list of individual position specifications if all the pi are lists:

For {{1,2},{3,4}}, the data at positions {1,2} and {3,4} is modified:

If any pi is not a list, {p1,p2,} is treated as a list of part specifications:

For {{{1,2},{3,4}}}, parts 3 and 4 of parts 1 and 2 are modified:

Possible Issues  (2)

TreeMapAt[f,tree,{}] does not map f because the list of positions is empty:

TreeMapAt[f,tree,{{}}] maps f at position {}, which corresponds to the whole tree:

A list of lists in the second argument is always interpreted as a list of position specifications:

As a result, specifications that are equivalent in Part may be treated differently by TreeMapAt:

Use a list of lists if consistency between Part and TreeMapAt is needed:

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


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


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


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


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


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