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 f gives {} or Hold[], no additional levels are added.
  • 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].
  • NestTree takes the same options as Tree.

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:

Options  (9)

Styling Individual Tree Elements  (2)

Specify the label for the generated tree element:

Specify labels and styles for subtrees by position:

Styling Entire Tree  (4)

Specify labels and styles for all subtrees:

Specify the base style:

Specify the style for the edges and the edges of the tree elements:

Specify the base style and styles for individual tree elements:

Tree Layout and Graphics Options  (3)

Specify the orientation for the root:

Specify a named embedding:

Specify Graphics options:

Applications  (10)

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 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 extracts the data from a JSON rule:

Define a function that extracts the children from a JSON rule:

Convert JSON rules 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:

Create a tree of permutations:

Create a CalkinWilf tree:

Create a SternBrocot tree:

Create a tree of terms in a linear recurrence:

Compute the terms of the linear recurrence:

Properties & Relations  (7)

NestTree applies a function f until the maximum level is reached or f returns {}:

NestTree[f,expr,n] is equivalent to applying Tree to expr and the list of results NestTree[f,expri,n-1] for the expri in f[expr]:

NestTree[TreeChildren,Tree[tree,None],Infinity,TreeData] is equivalent to tree:

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

Neat Examples  (2)

Construct a tree with levels giving the triangles in the 0^(th)- through 3^(rd)-step Sierpiński triangles:

Obtain the 3^(rd)-step Sierpiński triangle from this tree:

Construct a tree with levels giving the intervals in the 0^(th)- through 3^(rd)-step Cantor sets:

Obtain the 3^(rd)-step Cantor set from this tree:

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

Text

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

CMS

Wolfram Language. 2021. "NestTree." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/NestTree.html.

APA

Wolfram Language. (2021). NestTree. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/NestTree.html

BibTeX

@misc{reference.wolfram_2021_nesttree, author="Wolfram Research", title="{NestTree}", year="2021", howpublished="\url{https://reference.wolfram.com/language/ref/NestTree.html}", note=[Accessed: 24-May-2022 ]}

BibLaTeX

@online{reference.wolfram_2021_nesttree, organization={Wolfram Research}, title={NestTree}, year={2021}, url={https://reference.wolfram.com/language/ref/NestTree.html}, note=[Accessed: 24-May-2022 ]}