Monday, March 23, 2020

3_Tree Traversals Lab

/******************************************************************************
Trees Concepts !   Inoder Traversal              
@Mukesh Mann dated 23-03-2020
*******************************************************************************/
/******************************************************************************
*******************************************************************************/
#include <stdio.h>

 typedef struct node

            {

                int data;

                struct node *left;

                struct node *right;

             

            }Node;



// Lets try to creat a root node first

int * create()

{

 

 int x;

Node *newnode=(Node*)malloc(sizeof(Node));

 printf("Enter the data for the nodes(-1 for no data)");



 scanf("%d", &x);



 if(x==-1)

 {

     //printf("You have not entered any data");

     return NULL;

      }

      newnode->data=x;

      printf("Enter the left child data rooted at %d \n",newnode->data);

      newnode->left=create();

      printf("Enter the right child data rooted at %d\n",newnode->data);

      newnode->right=create();

      return newnode;

 

}

void traverse(Node *t)

{

 

    if(!t==NULL)

    {

   

     printf("Left child data is %d ",t->data);

      //printf("Left child data is %d",t->left->data);

      //printf("right child data is %d",t->left->data);

       traverse(t->left);

       t=t->right;

      }

   

   if(!t==NULL)

      {

       

       printf("Right child data is %d\n",t->data);

      //printf("Left child data is %d",t->left->data);

      //printf("right child data is %d",t->left->data);

       traverse(t->right);

       //t=root;

       

      }

}

void Inorder(Node *t)

{

    //printf("In order traversal is" );

    if(t)

    {

    Inorder(t->left);

    printf(" %d",t->data);

    Inorder(t->right);

     

    }

}



int main()

{

Node *root=create();

Node *t=root;

printf("Root child is %d\n ",t->data);

traverse(t);

Inorder(t);

printf(" is the order traversal \n\n" );


    return 0;

}

No comments:

Post a Comment

9_Regular Expressions

Regular expressions- Sometimes in HTML, the developer defines more than one class name ( that’s class input has more than one name Here ...