0% found this document useful (0 votes)
30 views18 pages

Lec 07 Tree Walks

Uploaded by

Saksham Rathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views18 pages

Lec 07 Tree Walks

Uploaded by

Saksham Rathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

CS213/293 Data Structure and Algorithms 2023

Lecture 7: Tree walks

Instructor: Ashutosh Gupta

IITB India

Compile date: 2023-08-20

cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 1
Application : Evaluating an expression
Example 7.1
If we want to evaluate an expression represented as a binary tree, we need to visit each node and
evaluate the expression in a certain order.

+ -18

* -20 / 2

-5 4 6 3
- 4 6 3

5 5
In green, we have evaluated the value of the node. The path indicates the order of evaluation.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 2
Topic 7.1

Tree walks

cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 3
Tree walks

Visiting nodes of a tree in a certain order are called tree walks.

There are two kinds of walks for trees.


▶ preorder: visit parent first
▶ postorder: visit children first

cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 4
Example: preorder

Example 7.2
Let a document be stored as a tree. We read the document in preorder.

Document Title

Chapter 1 Chapter 2

Section 1 Section 2 Section 1 Section 2

Subsection 1.1

cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 5
Preorder/Postorder walk

Algorithm 7.1: PreOrderWalk(n) Algorithm 7.2: PostOrderWalk(n)


1 visit(n); 1 for n′ ∈ children(n) do
2 for n′ ∈ children(n) do 2 PostOrderWalk(n’);
3 PreOrderWalk(n’); 3 visit(n);

The first example of expression evaluation is postorder walk.

Commentary: visit(v) is some action taken during the walk.

cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 6
Walking on ordered tree

How do we walk on an ordered tree?

For an ordered tree, we may visit children in the given order among siblings.

We may have choices to change the order of visits among ordered siblings.

Commentary: Our description of the algorithm works for both ordered and unordered trees. Our algorithm does not specify the order of visits of siblings for unordered trees.
Please pay attention to the subtle differences among trees, ordered trees, and binary trees.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 7
Topic 7.2

Walking binary trees

cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 8
Preorder/Postorder walk over binary trees

We have more structure in binary trees. Let us write the algorithm for walks again.

Algorithm 7.3: PreOrderWalk(n) Algorithm 7.4: PostOrderWalk(n)


1 if n == Null then 1 if n == Null then
2 return 2 return
3 visit(n); 3 PostOrderWalk(left(n));
4 PreOrderWalk(left(n)); 4 PostOrderWalk(right(n));
5 PreOrderWalk(right(n)); 5 visit(n);

cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 9
Inorder walk of binary trees
Definition 7.1
In an inorder walk of a binary tree, we visit the node after visiting the left subtree and before
visiting right subtree.

Algorithm 7.5: InOrderWalk(n)


1 if n == Null then
2 return
3 InOrderWalk(left(n));
4 visit(n);
5 InOrderWalk(right(n));

Exercise 7.1
Given a complete binary tree with 7 nodes, label the nodes so that the preorder, inorder, and
postorder traversals produce the sequence 1,2,...,7.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 10
Application : Printing an expression
To print an expression (without unary minus), we need to visit the nodes in inorder.
Algorithm 7.6: PrintExpression(n) +
1 if n is leaf then
2 print(label(n));
3 return * /
4 print(“(”);
5 PrintExpression(left(n));
6 print(label(n)); 5 4 6 3
7 PrintExpression(right(n));
8 print(“)”); ((5 * 4) + (6 / 3))

Exercise 7.2
a. Modify the above algorithm to support unary minus.
b. What will happen if “if” at line 1 is replaced by “if n == NULL then return”?
Commentary: The order of the walk is the pattern of recursive calls and actions on nodes. An application may need mixed action pattern. In the above printing example,
we need to print parentheses before and after making recursive calls. The parentheses are printed pre/post-order. All three walks are present in the above algorithm.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 11
Many trees have the same walks
The following two ordered trees have the same preorder walks.

Commentary: Answer:
A
B A A C

For postorder: B C B
A B

B C D A
C B C
For inorder: D E E C
A A

C C
Exercise 7.3 For postorder and preorder: B B
a. Give two binary trees that have the same postorder walks.
b. Give two binary trees that have the same inorder walks.
c. Give two binary trees that have the same postorder and preorder walks.
cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 12
Topic 7.3

Problems

cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 13
The uniqueness of walks if two walks are the same.

Exercise 7.4
Give an algorithm for reconstructing a binary tree if we have the preorder and inorder walks.

Exercise 7.5
Let us suppose all internal nodes of a binary tree have two children. Give an algorithm for
reconstructing the binary tree if we have the preorder and postorder walks.

cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 14
Exercise: previous print

Exercise 7.6
For a given binary tree, let prevPrint(T , a) give the node n′ such that label(n′ ) will appear just
before label(n) in the inorder printing of T. Give a program to compute prevloc.

cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 15
Reconstructing tree from preorder walks

Exercise 7.7
Let us suppose we can calculate the number of children of a node by looking at the label of a node
of a binary tree, e.g., arithmetic expressions. Give an algorithm for reconstructing the binary tree if
we have the preorder walk.

cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 16
Exercise: level-order walk

Exercise 7.8
Give an algorithm for walking a tree such that nodes are visited in the order of their level. Two
nodes at the same level can visit in any order.

Exercise 7.9
Give an algorithm for walking a tree such that nodes are visited in the order of their height.

cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 17
End of Lecture 7

cbna CS213/293 Data Structure and Algorithms 2023 Instructor: Ashutosh Gupta IITB India 18

You might also like