TreeMapAt
✖
TreeMapAt

Details and Options

- 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:
-
i the i child
-i the i child from the end
{i1,i2,…} the list of children with part numbers i1, i2, … All all children m;;n children m through n "key" the child whose key is "key" Key[k] the child with an arbitrary key k 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 - 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. »
- TreeMapAt works on trees containing an Association of subtrees, using the same specification for keys as in Part. »
- 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].
- TreeMapAt has the same options as Tree.
Examples
open allclose allBasic Examples (3)Summary of the most common use cases
Map f at the data of the second child of a tree:

https://wolfram.com/xid/0n4ltygsyz8-55oin9


https://wolfram.com/xid/0n4ltygsyz8-h1n0n1


https://wolfram.com/xid/0n4ltygsyz8-hsh07

Use the operator form of TreeMapAt:

https://wolfram.com/xid/0n4ltygsyz8-yn1603

Scope (14)Survey of the scope of standard use cases
Part Specifications (9)

https://wolfram.com/xid/0n4ltygsyz8-4pz5ah

Map at the second child from the end:

https://wolfram.com/xid/0n4ltygsyz8-wnnn1z


https://wolfram.com/xid/0n4ltygsyz8-5eqii1


https://wolfram.com/xid/0n4ltygsyz8-087ui1

Use Key to specify position:

https://wolfram.com/xid/0n4ltygsyz8-gpohgs

For string keys, Key is not needed:

https://wolfram.com/xid/0n4ltygsyz8-urimp2


https://wolfram.com/xid/0n4ltygsyz8-bovysl

Map at data matching a pattern:

https://wolfram.com/xid/0n4ltygsyz8-5hhn38

Map at subtrees with exactly two children:

https://wolfram.com/xid/0n4ltygsyz8-0ebjdg

Map using different specifications:

https://wolfram.com/xid/0n4ltygsyz8-8ffzed

Associations (5)
Map onto a tree containing an association by key:

https://wolfram.com/xid/0n4ltygsyz8-qn2p2g

If the key is a string, the Key wrapper is optional:

https://wolfram.com/xid/0n4ltygsyz8-kbt6kw

Map onto a tree containing an association by key:

https://wolfram.com/xid/0n4ltygsyz8-6vaf4l

Map onto a tree containing an association by position:

https://wolfram.com/xid/0n4ltygsyz8-snbzmi

Map at several subtrees of a tree containing an association:

https://wolfram.com/xid/0n4ltygsyz8-0bktbf

Map at a nested subtree of a tree containing an association:

https://wolfram.com/xid/0n4ltygsyz8-hhn55c

Map at several nested subtrees of a tree containing an association:

https://wolfram.com/xid/0n4ltygsyz8-npbjue

Properties & Relations (8)Properties of the function, and connections to other functions
TreeMap[f,tree,levelspec] is equivalent to TreeMapAt[f,tree,TreeLevel[levelspec]]:

https://wolfram.com/xid/0n4ltygsyz8-39l2x5

Construct a tree from the heads in an expression:

https://wolfram.com/xid/0n4ltygsyz8-etw6wf

TreeMapAt maps at the data of subtrees of a tree:

https://wolfram.com/xid/0n4ltygsyz8-58qu1q

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

https://wolfram.com/xid/0n4ltygsyz8-6l8grk

MapAt can map at the heads and leaves directly:

https://wolfram.com/xid/0n4ltygsyz8-ekzn6g

https://wolfram.com/xid/0n4ltygsyz8-lzlujp

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

https://wolfram.com/xid/0n4ltygsyz8-l4o1qg

https://wolfram.com/xid/0n4ltygsyz8-c1li3w


https://wolfram.com/xid/0n4ltygsyz8-ktswny

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

https://wolfram.com/xid/0n4ltygsyz8-c64486

https://wolfram.com/xid/0n4ltygsyz8-ehlm16

This extends to the empty list:

https://wolfram.com/xid/0n4ltygsyz8-hd3wyw

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

https://wolfram.com/xid/0n4ltygsyz8-wl3t8

TreeMapAt applies f repeatedly if a position is mentioned repeatedly:

https://wolfram.com/xid/0n4ltygsyz8-532gy

TreeMapAt can use the lists of positions returned by TreePosition:

https://wolfram.com/xid/0n4ltygsyz8-tspq6e

https://wolfram.com/xid/0n4ltygsyz8-redf11


https://wolfram.com/xid/0n4ltygsyz8-vu0873

This is the data returned by TreeExtract:

https://wolfram.com/xid/0n4ltygsyz8-rumkf4

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

https://wolfram.com/xid/0n4ltygsyz8-otyc2w
For {{1,2},{3,4}}, the data at positions {1,2} and {3,4} is modified:

https://wolfram.com/xid/0n4ltygsyz8-rd9htt

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

https://wolfram.com/xid/0n4ltygsyz8-6pftch


https://wolfram.com/xid/0n4ltygsyz8-5xamjx

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

https://wolfram.com/xid/0n4ltygsyz8-1inecb

Possible Issues (2)Common pitfalls and unexpected behavior
TreeMapAt[f,tree,{}] does not map f because the list of positions is empty:

https://wolfram.com/xid/0n4ltygsyz8-sg9n6y

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

https://wolfram.com/xid/0n4ltygsyz8-fhig6f

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

https://wolfram.com/xid/0n4ltygsyz8-ejwcun

https://wolfram.com/xid/0n4ltygsyz8-pulhcd

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

https://wolfram.com/xid/0n4ltygsyz8-hrlioq


https://wolfram.com/xid/0n4ltygsyz8-dnpae4

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

https://wolfram.com/xid/0n4ltygsyz8-07ps0b

Wolfram Research (2021), TreeMapAt, Wolfram Language function, https://reference.wolfram.com/language/ref/TreeMapAt.html (updated 2024).
Text
Wolfram Research (2021), TreeMapAt, Wolfram Language function, https://reference.wolfram.com/language/ref/TreeMapAt.html (updated 2024).
Wolfram Research (2021), TreeMapAt, Wolfram Language function, https://reference.wolfram.com/language/ref/TreeMapAt.html (updated 2024).
CMS
Wolfram Language. 2021. "TreeMapAt." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2024. https://reference.wolfram.com/language/ref/TreeMapAt.html.
Wolfram Language. 2021. "TreeMapAt." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2024. https://reference.wolfram.com/language/ref/TreeMapAt.html.
APA
Wolfram Language. (2021). TreeMapAt. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/TreeMapAt.html
Wolfram Language. (2021). TreeMapAt. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/TreeMapAt.html
BibTeX
@misc{reference.wolfram_2025_treemapat, author="Wolfram Research", title="{TreeMapAt}", year="2024", howpublished="\url{https://reference.wolfram.com/language/ref/TreeMapAt.html}", note=[Accessed: 15-April-2025
]}
BibLaTeX
@online{reference.wolfram_2025_treemapat, organization={Wolfram Research}, title={TreeMapAt}, year={2024}, url={https://reference.wolfram.com/language/ref/TreeMapAt.html}, note=[Accessed: 15-April-2025
]}