Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Unit 4 Tree (Tree Traversal)

The document discusses tree traversal techniques in data structures, specifically Preorder, Postorder, and Inorder traversals. Each technique is explained with its traversal order, applications, algorithms, and examples. Additionally, it covers the time and space complexity of these techniques and provides a C program implementation for each traversal method.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Unit 4 Tree (Tree Traversal)

The document discusses tree traversal techniques in data structures, specifically Preorder, Postorder, and Inorder traversals. Each technique is explained with its traversal order, applications, algorithms, and examples. Additionally, it covers the time and space complexity of these techniques and provides a C program implementation for each traversal method.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Tree traversal (Inorder, Preorder an Postorder)

In this article, we will discuss the tree traversal in the data structure. The term 'tree traversal' means traversing or
visiting each node of a tree. There is a single way to traverse the linear data structure such as linked list, queue,
and stack. Whereas, there are multiple ways to traverse a tree that are listed as follows -

o Preorder traversal
o Inorder traversal
o Postorder traversal
So, in this article, we will discuss the above-listed techniques of traversing a tree. Now, let's start discussing the
ways of tree traversal.

Preorder traversal

This technique follows the 'root left right' policy. It means that, first root node is visited after that the left subtree is
traversed recursively, and finally, right subtree is recursively traversed. As the root node is traversed before (or
pre) the left and right subtree, it is called preorder traversal.

So, in a preorder traversal, each node is visited before both of its subtrees.

The applications of preorder traversal include -

o It is used to create a copy of the tree.


o It can also be used to get the prefix expression of an expression tree.
Algorithm

1. Until all nodes of the tree are not visited


2.
3. Step 1 - Visit the root node
4. Step 2 - Traverse the left subtree recursively.
5. Step 3 - Traverse the right subtree recursively.
Example

Now, let's see the example of the preorder traversal technique.

Now, start applying the preorder traversal on the above tree. First, we traverse the root node A; after that, move
to its left subtree B, which will also be traversed in preorder.
So, for left subtree B, first, the root node B is traversed itself; after that, its left subtree D is traversed. Since
node D does not have any children, move to right subtree E. As node E also does not have any children, the
traversal of the left subtree of root node A is completed.

Now, move towards the right subtree of root node A that is C. So, for right subtree C, first the root node C has
traversed itself; after that, its left subtree F is traversed. Since node F does not have any children, move to the
right subtree G. As node G also does not have any children, traversal of the right subtree of root node A is
completed.

Therefore, all the nodes of the tree are traversed. So, the output of the preorder traversal of the above tree is -

A→B→D→E→C→F→G

To know more about the preorder traversal in the data structure, you can follow the link Preorder traversal.

Postorder traversal

This technique follows the 'left-right root' policy. It means that the first left subtree of the root node is traversed,
after that recursively traverses the right subtree, and finally, the root node is traversed. As the root node is
traversed after (or post) the left and right subtree, it is called postorder traversal.

So, in a postorder traversal, each node is visited after both of its subtrees.

The applications of postorder traversal include -

o It is used to delete the tree.


o It can also be used to get the postfix expression of an expression tree.
Algorithm

1. Until all nodes of the tree are not visited


2.
3. Step 1 - Traverse the left subtree recursively.
4. Step 2 - Traverse the right subtree recursively.
5. Step 3 - Visit the root node.
Example

Now, let's see the example of the postorder traversal technique.

Now, start applying the postorder traversal on the above tree. First, we traverse the left subtree B that will be
traversed in postorder. After that, we will traverse the right subtree C in postorder. And finally, the root node of
the above tree, i.e., A, is traversed.
So, for left subtree B, first, its left subtree D is traversed. Since node D does not have any children, traverse the
right subtree E. As node E also does not have any children, move to the root node B. After traversing node B, the
traversal of the left subtree of root node A is completed.

Now, move towards the right subtree of root node A that is C. So, for right subtree C, first its left subtree F is
traversed. Since node F does not have any children, traverse the right subtree G. As node G also does not have
any children, therefore, finally, the root node of the right subtree, i.e., C, is traversed. The traversal of the right
subtree of root node A is completed.

At last, traverse the root node of a given tree, i.e., A. After traversing the root node, the postorder traversal of the
given tree is completed.

Therefore, all the nodes of the tree are traversed. So, the output of the postorder traversal of the above tree is -

D→E→B→F→G→C→A

To know more about the postorder traversal in the data structure, you can follow the link Postorder traversal.

Inorder traversal

This technique follows the 'left root right' policy. It means that first left subtree is visited after that root node is
traversed, and finally, the right subtree is traversed. As the root node is traversed between the left and right
subtree, it is named inorder traversal.

So, in the inorder traversal, each node is visited in between of its subtrees.

The applications of Inorder traversal includes -

o It is used to get the BST nodes in increasing order.


o It can also be used to get the prefix expression of an expression tree.
Algorithm

1. Until all nodes of the tree are not visited


2.
3. Step 1 - Traverse the left subtree recursively.
4. Step 2 - Visit the root node.
5. Step 3 - Traverse the right subtree recursively.
Example:
Now, start applying the inorder traversal on the above tree. First, we traverse the left subtree B that will be
traversed in inorder. After that, we will traverse the root node A. And finally, the right subtree C is traversed in
inorder.

So, for left subtree B, first, its left subtree D is traversed. Since node D does not have any children, so after
traversing it, node B will be traversed, and at last, right subtree of node B, that is E, is traversed. Node E also
does not have any children; therefore, the traversal of the left subtree of root node A is completed.

After that, traverse the root node of a given tree, i.e., A.

At last, move towards the right subtree of root node A that is C. So, for right subtree C; first, its left subtree F is
traversed. Since node F does not have any children, node C will be traversed, and at last, a right subtree of node
C, that is, G, is traversed. Node G also does not have any children; therefore, the traversal of the right subtree of
root node A is completed.

As all the nodes of the tree are traversed, the inorder traversal of the given tree is completed. The output of the
inorder traversal of the above tree is -

D→B→E→A→F→C→G

To know more about the inorder traversal in data structure, you can follow the link Inorder Traversal.

Complexity of Tree traversal techniques


The time complexity of tree traversal techniques discussed above is O(n), where 'n' is the size of binary tree.

Whereas the space complexity of tree traversal techniques discussed above is O(1) if we do not consider the
stack size for function calls. Otherwise, the space complexity of these techniques is O(h), where 'h' is the tree's
height.

Implementation of Tree traversal


Program: Write a program to implement tree traversal techniques in C.

#include <stdio.h>
#include <stdlib.h>

struct node {
int data;
struct node* left;
struct node* right;
};

/*To create a new node*/


struct node* createNode(int val)
{
struct node* Node = (struct node*)malloc(sizeof(struct node));
Node->data = val;
Node->left = NULL;
Node->right = NULL;

return (Node);
}

/*function to traverse the nodes of binary tree in preorder*/


void traversePreorder(struct node* root)
{
if (root == NULL)
return;
printf(" %d ", root->data);
traversePreorder(root->left);
traversePreorder(root->right);
}

/*function to traverse the nodes of binary tree in Inorder*/


void traverseInorder(struct node* root)
{
if (root == NULL)
return;
traverseInorder(root->left);
printf(" %d ", root->data);
traverseInorder(root->right);
}

/*function to traverse the nodes of binary tree in postorder*/


void traversePostorder(struct node* root)
{
if (root == NULL)
return;
traversePostorder(root->left);
traversePostorder(root->right);
printf(" %d ", root->data);
}

int main()
{
struct node* root = createNode(36);
root->left = createNode(26);
root->right = createNode(46);
root->left->left = createNode(21);
root->left->right = createNode(31);
root->left->left->left = createNode(11);
root->left->left->right = createNode(24);
root->right->left = createNode(41);
root->right->right = createNode(56);
root->right->right->left = createNode(51);
root->right->right->right = createNode(66);

printf("\n The Preorder traversal of given binary tree is -\n");


traversePreorder(root);

printf("\n The Inorder traversal of given binary tree is -\n");


traverseInorder(root);

printf("\n The Postorder traversal of given binary tree is -\n");


traversePostorder(root);

return 0;
}
Output

You might also like