TreeFold

TreeFold[f,tree]

gives f[data,{res1,res2,}], where data is the data in tree and resi is the result of TreeFold[f,childi] for the i^(th) child of tree.

TreeFold[{f,g},tree]

gives g[data] if tree is a leaf and f[data,{res1,res2,}] otherwise.

TreeFold[f,tree,h]

gives f[h[tree],{res1,res2,}], where resi is the result of TreeFold[f,childi,h] for the i^(th) child of tree.

TreeFold[f]

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

Details

Examples

open allclose all

Basic Examples  (4)

Fold a function f on a tree:

Start by applying a separate function g on the tree leaves:

Convert a tree of solid regions into a CSGRegion:

Create a visualization of a tree:

Scope  (6)

Fold a leaf:

Fold a tree with several levels:

Specify separate functions for the internal subtrees and the leaves:

Fold a property of subtrees:

Use the operator form of TreeFold on one argument:

Use the operator form of TreeFold on two arguments:

Applications  (14)

Get a nested list of the leaves of a tree:

Construct a tree from the atoms of an expression:

Convert this tree to nested lists:

Total the data in the leaves of a tree:

Total the data in a tree:

Get a tree of subtotals, starting from the leaves:

Create a tree of terms in a linear recurrence:

Compute the terms of the linear recurrence:

Convert a tree to an XMLElement:

Convert a tree to JSON rules:

Convert a tree to a TextElement:

Pass the current level as an additional argument:

Fold the tree:

Linearly fold the data starting from the root with an initial seed:

Fold the tree:

Get a tree of subtotals, starting from the root:

Compute the minimum level of leaves in a tree using TreeFold with this function:

Display an outline of a tree:

Get a list of US cities with populations over 100,000:

Construct a graph giving the hierarchical clustering of the cities according to their geodetic positions:

Convert the clustering hierarchy from a Graph object to a Tree object:

For each leaf, obtain the geodetic position of a city from its index in the hierarchical clustering graph:

For each subtree representing a cluster, give the tree containing the spatial median of its children:

Obtain a tree of geodetic positions by using the position of a city for each leaf and the spatial median of the positions for each cluster:

Show the edges in this tree of geodetic positions on a map of the United States:

Create a family tree:

Create an association giving the dates of birth:

Define a function that compares two people, giving the person who was younger when they had their first child, together with that child and their age when that child was born:

Define a function that compares two siblings, giving the older sibling together with their date of birth, in addition to the youngest first-time parent among their descendants:

Define a function that gives the youngest first-time parent among a person's descendants, given the results for their children:

Find the youngest first-time parent in the tree, together with their first child and their age when the child was born:

Properties & Relations  (14)

Folding a leaf uses an empty list of values for its children:

TreeFold[{f,g},Tree[data,None]] applies the function g to data instead:

TreeFold[f,Tree[data,{child1,}]] is equivalent to applying f to data and the list of results TreeFold[f,childi]:

TreeFold[Tree,tree] is equivalent to tree:

Construct a tree from the heads in an expression:

Use TreeFold to insert a parent node above each subtree:

This corresponds to mapping on the arguments in an expression:

Map maps on the arguments in an expression by default:

Construct a tree from the atoms in an expression:

Use TreeFold to insert a sibling node before each subtree:

This corresponds to mapping on the subexpressions in an expression:

Map maps on the subexpressions in an expression with HeadsTrue:

The functions f and g can be replaced by a single function in TreeFold[{f,g},tree] :

TreeFold[fg,tree] is equivalent to TreeFold[{f,g},tree]:

TreeFold[f,tree] is equivalent to TreeFold[{f,f[#,{}]&},tree]:

TreeFold[{f,g},tree] is equivalent to TreeFold[{f,Identity},TreeMap[g,tree,{-1}]]:

First map the function g on the leaves of the tree:

Then reduce the tree using the function f for internal subtrees and Identity for the leaves:

TreeFold[f,tree,h] is equivalent to TreeFold[g,tree] with g a combination of f and h that recomputes the tree:

Fold applied to a list {a,b,} is equivalent to TreeFold applied to the tree

:

TreeFold can be used to compute the maximum level of a tree:

TreeFold can be used to compute the size of a tree:

TreeFold can be used to convert a tree to nested rules:

TreeFold can be used to create an expression from the leaves of a tree:

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), TreeFold, Wolfram Language function, https://reference.wolfram.com/language/ref/TreeFold.html.

Text

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

CMS

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

APA

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

BibTeX

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

BibLaTeX

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