Tree

Tree[{subtree1,subtree2,}]

represents a tree with a list of child subtrees subtreei.

Tree[data,subtrees]

represents a tree containing data in its root, with children given by subtrees.

Details

  • Tree objects represent rooted, ordered, labeled trees. Standard examples include XML trees, file systems and Wolfram Language expressions.
  • In Tree[data,subtrees], the subtrees are other Tree expressions or general expressions that are interpreted as leaves.
  • Tree[] displays in a notebook as a plot of a tree graph:
  • Tree[] is always converted to a standard form with structure Tree[data,subtrees].
  • Tree[subtrees] is equivalent to Tree[Null,subtrees].
  • Tree is treated as a raw object by functions like AtomQ.
  • Options affecting individual tree elements or nodes include:
  • TreeElementLabellabels for subtree elements
    TreeElementStylestyles for subtree elements
  • Options affecting the entire tree include:
  • TreeLayouthow to lay out subtrees and edges
  • Tree has all options shared by Graph and Graphics.

Examples

open allclose all

Basic Examples  (4)

A tree with two leaves:

A tree with multiple levels:

A tree with data in the leaves:

A tree containing another tree as data:

Scope  (2)

Represent a leaf with no child:

Specify children in a list:

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  (8)

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:

Convert an XML tree to an XMLElement:

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:

Convert a JSON tree to rules:

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:

Convert a text tree to a TextElement:

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  (6)

A root node of a tree does not need to have data:

RulesTree returns a Tree expression:

Check whether an expression with head Tree is a valid Tree object:

This is not a valid Tree object:

However, this is a valid Tree object that contains a child with an invalid Tree expression as data:

Construct a Tree expression with ExpressionTree:

Construct a Tree expression with GraphTree:

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

Text

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

CMS

Wolfram Language. 2021. "Tree." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 13. https://reference.wolfram.com/language/ref/Tree.html.

APA

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

BibTeX

@misc{reference.wolfram_2021_tree, author="Wolfram Research", title="{Tree}", year="13", howpublished="\url{https://reference.wolfram.com/language/ref/Tree.html}", note=[Accessed: 08-December-2021 ]}

BibLaTeX

@online{reference.wolfram_2021_tree, organization={Wolfram Research}, title={Tree}, year={13}, url={https://reference.wolfram.com/language/ref/Tree.html}, note=[Accessed: 08-December-2021 ]}