INORDER | TRAVERSING TECHNIQUE
Short Trick
Let we understand INORDER technique with the help of example:
Suppose we have any binary tree such as:
80 , 50 , 100 , 45 , 95 , 110 , 47 , 99 , 115 , 97
Now we find Inorder sequence of following binary tree.
First mark every node at the bottom like
Start traversing from 45 node just connect all the lines at bottom of each node till reach at the end node.
.
Now collect all the node in the sequence of traversing.
The traversing sequence will be:
INORDER: 45 , 47 , 50 , 80 , 95 , 97 , 99 , 100 , 110 , 115
RECURSIVE PROCEDURE FOR INORDER
void Inorder ( struct node *root)
{
if ( root 1=NULL)
{
Inorder ( root ➜lc);
Printf ( "%d", root➜data);
Inorde (root➜rc);
}
}
NONRECURSIVE FUNCTION FOR INORDER
void inorder(structnode *root)
{
structnode *nodestack[50], *temp=root;
int tos=-1;
do
{
while (temp !=NULL)
{
nodestack[++tos] = temp;
temp = temp➜lc;
}
if (tos==-1)
{
break;
}
else
{
temp = nodestack[tos--];
printf("%d",temp➜data);
temp =temp ➜ rc;
}
while(1);
}
No comments:
Post a Comment