TreeMap

Details and Options


- TreeMap allows a function to be applied to elements of subtrees on any range of levels in a Tree object. The function can be applied to the subtrees in many orders, including depth-first and breadth-first traversals.
- Structural elements elem for levelspecelem include:
- Additional elements include:
-
"OriginalData" the original data of the subtree "OriginalChildren" the original children of the subtree "OriginalSubtree" the original subtree "OriginalChildrenData" the original data of the children "Position" the position of the subtree "Index" the index of the subtree in the list of siblings "LevelPair" the non-negative and negative levels of the subtree "Level" the non-negative level of the subtree from the root "NegativeLevel" the negative level of the subtree from the leaves - TreeMap uses standard level specifications as in TreeLevel:
-
n levels 1 through n Infinity levels 1 through Infinity {n} level n only {n1,n2} levels n1 through n2 All levels 0 through Infinity "Leaves" level -1 only "NonLeaves" levels 0 through -2 - The default value for levelspec in TreeMap is {0,Infinity}.
- TreeMap traverses subtrees in a left-to-right, depth-first order, with children visited before their parents.
- TreeMap has the same options as Tree, with the following addition:
-
TreeTraversalOrder Automatic visit subtrees in different orders, such as depth-first and breadth-first traversals - TreeMap[f][tree] is equivalent to TreeMap[f,tree].
Examples
open allclose allBasic Examples (5)Summary of the most common use cases

https://wolfram.com/xid/0enygxjns-uvu8cl

Map f on the specified levels:

https://wolfram.com/xid/0enygxjns-i86m1e

Map f at the subtrees of a tree:

https://wolfram.com/xid/0enygxjns-c3mwam

Supply the position as an additional argument:

https://wolfram.com/xid/0enygxjns-gpvc3r

Use the operator form of TreeMap:

https://wolfram.com/xid/0enygxjns-3p0osi

Scope (12)Survey of the scope of standard use cases
Level Specifications (5)

https://wolfram.com/xid/0enygxjns-og41r1

https://wolfram.com/xid/0enygxjns-ruefj2


https://wolfram.com/xid/0enygxjns-zzwnef


https://wolfram.com/xid/0enygxjns-liiocn


https://wolfram.com/xid/0enygxjns-x26jum

Map on all levels, starting at level 1:

https://wolfram.com/xid/0enygxjns-dw2a13


https://wolfram.com/xid/0enygxjns-zjk81


https://wolfram.com/xid/0enygxjns-uxkye4

https://wolfram.com/xid/0enygxjns-2vkget


https://wolfram.com/xid/0enygxjns-g3zrna


https://wolfram.com/xid/0enygxjns-zj65f7

Positive and negative levels can be mixed:

https://wolfram.com/xid/0enygxjns-ilts77


https://wolfram.com/xid/0enygxjns-4n99bf


https://wolfram.com/xid/0enygxjns-ipt3km

Elements (7)

https://wolfram.com/xid/0enygxjns-limfmi


https://wolfram.com/xid/0enygxjns-4c8lnu


https://wolfram.com/xid/0enygxjns-7widce


https://wolfram.com/xid/0enygxjns-7lxnya


https://wolfram.com/xid/0enygxjns-2toz6g


https://wolfram.com/xid/0enygxjns-u54uun

Map at the data of the children:

https://wolfram.com/xid/0enygxjns-cphd62

Map at the original data of the children:

https://wolfram.com/xid/0enygxjns-xpza0n

Supply the position as an additional argument:

https://wolfram.com/xid/0enygxjns-os1lu9

Supply the index rather than the position:

https://wolfram.com/xid/0enygxjns-o8up9f

Supply the level specifications as an additional argument:

https://wolfram.com/xid/0enygxjns-3eo7d1

Supply the level rather than both level specifications:

https://wolfram.com/xid/0enygxjns-8t8lih

Supply the negative level rather than both level specifications:

https://wolfram.com/xid/0enygxjns-t7i43a

Supply a sequence of elements as arguments:

https://wolfram.com/xid/0enygxjns-z3ggwx

Options (3)Common values & functionality for each option
TreeTraversalOrder (3)
By default, subtrees are visited in a depth-first order, with parents visited after their children:

https://wolfram.com/xid/0enygxjns-5tcyv1

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

https://wolfram.com/xid/0enygxjns-kk8x29

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

https://wolfram.com/xid/0enygxjns-osp4wq

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

https://wolfram.com/xid/0enygxjns-r7lxyi

Properties & Relations (6)Properties of the function, and connections to other functions
Children are visited before their parents:

https://wolfram.com/xid/0enygxjns-8402bf




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

https://wolfram.com/xid/0enygxjns-cjhvh1




https://wolfram.com/xid/0enygxjns-rq2wqe




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

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

Construct a tree from the heads in an expression:

https://wolfram.com/xid/0enygxjns-rdybzq

TreeMap maps on the data of subtrees in a tree:

https://wolfram.com/xid/0enygxjns-ua6c1d

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

https://wolfram.com/xid/0enygxjns-xohbvz

Map can map on the heads and leaves directly:

https://wolfram.com/xid/0enygxjns-2swenz

https://wolfram.com/xid/0enygxjns-05t4xe

Construct a tree from the atoms in an expression:

https://wolfram.com/xid/0enygxjns-7bxt43

TreeMap can map on the data of just the leaves in a tree:

https://wolfram.com/xid/0enygxjns-r3ek67

This corresponds to mapping on the atoms in an expression:

https://wolfram.com/xid/0enygxjns-ro9mlw

Map can map on the atoms in an expression directly:

https://wolfram.com/xid/0enygxjns-s0zk5i

TreeFold applies a function to the data and results for the children:

https://wolfram.com/xid/0enygxjns-i1962k

TreeMap can compute the same result:

https://wolfram.com/xid/0enygxjns-mh0fxu

The new data of the root is the result of TreeFold:

https://wolfram.com/xid/0enygxjns-krmvr5

Possible Issues (1)Common pitfalls and unexpected behavior
TreeMap by default starts at level 1, so does not apply the function to the whole tree:

https://wolfram.com/xid/0enygxjns-obmaad


https://wolfram.com/xid/0enygxjns-hd3y18

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