Products
  • Wolfram|One

    The definitive Wolfram Language and notebook experience

  • Mathematica

    The original technical computing environment

  • Wolfram Notebook Assistant + LLM Kit

    All-in-one AI assistance for your Wolfram experience

  • System Modeler
  • Wolfram Player
  • Finance Platform
  • Wolfram Engine
  • Enterprise Private Cloud
  • Application Server
  • Wolfram|Alpha Notebook Edition
  • Wolfram Cloud App
  • Wolfram Player App

More mobile apps

Core Technologies of Wolfram Products

  • Wolfram Language
  • Computable Data
  • Wolfram Notebooks
  • AI & Linguistic Understanding

Deployment Options

  • Wolfram Cloud
  • wolframscript
  • Wolfram Engine Community Edition
  • Wolfram LLM API
  • WSTPServer
  • Wolfram|Alpha APIs

From the Community

  • Function Repository
  • Community Paclet Repository
  • Example Repository
  • Neural Net Repository
  • Prompt Repository
  • Wolfram Demonstrations
  • Data Repository
  • Group & Organizational Licensing
  • All Products
Consulting & Solutions

We deliver solutions for the AI era—combining symbolic computation, data-driven insights and deep technical expertise

  • Data & Computational Intelligence
  • Model-Based Design
  • Algorithm Development
  • Wolfram|Alpha for Business
  • Blockchain Technology
  • Education Technology
  • Quantum Computation

WolframConsulting.com

Wolfram Solutions

  • Data Science
  • Artificial Intelligence
  • Biosciences
  • Healthcare Intelligence
  • Sustainable Energy
  • Control Systems
  • Enterprise Wolfram|Alpha
  • Blockchain Labs

More Wolfram Solutions

Wolfram Solutions For Education

  • Research Universities
  • Colleges & Teaching Universities
  • Junior & Community Colleges
  • High Schools
  • Educational Technology
  • Computer-Based Math

More Solutions for Education

  • Contact Us
Learning & Support

Get Started

  • Wolfram Language Introduction
  • Fast Intro for Programmers
  • Fast Intro for Math Students
  • Wolfram Language Documentation

More Learning

  • Highlighted Core Areas
  • Demonstrations
  • YouTube
  • Daily Study Groups
  • Wolfram Schools and Programs
  • Books

Grow Your Skills

  • Wolfram U

    Courses in computing, science, life and more

  • Community

    Learn, solve problems and share ideas.

  • Blog

    News, views and insights from Wolfram

  • Resources for

    Software Developers

Tech Support

  • Contact Us
  • Support FAQs
  • Support FAQs
  • Contact Us
Company
  • About Wolfram
  • Career Center
  • All Sites & Resources
  • Connect & Follow
  • Contact Us

Work with Us

  • Student Ambassador Initiative
  • Wolfram for Startups
  • Student Opportunities
  • Jobs Using Wolfram Language

Educational Programs for Adults

  • Summer School
  • Winter School

Educational Programs for Youth

  • Middle School Camp
  • High School Research Program
  • Computational Adventures

Read

  • Stephen Wolfram's Writings
  • Wolfram Blog
  • Wolfram Tech | Books
  • Wolfram Media
  • Complex Systems

Educational Resources

  • Wolfram MathWorld
  • Wolfram in STEM/STEAM
  • Wolfram Challenges
  • Wolfram Problem Generator

Wolfram Initiatives

  • Wolfram Science
  • Wolfram Foundation
  • History of Mathematics Project

Events

  • Stephen Wolfram Livestreams
  • Online & In-Person Events
  • Contact Us
  • Connect & Follow
Wolfram|Alpha
  • Your Account
  • User Portal
  • Wolfram Cloud
  • Products
    • Wolfram|One
    • Mathematica
    • Wolfram Notebook Assistant + LLM Kit
    • System Modeler
    • Wolfram Player
    • Finance Platform
    • Wolfram|Alpha Notebook Edition
    • Wolfram Engine
    • Enterprise Private Cloud
    • Application Server
    • Wolfram Cloud App
    • Wolfram Player App

    More mobile apps

    • Core Technologies
      • Wolfram Language
      • Computable Data
      • Wolfram Notebooks
      • AI & Linguistic Understanding
    • Deployment Options
      • Wolfram Cloud
      • wolframscript
      • Wolfram Engine Community Edition
      • Wolfram LLM API
      • WSTPServer
      • Wolfram|Alpha APIs
    • From the Community
      • Function Repository
      • Community Paclet Repository
      • Example Repository
      • Neural Net Repository
      • Prompt Repository
      • Wolfram Demonstrations
      • Data Repository
    • Group & Organizational Licensing
    • All Products
  • Consulting & Solutions

    We deliver solutions for the AI era—combining symbolic computation, data-driven insights and deep technical expertise

    WolframConsulting.com

    Wolfram Solutions

    • Data Science
    • Artificial Intelligence
    • Biosciences
    • Healthcare Intelligence
    • Sustainable Energy
    • Control Systems
    • Enterprise Wolfram|Alpha
    • Blockchain Labs

    More Wolfram Solutions

    Wolfram Solutions For Education

    • Research Universities
    • Colleges & Teaching Universities
    • Junior & Community Colleges
    • High Schools
    • Educational Technology
    • Computer-Based Math

    More Solutions for Education

    • Contact Us
  • Learning & Support

    Get Started

    • Wolfram Language Introduction
    • Fast Intro for Programmers
    • Fast Intro for Math Students
    • Wolfram Language Documentation

    Grow Your Skills

    • Wolfram U

      Courses in computing, science, life and more

    • Community

      Learn, solve problems and share ideas.

    • Blog

      News, views and insights from Wolfram

    • Resources for

      Software Developers
    • Tech Support
      • Contact Us
      • Support FAQs
    • More Learning
      • Highlighted Core Areas
      • Demonstrations
      • YouTube
      • Daily Study Groups
      • Wolfram Schools and Programs
      • Books
    • Support FAQs
    • Contact Us
  • Company
    • About Wolfram
    • Career Center
    • All Sites & Resources
    • Connect & Follow
    • Contact Us

    Work with Us

    • Student Ambassador Initiative
    • Wolfram for Startups
    • Student Opportunities
    • Jobs Using Wolfram Language

    Educational Programs for Adults

    • Summer School
    • Winter School

    Educational Programs for Youth

    • Middle School Camp
    • High School Research Program
    • Computational Adventures

    Read

    • Stephen Wolfram's Writings
    • Wolfram Blog
    • Wolfram Tech | Books
    • Wolfram Media
    • Complex Systems
    • Educational Resources
      • Wolfram MathWorld
      • Wolfram in STEM/STEAM
      • Wolfram Challenges
      • Wolfram Problem Generator
    • Wolfram Initiatives
      • Wolfram Science
      • Wolfram Foundation
      • History of Mathematics Project
    • Events
      • Stephen Wolfram Livestreams
      • Online & In-Person Events
    • Contact Us
    • Connect & Follow
  • Wolfram|Alpha
  • Wolfram Cloud
  • Your Account
  • User Portal
Wolfram Language & System Documentation Center
TreeMap
  • See Also
    • Tree
    • TreeScan
    • TreeTraversalOrder
    • TreeMapAt
    • TreeLevel
    • NestTree
    • TreeFold
    • Map
  • Related Guides
    • Computation on Trees
    • Trees
    • See Also
      • Tree
      • TreeScan
      • TreeTraversalOrder
      • TreeMapAt
      • TreeLevel
      • NestTree
      • TreeFold
      • Map
    • Related Guides
      • Computation on Trees
      • Trees

TreeMap[f,tree]

applies f to the data of each subtree of tree.

TreeMap[f,tree,levelspec]

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

TreeMap[f,tree,levelspecelems]

applies f to the elements elems of subtrees on levels specified by levelspec.

TreeMap[f]

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

Details and Options
Details and Options Details and Options
Examples  
Basic Examples  
Scope  
Level Specifications  
Elements  
Options  
TreeTraversalOrder  
Properties & Relations  
Possible Issues  
See Also
Related Guides
History
Cite this Page
BUILT-IN SYMBOL
  • See Also
    • Tree
    • TreeScan
    • TreeTraversalOrder
    • TreeMapAt
    • TreeLevel
    • NestTree
    • TreeFold
    • Map
  • Related Guides
    • Computation on Trees
    • Trees
    • See Also
      • Tree
      • TreeScan
      • TreeTraversalOrder
      • TreeMapAt
      • TreeLevel
      • NestTree
      • TreeFold
      • Map
    • Related Guides
      • Computation on Trees
      • Trees

TreeMap

TreeMap[f,tree]

applies f to the data of each subtree of tree.

TreeMap[f,tree,levelspec]

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

TreeMap[f,tree,levelspecelems]

applies f to the elements elems of subtrees on levels specified by levelspec.

TreeMap[f]

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

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 levelspecelem include:
  • Additional elements include:
  • "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:
  • nlevels 1 through n
    Infinitylevels 1 through Infinity
    {n}level n only
    {n1,n2}levels n1 through n2
    Alllevels 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 Automaticvisit subtrees in different orders, such as depth-first and breadth-first traversals
  • TreeMap[f][tree] is equivalent to TreeMap[f,tree].

Examples

open all close all

Basic Examples  (5)

Map f at the data of a tree:

Map f on the specified levels:

Map f at the subtrees of a tree:

Supply the position as an additional argument:

Use the operator form of TreeMap:

Scope  (12)

Level Specifications  (5)

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:

Positive and negative levels can be mixed:

Map on the leaves:

Map on the non-leaves:

Elements  (7)

Map at the data (default):

Map at the subtrees:

Map at the original subtrees:

Map at the children:

Map at the original children:

Map at the data of the children:

Map at the original data of the children:

Supply the position as an additional argument:

Supply the index rather than the position:

Supply the level specifications as an additional argument:

Supply the level rather than both level specifications:

Supply the negative level rather than both level specifications:

Supply a sequence of elements as arguments:

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 on the data of subtrees in a tree:

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

Map can map on the heads and leaves directly:

Construct a tree from the atoms in an expression:

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

This corresponds to mapping on the atoms in an expression:

Map can map on the atoms in an expression directly:

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

TreeMap can compute the same result:

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:

See Also

Tree  TreeScan  TreeTraversalOrder  TreeMapAt  TreeLevel  NestTree  TreeFold  Map

Related Guides

    ▪
  • Computation on Trees
  • ▪
  • Trees

History

Introduced in 2021 (12.3) | Updated in 2021 (13.0) ▪ 2022 (13.1) ▪ 2024 (14.0)

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).

CMS

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

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: 01-December-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: 01-December-2025]}

Top
Introduction for Programmers
Introductory Book
Wolfram Function Repository | Wolfram Data Repository | Wolfram Data Drop | Wolfram Language Products
Top
  • Products
  • Wolfram|One
  • Mathematica
  • Notebook Assistant + LLM Kit
  • System Modeler

  • Wolfram|Alpha Notebook Edition
  • Wolfram|Alpha Pro
  • Mobile Apps

  • Wolfram Player
  • Wolfram Engine

  • Volume & Site Licensing
  • Server Deployment Options
  • Consulting
  • Wolfram Consulting
  • Repositories
  • Data Repository
  • Function Repository
  • Community Paclet Repository
  • Neural Net Repository
  • Prompt Repository

  • Wolfram Language Example Repository
  • Notebook Archive
  • Wolfram GitHub
  • Learning
  • Wolfram U
  • Wolfram Language Documentation
  • Webinars & Training
  • Educational Programs

  • Wolfram Language Introduction
  • Fast Introduction for Programmers
  • Fast Introduction for Math Students
  • Books

  • Wolfram Community
  • Wolfram Blog
  • Public Resources
  • Wolfram|Alpha
  • Wolfram Problem Generator
  • Wolfram Challenges

  • Computer-Based Math
  • Computational Thinking
  • Computational Adventures

  • Demonstrations Project
  • Wolfram Data Drop
  • MathWorld
  • Wolfram Science
  • Wolfram Media Publishing
  • Customer Resources
  • Store
  • Product Downloads
  • User Portal
  • Your Account
  • Organization Access

  • Support FAQ
  • Contact Support
  • Company
  • About Wolfram
  • Careers
  • Contact
  • Events
Wolfram Community Wolfram Blog
Legal & Privacy Policy
WolframAlpha.com | WolframCloud.com
© 2025 Wolfram
© 2025 Wolfram | Legal & Privacy Policy |
English