SlideShare a Scribd company logo
1 of 16
Download to read offline
1 
Tree Walks/Traversals 
… 
A tree 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
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
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
4 
Traversals of Binary Trees 
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)
5 
Examples of pre and postorder 
… 
We assume that we are only printing the data in a node when we visit it. 
Preorder Postorder 
a 
e 
b 
c 
d 
g 
f 
a 
b 
g 
d 
c 
f 
e 
c 
f 
e 
d 
g 
b 
a
6 
Evaluating Arithmetic Expressions 
… 
specialization of a postorder traversal 
Algorithmevaluate(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()) 
returnx o y
7 
Traversing Trees 
… 
Besides preorder and postorder, a third possibility arises when v is visted between the visit to the left ad right subtree. 
… 
AlgorithminOrder(v) 
if (v == null) then return 
else inOrder(v.leftChild()) 
visit(v) 
inOrder(v.rightChild())
8 
Inorder Example 
… 
Inorder 
a 
e 
b 
c 
d 
g 
f 
c 
b 
a 
d 
f 
g 
e
9 
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
10 
Printing an arithmetic expression 
… 
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
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
12 
Specializing Generic Binary Tree Traversal 
… 
printing an arithmetic expression 
public class PrintExpressionTraversal extends BinaryTreeTraversal{ 
... 
protected void external(Position p, TraversalResult r) 
{ System.out.print(p.element());} 
protected void left(Position p, TraversalResult r) 
{ System.out.print("(");} 
protected void below(Position p, TraversalResult r) 
{ System.out.print(p.element());} 
protected void right(Position p, TraversalResult r) 
{ System.out.print(")");} 
}
13 
Building tree from pre-and in-order 
… 
Given the preorder and inorder traversals of a binary tree we can uniquely determine the tree. 
Preorder Inorder 
a 
a b c d f g e 
c b f d g a e 
e 
b c d f g 
c b f d g 
b 
c 
d f g 
f d g 
d 
g 
f
14 
Building tree from post 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.
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. 
Preorder: a b c 
Postorder: c b a 
a 
b 
c 
a 
b 
c
16 
A special case 
… 
If each internal node of the binary tree has at least two children then the tree can be determined from the pre and post order traversals. 
Preorder postorder 
a b c d f g e 
b c d f g 
d f g 
c f g d b e a 
a 
e 
b 
c 
d 
g 
f 
c f g d b 
f g d

More Related Content

Similar to Lec7

Trees - Non Linear Data Structure
Trees - Non Linear Data StructureTrees - Non Linear Data Structure
Trees - Non Linear Data StructurePriyanka Rana
 
Breath first Search and Depth first search
Breath first Search and Depth first searchBreath first Search and Depth first search
Breath first Search and Depth first searchKirti Verma
 
2.5 bfs & dfs 02
2.5 bfs & dfs 022.5 bfs & dfs 02
2.5 bfs & dfs 02Krish_ver2
 
Admissions in india 2015
Admissions in india 2015Admissions in india 2015
Admissions in india 2015Edhole.com
 
Tree(Data Structure)
Tree(Data Structure)Tree(Data Structure)
Tree(Data Structure)FariaFerdowsy
 
Graph Traversals
Graph TraversalsGraph Traversals
Graph TraversalsMaryJacob24
 
Data structures final lecture 1
Data structures final  lecture 1Data structures final  lecture 1
Data structures final lecture 1Nazir Ahmed
 

Similar to Lec7 (10)

2.5 graph dfs
2.5 graph dfs2.5 graph dfs
2.5 graph dfs
 
Trees - Non Linear Data Structure
Trees - Non Linear Data StructureTrees - Non Linear Data Structure
Trees - Non Linear Data Structure
 
Binary trees
Binary treesBinary trees
Binary trees
 
Breath first Search and Depth first search
Breath first Search and Depth first searchBreath first Search and Depth first search
Breath first Search and Depth first search
 
2.5 bfs & dfs 02
2.5 bfs & dfs 022.5 bfs & dfs 02
2.5 bfs & dfs 02
 
Admissions in india 2015
Admissions in india 2015Admissions in india 2015
Admissions in india 2015
 
Tree(Data Structure)
Tree(Data Structure)Tree(Data Structure)
Tree(Data Structure)
 
Trees unit 3
Trees unit 3Trees unit 3
Trees unit 3
 
Graph Traversals
Graph TraversalsGraph Traversals
Graph Traversals
 
Data structures final lecture 1
Data structures final  lecture 1Data structures final  lecture 1
Data structures final lecture 1
 

More from Nikhil Chilwant (20)

The joyless economy
The joyless economyThe joyless economy
The joyless economy
 
I 7
I 7I 7
I 7
 
IIT Delhi training pioneer ct (acemicromatic )
IIT Delhi training pioneer ct (acemicromatic )IIT Delhi training pioneer ct (acemicromatic )
IIT Delhi training pioneer ct (acemicromatic )
 
Lec39
Lec39Lec39
Lec39
 
Lec38
Lec38Lec38
Lec38
 
Lec37
Lec37Lec37
Lec37
 
Lec36
Lec36Lec36
Lec36
 
Lec35
Lec35Lec35
Lec35
 
Lec34
Lec34Lec34
Lec34
 
Lec33
Lec33Lec33
Lec33
 
Lec32
Lec32Lec32
Lec32
 
Lec30
Lec30Lec30
Lec30
 
Lec29
Lec29Lec29
Lec29
 
Lec28
Lec28Lec28
Lec28
 
Lec27
Lec27Lec27
Lec27
 
Lec26
Lec26Lec26
Lec26
 
Lec25
Lec25Lec25
Lec25
 
Lec24
Lec24Lec24
Lec24
 
Lec23
Lec23Lec23
Lec23
 
Lec21
Lec21Lec21
Lec21
 

Lec7

  • 1. 1 Tree Walks/Traversals … A tree 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
  • 2. 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
  • 3. 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
  • 4. 4 Traversals of Binary Trees 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)
  • 5. 5 Examples of pre and postorder … We assume that we are only printing the data in a node when we visit it. Preorder Postorder a e b c d g f a b g d c f e c f e d g b a
  • 6. 6 Evaluating Arithmetic Expressions … specialization of a postorder traversal Algorithmevaluate(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()) returnx o y
  • 7. 7 Traversing Trees … Besides preorder and postorder, a third possibility arises when v is visted between the visit to the left ad right subtree. … AlgorithminOrder(v) if (v == null) then return else inOrder(v.leftChild()) visit(v) inOrder(v.rightChild())
  • 8. 8 Inorder Example … Inorder a e b c d g f c b a d f g e
  • 9. 9 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
  • 10. 10 Printing an arithmetic expression … 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
  • 11. 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
  • 12. 12 Specializing Generic Binary Tree Traversal … printing an arithmetic expression public class PrintExpressionTraversal extends BinaryTreeTraversal{ ... protected void external(Position p, TraversalResult r) { System.out.print(p.element());} protected void left(Position p, TraversalResult r) { System.out.print("(");} protected void below(Position p, TraversalResult r) { System.out.print(p.element());} protected void right(Position p, TraversalResult r) { System.out.print(")");} }
  • 13. 13 Building tree from pre-and in-order … Given the preorder and inorder traversals of a binary tree we can uniquely determine the tree. Preorder Inorder a a b c d f g e c b f d g a e e b c d f g c b f d g b c d f g f d g d g f
  • 14. 14 Building tree from post 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.
  • 15. 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. Preorder: a b c Postorder: c b a a b c a b c
  • 16. 16 A special case … If each internal node of the binary tree has at least two children then the tree can be determined from the pre and post order traversals. Preorder postorder a b c d f g e b c d f g d f g c f g d b e a a e b c d g f c f g d b f g d