Tree traversals visit nodes in a tree in a specified order. A preorder traversal visits each node before its children, while a postorder traversal visits each node after its children. Binary trees can be traversed in preorder, postorder, or inorder. Preorder and inorder traversals can uniquely determine a binary tree, while preorder and postorder may not. Special cases like each internal node having two children can allow reconstruction from preorder and postorder.
In this document
Powered by AI
Introduces tree walks or traversals to visit nodes in specific orders. Explains preorder (process parents before children) and postorder (process children before parents) traversals.
Details the algorithm for preorder traversal with a recursive structure to visit nodes as encountered, akin to reading a document sequentially.
Describes postorder traversal, elaborating on the algorithm that processes children before the parent node, similar to the 'du' command in Unix.
Illustrates how to conduct preorder and postorder traversals specifically for binary trees, highlighting their recursive structures.
Provides examples of preorder and postorder sequences, elucidating visiting patterns of nodes during traversal.
Explains how postorder traversal can be specialized to evaluate arithmetic expressions stored within a binary tree structure.
Introduces inorder traversal, specifying a method to visit nodes between left and right children, defining its algorithm clearly.
Demonstrates an example of inorder traversal, showing the sequence in which nodes are visited in a binary tree.
Elucidates the Euler tour traversal as a generic method that encompasses preorder, inorder, and postorder traversals.
Details the Euler walk method for printing arithmetic expressions, outlining steps for formatting through traversal.
Explains the Template Method Pattern as a generic computation strategy that can be adapted through subclassing.
Presents a specialization of binary tree traversal for printing expressions, showing method overrides in a subclass.
Details a method for reconstructing a binary tree given its preorder and inorder traversal data.
Explains how postorder and inorder traversals can also be used to construct a binary tree.
Discusses the inadequacy of using only preorder and postorder traversals to uniquely identify a binary tree structure.
Describes a special case where having at least two children per internal node allows the unique identification of a binary tree.
Tree Walks/Traversals
Atree walk or traversal is a way of visiting
all the nodes in a tree in a specified order.
A preorder tree walk processes each node
before processing its children
A postorder tree walk processes each
node after processing its children
1
2.
Traversing Trees (preorder)
preorder traversal
Algorithm preOrder(v)
“visit” node v
for each child w of v do
recursively perform preOrder(w)
reading a document from beginning to end
2
3.
Traversing Trees (postorder)
postorder traversal
Algorithm postOrder(v)
for each child w of v do
recursively perform postOrder(w)
“visit” node v
du (disk usage) command in Unix
3
4.
Traversals of BinaryTrees
preorder(v)
if (v == null) then return
else visit(v)
preorder(v.leftchild())
preorder(v.rightchild())
postorder(v)
if (v == null) then return
else postorder(v.leftchild())
postorder(v.rightchild())
visit(v)
4
5.
Examples of preand postorder
a
We assume that we
are only printing the b e
data in a node when
d
we visit it. c
Preorder Postorder f
g
a b c d f g e c f g d be a
5
6.
Evaluating Arithmetic Expressions
specialization of a
postorder traversal
Algorithm evaluate(v)
if v is a leaf
return the variable stored at v
else
let o be the operator stored at v
x evaluate(v.leftChild())
y evaluate(v.rightChild())
return x o y 6
7.
Traversing Trees
Besides preorder and postorder, a third
possibility arises when v is visted between the
visit to the left ad right subtree.
Algorithm inOrder(v)
if (v == null) then return
else inOrder(v.leftChild())
visit(v)
inOrder(v.rightChild())
7
Euler Tour Traversal
generic traversal of a binary tree
the preorder, inorder, and postorder traversals are special
cases of the Euler tour traversal
“walk around” the tree and visit each node three times:
on the left
from below
on the right
9
10.
Printing an arithmeticexpression
Printing an arithmetic
expression - so called
Euler’s walk:
Print “(“ before
traversing the left
subtree, traverse it
Print the value of a
node
Traverse the right
subtree, print “)” after
traversing it
10
11.
Template Method Pattern
generic computation
mechanism that can be
specialized by redefining
certain steps
implemented by means of
an abstract Java class
with methods that can be
redefined by its
subclasses
11
Building tree frompre- and in- order
Given the preorder and
inorder traversals of a binary a
tree we can uniquely
determine the tree. b e
Preorder Inorder
c d
abcdfge cbfdgae
g
f
bcdfg cbfdg
d f g f d g
13
14.
Building tree frompost and inorder
In place of preorder we can use postorder.
The last node visited in the postorder
traversal is the root of the binary tree.
This can then be used to split in the
inorder traversal to identify the left and
right subtrees.
Procedure is similar to the one for
obtaining tree from preorder and inorder
traversals.
14
15.
Insufficiency of pre& postorder
Given the pre and postoder traversal of a
binary tree we cannot uniquely identify the
tree.
This is because there can be two trees
with the same pre and postorder
traversals.
a a
Preorder: a b c
b b
Postorder: c b a
c c
15
16.
A special case
If each internal node of the
binary tree has at least two
a
children then the tree can
be determined from the pre b e
and post order traversals.
Preorder postorder c d
g
abcdfge cfgdbea f
bcdfg cfgdb
d f g f g d
16