PREORDER | TRAVERSAL TECHNIQUE

                 PREORDER  | TRAVERSING TECHNIQUE

A Short Trick

Let we understand the PREORDER TRAVERSING TECHNIQUE from the following binary tree.

Suppose we have any binary tree such as:

80 , 50 , 100 , 45 , 95 , 110 , 47 , 99 , 115 , 97




Now we find  Preorder sequence of following binary tree.

First mark every node at the right side of node like




Now we will start traversing from node 80 like




Now here preorder sequence will be:

PREORDER: 80 , 50 , 45 , 47 , 100 , 95 , 99 , 97 , 110 , 115


RECURSIVE PROCEDURE FOR PREORDER


void Preorder( struct node  *root)
{
    if ( root 1=NULL)
    {
        Printf ( "%d", root➜data);
        Preorder ( root ➜lc);
        Preorder (root➜rc);
    }
}


NON RECURSIVE FUNCTION FOR PREORDER


void Preorder(structnode *root)
{
    structnode *nodestack[50], *temp=root;
    int tos= 0 ;
    node stack[Tos] = NULL;
    temp=root;
    while (temp 1 = NULL)
    {
        printf(" %d ", temp➜data);
        if (temp ➜ rc != NULL)
        {
            node stack[++Tos] = temp ➜ rc;
        }
        if (temp ➜ lc != NULL )
        {
            temp = temp ➜ lc;
        }
        else
        {
            temp = node stack [Tos--];
    }
    
}
    

No comments:

Post a Comment