Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 24

Tree Traversal

Traversal is a process to visit all the nodes of a tree and


may print their values too. Because, all nodes are
connected via edges (links) we always start from the root
(head) node. That is, we cannot randomly access a node
in a tree. There are three ways which we use to traverse a
tree −

•In-order Traversal
•Pre-order Traversal
•Post-order Traversal

Generally, we traverse a tree to search or locate a given


item or key in the tree or to print all the values it contains.
In-order Traversal
In this traversal method, the left subtree is visited first,
then the root and later the right sub-tree. We should
always remember that every node may represent a
subtree itself.
If a binary tree is traversed in-order, the output will
produce sorted key values in an ascending order.
We start from A, and following in-order traversal, we move
to its left subtree B. B is also traversed in-order. The
process goes on until all the nodes are visited. The output
of inorder traversal of this tree will be −
D→B→E→A→F→C→G

Algorithm
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Visit root node.
Step 3 − Recursively traverse right subtree.
Pre-order Traversal
In this traversal method, the root node is visited first, then
the left subtree and finally the right subtree.
We start from A, and following pre-order traversal, we first
visit A itself and then move to its left subtree B. B is also
traversed pre-order. The process goes on until all the
nodes are visited. The output of pre-order traversal of this
tree will be −
A→B→D→E→C→F→G

Algorithm
Until all nodes are traversed −
Step 1 − Visit root node.
Step 2 − Recursively traverse left subtree.
Step 3 − Recursively traverse right subtree.
Post-order Traversal
In this traversal method, the root node is visited last,
hence the name. First we traverse the left subtree, then
the right subtree and finally the root node.
We start from A, and following Post-order traversal, we
first visit the left subtree B. B is also traversed post-order.
The process goes on until all the nodes are visited. The
output of post-order traversal of this tree will be −
D→E→B→F→G→C→A

Algorithm
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Recursively traverse right subtree.
Step 3 − Visit root node.
#include <stdio.h>
#include <stdlib.h>

struct node {
int item;
struct node* left;
struct node* right;
};
// Create a new Node
struct node* createNode(value) {
struct node* newNode = malloc(sizeof(struct node));
newNode->item = value;
newNode->left = NULL;
newNode->right = NULL;

return newNode;
}
// Insert on the left of the node
struct node* insertLeft(struct node* root, int value) {
root->left = createNode(value);
return root->left;
}
// Insert on the right of the node
struct node* insertRight(struct node* root, int value) {
root->right = createNode(value);
return root->right;
}
#include <stdio.h>
#include <stdlib.h>

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

// Inorder traversal
void inorderTraversal(struct node* root) {
if (root == NULL) return;
inorderTraversal(root->left);
printf("%d ->", root->item);
inorderTraversal(root->right);
}
// Preorder traversal
void preorderTraversal(struct node* root) {
if (root == NULL) return;
printf("%d ->", root->item);
preorderTraversal(root->left);
preorderTraversal(root->right);
}

// Postorder traversal
void postorderTraversal(struct node* root) {
if (root == NULL) return;
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ->", root->item);
}
// Create a new Node
struct node* createNode(value) {
struct node* newNode = malloc(sizeof(struct node));
newNode->item = value;
newNode->left = NULL;
newNode->right = NULL;

return newNode;
}

// Insert on the left of the node


struct node* insertLeft(struct node* root, int value) {
root->left = createNode(value);
return root->left;
}
// Insert on the right of the node
struct node* insertRight(struct node* root, int value) {
root->right = createNode(value);
return root->right;
}

int main() {
struct node* root = createNode(1);
insertLeft(root, 2);
insertRight(root, 3);
insertLeft(root->left, 4);

printf("Inorder traversal \n");


inorderTraversal(root);

printf("\nPreorder traversal \n");


preorderTraversal(root);

printf("\nPostorder traversal \n");


postorderTraversal(root);
}
Non Recursive Tree Traversal Algorithm

POSTORD(INFO, LEFT, RIGHT, ROOT)


Step-1 [Push NULL onto STACK and initialize PTR]
Set TOP=1, STACK[1]=NULL and PTR=ROOT.

Step-2 [Push left-most path onto STACK] repeat step 3 to 5


while PTR not equal NULL.

Step-3 set TOP=TOP+1 and STACK[TOP]=PTR. [Pushes PTR


on STACK].

Step-4 if RIGHT[PTR] not equal NULL then [push on STACK.]


Set TOP=TOP+1 and STACK[TOP]= RIGHT[PTR]
[End of if structure]

Step-5 set PTR = LEFT[PTR].[Update pointer PTR]


[End of step 2 loop].
Step-6 set PTR= STACK[TOP] and TOP=TOP-1.
[Pops node from STACK].

Step-7 Repeat while PTR>0;


Apply PROCESS TO INFO[PTR].
Set PTR= STACK[TOP] and TOP= TOP-1.
[Pops node from STACK]
[End of loop]

Step-8 if PTR<0 then:


Set PTR= -PTR
Go to step 2
[End of if structure]

Step-9 Exit.
INORD( INFO, LEFT, RIGHT, ROOT)
Step-1 [Push NULL onto STACK and initialize PTR;]
Set TOP=1 STACK[1]= NULL and PTR=ROOT.

Step-2 Repeat while PTR not equal NULL[Pushes left most


path onto STACK].
a) Set TOP=TOP+1 and STACK[TOP]=PTR [saves node]
b) Set PTR=LEFT[PTR] [updates PTR]
[End of loop]

Step-3 set PTR= STACK[TOP and TOP=TOP-1.[pops node


from STACK].

Step-4 Repeat step 5 to 7 while PTR is not equal to Null;


[backtracking].

Step-5 Apply PROCESS to INFO[PTR],


Step-6 [RIGHT child?] if RIGHT[PTR] is not equal NULL
then.
a) Set PTR=RIGHT[PTR]
b) Go to step 3.[End of if structure.]

Step-7 set PTR= STACK[TOP] and TOP=TOP-1. [pops node]


[End of step 4 loop]

Step-8 Exit.
PREORDER( INFO, LEFT,RIGHT, ROOT)
Step-1 [initially push NULL onto stack and initialize PTR.]
Set TOP=1 STACK[1]=NULL and PTR= ROOT.

Step-2 Repeat step 3 to 5 while PTR not equal NULL.

Step-3 Apply PROCESS to INFO[PTR].

Step-4 [RIGHT child?]


If RIGHT[PTR] not equal NULL then [PUSH on STACK]
Set TOP= TOP+1 and STACK[TOP]= RIGHT[PTR]
[End of if structure.]
Step-5 [LEFT child?]
If LEFT[PTR] not equal NULL then
Set PTR= LEFT[PTR].
Else [pop from STACK;]
Set PTR= STACK[TOP] and TOP=TOP+!;
[End of if structure]
[End of step 2 loop]

Step-6 Exit.
Thanks

You might also like