TreeMap

TreeMap[f,tree]

applies f to the data in each subtree of tree.

TreeMap[f,tree,levelspec]

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

TreeMap[f,tree,levelspecelem]

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

TreeMap[f]

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

Details

  • 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.
  • TreeMap[f,tree,levelspec] applies f to the data on the given levels. This can also be expressed as TreeMap[f,tree,levelspec"Data"].
  • TreeMap[f,tree,levelspec"Subtree"] applies f to the subtree itself, rather than its data.
  • TreeMap[f,tree,levelspec"OriginalSubtree"] applies f to the original subtree, rather than the subtree already modified by f.
  • TreeMap 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 TreeMap is {0,Infinity}.
  • TreeMap traverses subtrees in a left-to-right, depth-first order, with children visited before their parents.
  • TreeMap[,TreeTraversalOrderorder] allows visiting subtrees in different orders, such as depth-first and breadth-first traversals.
  • TreeMap[f][tree] is equivalent to TreeMap[f,tree].

Examples

open allclose all

Basic Examples  (4)

Map f at the data of a tree:

Map f on the specified levels:

Map f at the subtrees of a tree:

Use the operator form of TreeMap:

Scope  (7)

Level Specifications  (6)

Map on all levels (default):

Map on levels 1 through 2:

Map only on level 2:

Map on levels 0 through 2:

Map on all levels, starting at level 1:

Map also on level 0:

Negative levels:

Map on the leaves:

Positive and negative levels can be mixed:

Map on the internal subtrees:

Elements  (1)

Map at the data (default):

Map at the subtrees:

Map at the original subtrees:

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  (6)

Children are visited before their parents:

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

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

Construct a tree from the heads in an expression:

TreeMap maps at the data of subtrees of a tree:

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

Map can map at the heads and leaves directly:

Construct a tree from the atoms in an expression:

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

This corresponds to mapping at the atoms in an expression:

Map can map at the atoms in an expression directly:

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

TreeMap can compute the same result when applied to subtrees:

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

Possible Issues  (1)

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

Wolfram Research (2021), TreeMap, Wolfram Language function, https://reference.wolfram.com/language/ref/TreeMap.html (updated 13).

Text

Wolfram Research (2021), TreeMap, Wolfram Language function, https://reference.wolfram.com/language/ref/TreeMap.html (updated 13).

CMS

Wolfram Language. 2021. "TreeMap." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 13. 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

BibTeX

@misc{reference.wolfram_2021_treemap, author="Wolfram Research", title="{TreeMap}", year="13", howpublished="\url{https://reference.wolfram.com/language/ref/TreeMap.html}", note=[Accessed: 20-January-2022 ]}

BibLaTeX

@online{reference.wolfram_2021_treemap, organization={Wolfram Research}, title={TreeMap}, year={13}, url={https://reference.wolfram.com/language/ref/TreeMap.html}, note=[Accessed: 20-January-2022 ]}