Tree Traversals
Systematic way of accessing all of the positions of a tree. An example of implementing these in an abstree Tree
datastructure is presented in Trees
Pre-Order, Post-Order, and In-Order
Pre-Order
in pre-order traversal the root of the tree is visited first, and then the subtrees rooted at its children are traversed recursively. If the tree is ordered, then the subtrees are traversed according to the order of the children.
1 2 3 4 5 |
|
Post-Order
Opposite of pre-order traversal, because it recursively traverses the subtrees rooted at the children of the root first, and then visits the root.
1 2 3 4 5 |
|
In-Order
This traversal is specific to binary trees. We visit a position between the recursive traversal of the left and right subtrees of a binary tree. i.e. visiting nodes from left to right
. For every position p
, inorder traversal visits p
after all of the positions in the left subtree of p
and before all the positions in the right subtree of p
.
1 2 3 4 5 6 7 |
|
Breadth First Search
Traverse a tree so that we visit all of the positions at a given depth before we visit the positions at the next depth/level.
We use a queue to produce a FIFO sematic for the order in which we visit nodes without making it recursive.
1 2 3 4 5 6 7 8 |
|
Depth First Search
Prim-Jarnik
Kruskal's
Euler Tours
The Euler tour traversal of a general tree T
is defined as a "walk" around T
, where we start by going from the root towards its leftmost child, viewing the edges of T
as being "walls" that we always keep to the left.
The complexity of the walk is O(n)
- it progresses exactly to times along each of the n-1
edges of the tree - once going downward along the edge, and later going upward along the edge.
To unify this with preorder
and postorder
traversals, we can think of there being two notable "visits" to each position p
:
- a
pre visit
occurs when first reaching the position, that is, whe the walk passes immediately left of the node in our visualization - a
post visit
occurs when the walk later proceeds upward from that position, that is, when the walk pass to the right of the node in our visualization
Euler tour can be done recursively such that in between the pre-visit and post-visit of a position will be a recursve tour of each of the subtrees.
1 2 3 4 5 6 |
|
A more complete implementation is below, where we use the Template Method to define the generic traversal algorithm, and use two hooks (auxiliary functions) - one for the previsit before the subtrees are traversed, and another for the postvisit after the completion of subtree traversals.
The hooks can be overridden to provide specialized behavior.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
|
Applications
TODO: Add notes here - leaving blank for now.