Binary Tree Traversal

DhrumilPanchal4 12,180 views 18 slides Oct 05, 2018
Slide 1
Slide 1 of 18
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18

About This Presentation

This presentation is useful to study about data structure and topic is Binary Tree Traversal. This is also useful to make a presentation about Binary Tree Traversal.


Slide Content

Name : Panchal Dhrumil Indravadan Subject : Data Structure Branch : Computer Engineering (B.E.) Semester : Third Sem Year : 2018-19 WELCOME

Tree Traversal T opic

Binary Tree Traversal Preorder Inorder Postorder Example C ontain

The most common operations performed on tree structure is that of traversal. This is a procedure by which each node in the tree is processed exactly once in a systematic manner. There are three ways of traversing a binary tree. 1. Preorder Traversal 2. Inorder Traversal 3. Postorder Traversal Binary Tree Traversal

Preorder traversal : A B C D E F G Inorder traversal : C B A E F D G Postorder traversal : C B F E G D A Converse Preorder traversal : A D G E F B C Converse Inorder traversal : G D F E A B C Converse Postorder traversal : G F E D C B A A B C D E F G

Preorder traversal of a binary tree is defined as follow Process the root node Traverse the left subtree in preorder Traverse the right subtree in preorder If particular subtree is empty (i.e., node has no left or right descendant) the traversal is performed by doing nothing, In other words, a null subtree is considered to be fully traversed when it is encountered. The preorder traversal of a tree is given by A B C D E F G Preorder

The Inorder traversal of a binary tree is given by following steps, Traverse the left subtree in Inorder Process the root node Traverse the right subtree in Inorder The Inorder traversal of a tree is given by C B A E F D G Inorder

The postorder traversal is given by Traverse the left subtree in postorder Traverse the right subtree in postorder Process the root node The Postorder traversal of a tree is given by C B F E G D A Postorder

If we interchange left and right words in the preceding definitions, we obtain three new traversal orders which are called Converse Preorder (A D G E F B C) Converse Inorder (G D F E A B C) Converse Postorder (G F E D C B A) Converse …

Given a binary tree whose root node address is given by pointer variable root and whose node structure is same as described below. This procedure traverses the tree in preorder, in a recursive manner. Procedure : preorder(root) ROOT LPTR RPTR

void preorder (root) { if (root==NULL) return; printf (“ %d\n ”, root->info); preorder ( root-> left); preorder ( root-> right); }

Given a binary tree whose root node address is given by pointer variable “root” and whose node structure is same as described below. This procedure traverses the tree in inorder , in a recursive manner. Procedure : inorder(root->left) LPTR ROOT RPTR

void inorder (root->left) { if (root==NULL) return; inorder ( root); printf (“ %d\n ”, root->info); inorder ( root-> right); }

Given a binary tree whose root node address is given by pointer variable “root” and whose node structure is same as described below. This procedure traverses the tree in postorder , in a recursive manner. Procedure : postorder(root->left) LPTR RPTR ROOT

void postorder (root->left) { if (root==NULL) return; postorder (root-> right); postorder (root); printf (“ %d\n ”, root->info); }

Inorder: 2 1 4 5 3 Preorder: 1 2 3 4 5 Post order: 2 5 4 3 1 Give traversal order of following tree into Inorder, Preorder and Postorder. 1 2 3 4 5

Inspiration from Prof. Keyur Suthar and Prof. Rashmin Prajapati Notes of DS Textbook of DS Image from Google images Some my own knowledge R eferences

#include< stdio.h > int main() { printf (“Thank You”); return 0; } Thank You