0% found this document useful (0 votes)
11 views11 pages

Tree Traversal

Uploaded by

puravbeniwal4
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)
11 views11 pages

Tree Traversal

Uploaded by

puravbeniwal4
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/ 11

LECTURE NOTES

on

DATA STRUCTURES
2021 – 2022

B.Tech, IIIrd Sem e s t e r


Mr. Deepanshu Singh Yadav, Assistant Profes so r

Sitapur Road,Lucknow
Uttar Pradesh
India
Pin Code: 226021

INSTITUTE OF ENGINEERING
&TECHNOLOGY

Department of Computer Science and Engineering

Mr. Deepanshu Singh Yadav, Assistant Professor, IET, LUCKNOW Page 1


REFERENCES:
1. Aaron M. Tenenbaum, YedidyahLangsam and Moshe J. Augenstein “Data Structures Using C
and C++”, PHI Learning Private Limited, Delhi India.
2. Horowitz and Sahani, “Fundamentals of Data Structures”, Galgotia Publications Pvt Ltd Delhi
India.
3. A.K. Sharma ,Data Structure Using C, Pearson Education India.
4. Rajesh K. Shukla, “Data Structure Using C and C++” Wiley Dreamtech Publication
5. Lipschutz, “Data Structures” Schaum’s Outline Series, Tata Mcgraw-hill Education (India) Pvt.
Ltd
6. Michael T. Goodrich, Roberto Tamassia, David M. Mount “Data Structures and Algorithms in
C++”, Wiley India.
7. P.S. Deshpandey, “C and Datastructure”, Wiley Dreamtech Publication.
8. R. Kruse etal, “Data Structures and Program Design in C”, Pearson Education.
9. Berztiss, A.T.: Data structures, Theory and Practice :, Academic Press.
10. codeforwin.org/
11. www.programiz.com/
12.
13. Tutorials point

Disclaimer: The e-content is exclusively meant for academic purposes and for enhancing teaching
and learning. Any other use for economic/commercial purpose is strictly prohibited. The users of the
content shall not distribute, disseminate or share it with anyone else and its use is restricted to
advancement of individual knowledge.
The information provided in this e-content is developed from authentic references, to the best of my
knowledge

Mr. Deepanshu Singh Yadav, Assistant Professor, IET, LUCKNOW Page 2


Binary Tree Traversal
 Binary tree traversing is a process of accessing every node of the tree and exactly once. A tree
is defined in a recursive manner. Binary tree traversal also defined recursively.

There are three techniques of traversal:

1. Preorder Traversal
2. Postorder Traversal
3. Inorder Traversal

1. Preorder Traversal
Algorithm for preorder traversal

Step 1 : Start from the Root.

Step 2 : Then, go to the Left Subtree.

Step 3 : Then, go to the Right Subtree.


Root → Left → Right
The algorithm for preorder traversal is as follows:
void preorder(node *root)
{
if( root != NULL )
{
print root -> data;
preorder (root -> lchild);
preorder (root -> rchild);
}
}

The above figure represents how preorder traversal actually works.

Mr. Deepanshu Singh Yadav, Assistant Professor, IET, LUCKNOW Page 3


Following steps can be defined the flow of preorder traversal:

Step 1 : A + B (B + Preorder on D (D + Preorder on E and F)) + C (C + Preorder on G and H)

Step 2 : A + B + D (E + F) + C (G + H)

Step 3 : A + B + D + E + F + C + G + H

Preorder Traversal : A B C D E F G H

Example 2
Consider the following example-

Preorder Traversal Shortcut

Traverse the entire tree starting from the root node keeping
yourself to the left.

Applications-
 Preorder traversal is used to get prefix expression of an expression tree.
 Preorder traversal is used to create a copy of the tree.

Mr. Deepanshu Singh Yadav, Assistant Professor, IET, LUCKNOW Page 4


2. Postorder Traversal
Algorithm for postorder traversal

Step 1 : Start from the Left Subtree (Last Leaf).

Step 2 : Then, go to the Right Subtree.

Step 3 : Then, go to the Root.


Left → Right → Root

void postorder(node *root)


{
if( root != NULL )
{
postorder (root -> lchild); postorder (root -> rchild); print (root -> data);
}
}

The above figure represents how postorder traversal actually works.

Following steps can be defined the flow of postorder traversal:

Step 1 : As we know, preorder traversal starts from left subtree (last leaf) ((Postorder on E + Postorder
on F) + D + B )) + ((Postorder on G + Postorder on H) + C) + (Root A)

Step 2 : (E + F) + D + B + (G + H) + C + A

Step 3 : E + F + D + B + G + H + C + A

Postorder Traversal : E F D B G H C A

Mr. Deepanshu Singh Yadav, Assistant Professor, IET, LUCKNOW Page 5


Example-
Consider the following example-

Postorder Traversal Shortcut

Pluck all the leftmost leaf nodes one by one.

Applications-

 Postorder traversal is used to get postfix expression of an expression tree.


 Postorder traversal is used to delete the tree.
 This is because it deletes the children first and then it deletes the parent.

Mr. Deepanshu Singh Yadav, Assistant Professor, IET, LUCKNOW Page 6


3. Inorder Traversal
Algorithm for inorder traversal

Step 1 : Start from the Left Subtree.

Step 2 : Then, visit the Root.

Step 3 : Then, go to the Right Subtree.

Left → Root → Right


The algorithm for inorder traversal is as follows:
void inorder(node *root)
{
if(root != NULL)
{
inorder(root->lchild);

print root -> data; inorder(root->rchild);


}
}

The above figure represents how inorder traversal actually works.

Following steps can be defined the flow of inorder traversal:

Step 1 : B + (Inorder on E) + D + (Inorder on F) + (Root A ) + (Inorder on G) + C (Inorder on H)

Step 2 : B + (E) + D + (F) + A + G + C + H

Step 3 : B + E + D + F + A + G + C + H

Inorder Traversal : Tree Traversal-

Mr. Deepanshu Singh Yadav, Assistant Professor, IET, LUCKNOW Page 7


Example 2
Consider the following example-

Inorder Traversal Shortcut

Keep a plane mirror horizontally at the bottom of the tree and take the
projection of all the nodes.

Application-
 Inorder traversal is used to get infix expression of an expression tree.

Mr. Deepanshu Singh Yadav, Assistant Professor, IET, LUCKNOW Page 8


Breadth First Traversal-

 Breadth First Traversal of a tree prints all the nodes of a tree level by level.
 Breadth First Traversal is also called as Level Order Traversal.

Example-

Application-

 Level order traversal is used to print the data in the same order as stored in the array representation of
a complete binary tree.

Mr. Deepanshu Singh Yadav, Assistant Professor, IET, LUCKNOW Page 9


Example 1:
Traverse the following binary tree in pre, post, inorder and level order.

A Preorder traversal yields:


A, B, D, C, E, G, F, H, I
B C Postorder traversal yields:
D, B, G, E, H, I, F, C, A
D E F
Inorder traversal yields:
G H I
D, B, A, E, G, C, H, F, I

Level order traversal yields:


A, B, C, D, E, F, G, H, I

Binary T re e Pre, Post, Inorder and level order Traversing

Example 2:
Traverse the following binary tree in pre, post, inorder and level order.

Preorder traversal yields:


P, F, B, H, G, S, R, Y, T, W, Z

Postorder traversal yields:


B H R Y B, G, H, F, R, W, T, Z, Y, S, P

Inorder traversal yields:


B, F, G, H, P, R, S, T, W, Y, Z

Level order traversal yields:


P, F, S, B, H, R, Y, G, T, Z, W

Binary T re e Pre, Post, Inorder and level order Traversing

Mr. Deepanshu Singh Yadav, Assistant Professor, IET, LUCKNOW Page 10


Example 3:
Traverse the following binary tree in pre, post, inorder and level order.

Preorder traversal yields:


A, B, D, G, K, H, L, M, C, E

Postorder traversal yields:


K, G, L, M, H, D, B, E, C, A

Inorder traversal yields:


K, G, D, L, H, M, B, A, E, C

Level order traversal yields:


A, B, C, D, E, G, H, K, L, M

Binary T re e Pre, Post, Inorder and level order Traversing

Mr. Deepanshu Singh Yadav, Assistant Professor, IET, LUCKNOW Page 11

You might also like