TreeLevel
Details and Options
- TreeLevel lists the subtrees on any range of levels in a Tree object. The list of subtrees can be given in many different orders, including depth-first and breadth-first traversals.
- TreeLevel[tree,levelspec] gives a list of the subtrees on the given levels. This can also be expressed as TreeLevel[tree,levelspec"Subtree"].
- TreeLevel[tree,levelspec"Data"] gives a list of the data on the given levels.
- TreeLevel uses standard level specifications:
-
n levels 1 through n All levels 0 through Infinity Infinity levels 1 through Infinity {n} level n only {n1,n2} levels n1 through n2 "Leaves" level -1 only "NonLeaves" levels 0 through -2 - A positive level n+1 consists of all subtrees of tree that are children of subtrees on level n.
- Level 0 corresponds to the root of the tree.
- Level -1 corresponds to the leaves.
- A negative level -(n+1) consists of all subtrees whose children are on levels -1, -2, …, -n, with highest level -n.
- TreeLevel traverses subtrees in a left-to-right, depth-first order, with children visited before their parents.
- TreeLevel[…,TreeTraversalOrderorder] allows visiting subtrees in different orders, such as depth-first and breadth-first traversals.
- TreeLevel[levelspec][tree] is equivalent to TreeLevel[tree,levelspec].
Examples
open all close allBasic Examples (3)
Extract the subtrees on the second level:
TreeLevel[[image], {2}]Extract the data on a range of levels:
TreeLevel[[image], {1, -2} -> "Data"]Use the operator form of TreeLevel:
TreeLevel[{1, 3}][[image]]Scope (8)
Level Specifications (7)
Extract the subtrees on all levels:
TreeLevel[[image], All]Extract the subtrees on levels 1, 2 and 3:
tree = [image];TreeLevel[tree, 3]Extract the subtrees only on level 3:
TreeLevel[tree, {3}]TreeLevel[tree, {0, 2}]Extract all levels, starting at level 1:
TreeLevel[[image], Infinity]TreeLevel[[image], {0, Infinity}]tree = [image];TreeLevel[tree, -1]TreeLevel[tree, -2]TreeLevel[tree, {-2}]Positive and negative levels can be mixed:
TreeLevel[[image], {1, -1}]TreeLevel[[image], "Leaves"]TreeLevel[[image], "NonLeaves"]Use TreeLevel as an operator form in a tree position specification:
TreeExtract[[image], {2, TreeLevel[{1}]}]Options (3)
TreeTraversalOrder (3)
By default, subtrees are listed in a depth-first order, with parents visited after their children:
TreeLevel[[image], All, TreeTraversalOrder -> "DepthFirst"]Specify a top-down, right-to-left variant:
TreeLevel[[image], All, TreeTraversalOrder -> {"DepthFirst", "TopDown", "RightLeft"}]List subtrees in a breadth-first order, with nodes on the same level from the root visited before the nodes on the next level:
TreeLevel[[image], All, TreeTraversalOrder -> "BreadthFirst"]List subtrees in a leaves-first order, with nodes on the same level from the leaves visited before the nodes on the next level:
TreeLevel[[image], All, TreeTraversalOrder -> "LeavesFirst"]Applications (2)
Properties & Relations (4)
TreeLevel[tree,levelspec] is equivalent to TreeChildren@TreeExtract[tree,TreeLevel[levelspec]]:
tree = RandomTree[16]TreeLevel[tree, {2, 4}]% === TreeChildren@TreeExtract[tree, TreeLevel[{2, 4}]]Level[expr,levelspec] is equivalent to TreeLevel[ExpressionTree[expr,"Subexpressions"],levelspec"Data"]:
Level[a[b[c, d[e, f]], g[h]], {0, -2}]ExpressionTree[a[b[c, d[e, f]], g[h]], "Subexpressions"]TreeLevel[%, {0, -2} -> "Data"]Level[expr,levelspec,HeadsTrue] is equivalent to TreeLevel[ExpressionTree[expr,"Subexpressions",HeadsTrue],levelspec"Data"] for positive levels:
Level[f[g, h][a, b, c], {1, 2}, Heads -> True]ExpressionTree[f[g, h][a, b, c], "Subexpressions", Heads -> True]TreeLevel[%, {1, 2} -> "Data"]TreeLeaves gives the subtrees at level -1:
tree = [image];TreeLeaves[tree] === TreeLevel[tree, {-1}]Possible Issues (1)
TreeLevel by default starts at level 1, so does not extract the whole tree:
TreeLevel[[image], -1]TreeLevel[[image], {0, -1}]Related Guides
Text
Wolfram Research (2021), TreeLevel, Wolfram Language function, https://reference.wolfram.com/language/ref/TreeLevel.html (updated 2022).
CMS
Wolfram Language. 2021. "TreeLevel." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2022. https://reference.wolfram.com/language/ref/TreeLevel.html.
APA
Wolfram Language. (2021). TreeLevel. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/TreeLevel.html
BibTeX
@misc{reference.wolfram_2026_treelevel, author="Wolfram Research", title="{TreeLevel}", year="2022", howpublished="\url{https://reference.wolfram.com/language/ref/TreeLevel.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_treelevel, organization={Wolfram Research}, title={TreeLevel}, year={2022}, url={https://reference.wolfram.com/language/ref/TreeLevel.html}, note=[Accessed: 13-June-2026]}