gives the tree obtained by applying f to the data in the leaves of tree and extending the tree.


applies f at most n times, adding at most n additional levels to each leaf.


applies h to the data of the added subtrees.


  • NestTree[f,tree] is equivalent to NestTree[f,tree,1].
  • In NestTree[f,tree,n], f[data0] should give {data1,data2,} or Hold[data1,data2,].
  • If expr is not a valid Tree object, then NestTree[f,expr,n] is equivalent to NestTree[f,Tree[expr,None],n].
  • In NestTree[f,tree,n], n can be any non-negative machine integer or Infinity.
  • NestTree[f,tree,n+1] is equivalent to NestTree[f,NestTree[f,tree,n],1].
  • NestTree[f,tree,n,h] starts by applying h to the data data0 in each leaf, then applies f up to n times, extending each leaf with children containing h[data1], h[data2], , where h[data0] is the resulting data in the leaf and f[data0] gives {data1,data2,} or Hold[data1,data2,].
  • If f[data0] gives Hold[data1,data2,], then the expressions data1, data2, are given to the functions f and h unevaluated in NestTree[f,tree,n,h].


open allclose all

Basic Examples  (2)

Extend the leaves of a tree:

Build a tree from an expression:

Scope  (5)

Build a tree by adding a level of children to a leaf:

Specify the number of additional levels:

Repeatedly apply a function until no more levels are added:

Extend a tree:

Apply a function to the data after extending the tree:

Build a tree using a function with a holding attribute:

Build a tree without evaluating intermediate results:

Applications  (5)

Create a tree of life:

Create a tree of descendants:

Create a tree from the hierarchy of files in a directory:

Define a function that extracts the tag and its attributes from an XMLElement:

Define a function that yields the child elements of an XMLElement:

Convert an XMLElement object to a tree:

Define a function that converts a TextElement object to a tree:

Define a function that yields the child elements of a TextElement:

Define a function that extracts the "GrammaticalUnit" from a TextElement:

Convert TextElement to a tree:

Properties & Relations  (4)

NestList gives a list of results of repeatedly applying a function to a single value:

NestTree gives a tree of results:

NestGraph applies a function to each vertex in a graph:

NestTree applies a function to each leaf in a tree:

Imitate the output of CompleteKaryTree using NestTree:

Convert the tree to an actual Graph object:

Generate a tree containing random integers from 0 to 10 and between 1 and 3 children for internal subtrees:

Use RandomTree to generate a random tree of a certain size:

Possible Issues  (1)

NestTree[f,tree] applies the function to the leaves' data:

To apply the function directly to the tree itself, use Tree[tree,None]:

Wolfram Research (2021), NestTree, Wolfram Language function,


Wolfram Research (2021), NestTree, Wolfram Language function,


@misc{reference.wolfram_2021_nesttree, author="Wolfram Research", title="{NestTree}", year="2021", howpublished="\url{}", note=[Accessed: 27-November-2021 ]}


@online{reference.wolfram_2021_nesttree, organization={Wolfram Research}, title={NestTree}, year={2021}, url={}, note=[Accessed: 27-November-2021 ]}


Wolfram Language. 2021. "NestTree." Wolfram Language & System Documentation Center. Wolfram Research.


Wolfram Language. (2021). NestTree. Wolfram Language & System Documentation Center. Retrieved from