TreeTraversalOrder
is an option for TreeMap and related functions that specifies the order to visit subtrees.
Details
- Traversing a tree is also known as scanning a tree or tree search. A traversal order specifies the order subtrees are visited in functions such as TreeMap and TreeScan.
- There are many different traversal orders and variants, including pre-, in-, and post-order depth-first traversals and breadth-first traversals.
- General settings for TreeTraversalOrder can be given as:
-
Automatic use the default traversal order {tspec,vspec,hspec} base order tspec, vertical order vspec, horizontal order hspec spec any subset of tspec, vspec, hspec, with defaults for the rest - Settings for the base order tspec include:
-
"DepthFirst" traverse the entire subtree before traversing its next sibling "BreadthFirst","LevelOrder" visit nodes by level starting from the root "LeavesFirst" visit nodes by level starting from the leaves - The relevant disjoint sets of nodes for base orders tspec are:
- Settings for the vertical order vspec include:
-
"TopDown","OuterInner","PreOrder" visit parents before their children, starting from the root "BottomUp","InnerOuter","PostOrder" visit children before their parents, starting from the leaves - Additional settings for the vertical order vspec for "DepthFirst" tspec include:
-
"InOrder" visit parents after their first children - Settings for the horizontal order hspec include:
-
"LeftRight" visit nodes from left to right "RightLeft" visit nodes from right to left - If tspec is not specified, "DepthFirst" is used.
- If vspec is Automatic or not specified, "BottomUp" is used for "DepthFirst" and "LeavesFirst" tspec, following the standard behavior of Map and Scan. For "BreadthFirst" tspec, "TopDown" is used, following standard practice.
- If hspec is not specified, "LeftRight" is used.
Examples
open allclose allBasic Examples (3)
Scope (18)
Base Order (14)
"DepthFirst" (6)
A depth-first traversal visits nodes in a left-to-right, bottom-up order by default:
Specify a pre-order, depth-first traversal:
Specify an in-order, depth-first traversal:
By default, a post-order, depth-first traversal is used:
Visit parents after their first children:
Specify a right-to-left, depth-first traversal:
By default, a left-to-right, depth-first traversal is used:
Specify the vertical and horizontal orders for a depth-first traversal:
Properties & Relations (5)
Compare bottom-up variants of depth-first, breadth-first and leaves-first traversals:
Compare top-down variants of depth-first, breadth-first and leaves-first traversals:
A pre-order, depth-first traversal corresponds to the leftmost, outermost order of the positions:
Lexicographic order puts positions in numerical order first, then in order of increasing length:
A breadth-first traversal corresponds to the outermost, leftmost order of the positions:
Canonical order puts positions in order of increasing length first, then in numerical order:
A bottom-up, left-to-right traversal order is the reverse of a top-down, right-to-left traversal order:
Text
Wolfram Research (2021), TreeTraversalOrder, Wolfram Language function, https://reference.wolfram.com/language/ref/TreeTraversalOrder.html (updated 2024).
CMS
Wolfram Language. 2021. "TreeTraversalOrder." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2024. https://reference.wolfram.com/language/ref/TreeTraversalOrder.html.
APA
Wolfram Language. (2021). TreeTraversalOrder. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/TreeTraversalOrder.html