# NestTree NestTree[f,tree]

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

NestTree[f,tree,n]

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

NestTree[f,tree,n,h]

applies h to the data of the added subtrees.

# Details • 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].

# Examples

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]: