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.
-
AlignmentPoint Center the default point in the graphic to align with AspectRatio Automatic ratio of height to width Axes False whether to draw axes AxesLabel None axes labels AxesOrigin Automatic where axes should cross AxesStyle {} style specifications for the axes Background None background color for the plot BaselinePosition Automatic how to align with a surrounding text baseline BaseStyle {} base style specifications for the graphic ContentSelectable Automatic whether to allow contents to be selected CoordinatesToolOptions Automatic detailed behavior of the coordinates tool Epilog {} primitives rendered after the main plot FormatType TraditionalForm the default format type for text Frame False whether to put a frame around the plot FrameLabel None frame labels FrameStyle {} style specifications for the frame FrameTicks Automatic frame ticks FrameTicksStyle {} style specifications for frame ticks GridLines None grid lines to draw GridLinesStyle {} style specifications for grid lines ImageMargins 0. the margins to leave around the graphic ImagePadding All what extra padding to allow for labels etc. ImageSize Automatic the absolute size at which to render the graphic LabelStyle {} style specifications for labels Method Automatic details of graphics methods to use PlotLabel None an overall label for the plot PlotRange All range of values to include PlotRangeClipping False whether to clip at the plot range PlotRangePadding Automatic how much to pad the range of values PlotRegion Automatic the final display region to be filled PreserveImageOptions Automatic whether to preserve image options when displaying new versions of the same graphic Prolog {} primitives rendered before the main plot RotateLabel True whether to rotate y labels on the frame Ticks Automatic axes ticks TicksStyle {} style specifications for axes ticks
List of all options
Examples
open all close allBasic Examples (3)
Map f at the data of the second child of a tree:
TreeMapAt[f, [image], {2}]TreeMapAt[f, [image], {3, 3}]TreeMapAt[f, [image], {{1}, {2, 1}, {3, 3}}]Use the operator form of TreeMapAt:
TreeMapAt[f, 1][[image]]Scope (14)
Part Specifications (9)
TreeMapAt[h, [image], 3]Map at the second child from the end:
TreeMapAt[h, [image], -2]TreeMapAt[h, [image], All]TreeMapAt[f, [image], 1 ;; -1 ;; 2]Use Key to specify position:
TreeMapAt[f, [image], Key["b"]]For string keys, Key is not needed:
TreeMapAt[f, [image], "b"]TreeMapAt[f, [image], TreeLevel[{1, 2}]]Map at data matching a pattern:
TreeMapAt[f, [image], TreeCases[_ ? EvenQ]]Map at subtrees with exactly two children:
TreeMapAt[f, [image], TreeSelect[Length[TreeChildren[#]] == 2&]]Map using different specifications:
TreeMapAt[f, [image], {TreeLevel[{2}], TreeCases[_ ? OddQ]}]Associations (5)
Map onto a tree containing an association by key:
TreeMapAt[f, [image], {Key[expr]}]If the key is a string, the Key wrapper is optional:
TreeMapAt[f, [image], {"str"}]Map onto a tree containing an association by key:
TreeMapAt[f, [image], {Key[1]}]Map onto a tree containing an association by position:
TreeMapAt[f, [image], {1}]Map at several subtrees of a tree containing an association:
TreeMapAt[f, [image], {{1}, {Key[1]}}]Map at a nested subtree of a tree containing an association:
TreeMapAt[f, [image], {Key[3], 1}]Map at several nested subtrees of a tree containing an association:
TreeMapAt[f, [image], {{Key[3], 1}, {3, 1}}]Properties & Relations (8)
TreeMap[f,tree,levelspec] is equivalent to TreeMapAt[f,tree,TreeLevel[levelspec]]:
TreeMap[f, [image], {1, 2}] === TreeMapAt[f, [image], TreeLevel[{1, 2}]]Construct a tree from the heads in an expression:
ExpressionTree[1[2[3, 4], 5[6, 7]], "Heads"]TreeMapAt maps at the data of subtrees of a tree:
TreeMapAt[f, %, {{1}, {{1, 2}, 2}}]This corresponds to mapping at the heads and leaves in an expression:
TreeExpression[%, "Heads"]MapAt can map at the heads and leaves directly:
mapAtData[f_][head_[args___]] := f[head][args]
mapAtData[f_][expr_] := f[expr]MapAt[mapAtData[f], 1[2[3, 4], 5[6, 7]], {{1}, {{1, 2}, 2}}]If pos is not a list, pos and {pos} are equivalent specifications:
tree = [image];TreeMapAt[f, tree, 3] === TreeMapAt[f, tree, {3}]TreeMapAt[f, tree, 3 ;; 5] === TreeMapAt[f, tree, {3 ;; 5}]TreeMapAt[f,tree,{{i1,j1,…},{i2,j2,…},…}] is equivalent to …@TreeMapAt[f,{i2,j2,…}]@TreeMapAt[f,{i1,j1,…}]@tree:
tree = [image];TreeMapAt[f, tree, {{1 ;; 2, 1}, {2 ;; 3, 2}}] === TreeMapAt[f, {2 ;; 3, 2}]@TreeMapAt[f, {1 ;; 2, 1}]@treeThis extends to the empty list:
TreeMapAt[f, tree, {}] === treeMapping at position {} applies the function to the data of the root node:
TreeMapAt[f, [image], {{}}]TreeMapAt applies f repeatedly if a position is mentioned repeatedly:
TreeMapAt[f, [image], {{1}, {3}, {1}}]TreeMapAt can use the lists of positions returned by TreePosition:
tree = [image];pos = TreePosition[tree, _ ? EvenQ]TreeMapAt[f, tree, pos]This is the data returned by TreeExtract:
TreeExtract[tree, pos, TreeData]TreeMapAt[f,tree,{p1,p2,…}] treats {p1,p2,…} as a list of individual position specifications if all the pi are lists:
tree = [image];For {{1,2},{3,4}}, the data at positions {1,2} and {3,4} is modified:
TreeMapAt[f, tree, {{1, 2}, {3, 4}}]If any pi is not a list, {p1,p2,…} is treated as a list of part specifications:
TreeMapAt[f, tree, {{1, 2}, 3}]TreeMapAt[f, tree, {1, {3, 4}}]For {{{1,2},{3,4}}}, parts 3 and 4 of parts 1 and 2 are modified:
TreeMapAt[f, tree, {{{1, 2}, {3, 4}}}]Possible Issues (2)
TreeMapAt[f,tree,{}] does not map f because the list of positions is empty:
TreeMapAt[f, [image], {}]TreeMapAt[f,tree,{{}}] maps f at position {}, which corresponds to the root node:
TreeMapAt[f, [image], {{}}]A list of lists in the second argument is always interpreted as a list of position specifications:
tree = [image];TreeMapAt[f, tree, {{1, 2}}]As a result, specifications that are equivalent in Part may be treated differently by TreeMapAt:
Part[TreeChildren[tree], {1, 2}] === Part[TreeChildren[tree], 1 ;; 2]TreeMapAt[f, tree, {{1, 2}}] === TreeMapAt[f, tree, {1 ;; 2}]Use a list of lists if consistency between Part and TreeMapAt is needed:
TreeMapAt[f, tree, {{{1, 2}}}] === TreeMapAt[f, tree, {{1 ;; 2}}]Related Guides
Text
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.
APA
Wolfram Language. (2021). TreeMapAt. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/TreeMapAt.html
BibTeX
@misc{reference.wolfram_2026_treemapat, author="Wolfram Research", title="{TreeMapAt}", year="2024", howpublished="\url{https://reference.wolfram.com/language/ref/TreeMapAt.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_treemapat, organization={Wolfram Research}, title={TreeMapAt}, year={2024}, url={https://reference.wolfram.com/language/ref/TreeMapAt.html}, note=[Accessed: 13-June-2026]}