How to Create a Tree In C Language With source code and explanation

#include<stdio.h>
#include<conio.h>
struct node *creat_tree(int value,struct node*);
int search_tree(int value,struct node*);
int update_tree(int value,struct node*);
struct node *delete_tree(int value,struct node*);
void inorder(struct node*);
void preorder(struct node*);
void postorder(struct node*);
int c=0,a;

struct node
{
    int info;
    struct node *lptr;
    struct node *rptr;
}*head;
void main()
{
   int ch,value;
   head=NULL;
   do
   {
     clrscr();
     printf("\n1:Insert Value");
     printf("\n2:Preorder Display");
     printf("\n3:Inorder Display");
     printf("\n4:Postorder Display");
     printf("\n5:Search Value");
     printf("\n6:Update Value");
     printf("\n7:Delete Value");
     printf("\n8:Exit");
     printf("\nEnter The Above Choice:->");
     scanf("%d",&ch);
     switch(ch)
     {
       case 1:
          printf("\nEnter The Value:->");
          scanf("%d",&value);
          head=creat_tree(value,head);
          break;
      case 2:
         preorder(head);
         break;
      case 3:
         inorder(head);
         break;
      case 4:
         postorder(head);
         break;
      case 5:
         printf("\nEnter Value for Search:->");
         scanf("%d",&value);
         c=search_tree(value,head);
         if(c==0)
         {
        printf("\nValue not Found");
        getch();
         }
         c=0;
         break;
      case 6:
        printf("\nWhich Element You Update:->");
        scanf("%d",&value);
        c=update_tree(value,head);
        if(c==0)
        {
           printf("\nValue is Not Found");
           getch();
        }
        c=0;
        break;
      case 7:
           printf("\nWhich Element You Delete:->");
           scanf("%d",&value);
           head=delete_tree(value,head);
           break;
     }
   }
   while(ch!=8);
}
struct node *creat_tree(int value,struct node *node1)
{
   if(node1==NULL)
   {
      node1=(struct node*)malloc(sizeof(struct node));
      node1->info=value;
      node1->lptr=NULL;
      node1->rptr=NULL;
   }
   else
   {
     if(node1->info==value)
     {
       printf("\nDuplicate Value D not Allow");
       getch();

     return node1;
     }

 if(node1->info>=value)
 {
   node1->lptr=creat_tree(value,node1->lptr);
 }
 else
 {
   node1->rptr=creat_tree(value,node1->rptr);
 }
}
return node1;
//}
}

void preorder(struct node *node1)
{
  if(node1!=NULL)
  {
    printf("%d,",node1->info);
    getch();
    preorder(node1->lptr);
    preorder(node1->rptr);
  }
}
void inorder(struct node *node1)
{
  if(node1!=NULL)
  {
    inorder(node1->lptr);
    printf("%d,",node1->info);
    getch();
    inorder(node1->rptr);
  }
}

void postorder(struct node *node1)
{
   if(node1!=NULL)
   {
     postorder(node1->lptr);
     postorder(node1->rptr);
     printf("%d,",node1->info);
     getch();

}
}
int search_tree(int value,struct node *node1)
{
  if(node1->info==value)
  {
     printf("\nValue Found");
     c=1;
     getch();
  }
  else
  {
    if(node1->info>=value)
    {
      search_tree(value,node1->lptr);
    }
    else
    {
      search_tree(value,node1->rptr);
    }
    }
    return c;
}

int update_tree(int value,struct node *node1)
{
  if(node1->info==value)
  {
     printf("\nEnter Element For Update:->");
     scanf("%d",&a);
     node1->info=a;
     printf("\nUpdate Success.....");
     c=1;
     getch();
  }
  else
  {
   if(node1->info>=value)
   {
     update_tree(value,node1->lptr);
   }
   else
   {
     update_tree(value,node1->rptr);
   }
 }
 return c;
}

struct node *delete_tree(int value,struct node *node1)
{
   if(node1->info==value)
   {
     if(node1->lptr==NULL && node1->rptr==NULL)
     {
    printf("\nThe Deleted Value is[%d]",node1->info);
    free(node1);
    node1=NULL;
     }
     else
     {
    printf("\nValue Can not Be deleted");
     }
     getch();
   }
   else
   {
     if(node1->info>=value)
     {
       node1->lptr=delete_tree(value,node1->lptr);
     }
     else
     {
      node1->rptr=delete_tree(value,node1->rptr);
     }
     }
     return node1;
}
How to Create a Tree In C Language With source code and explanation How to Create a Tree In C Language With source code and explanation Reviewed by Unknown on 5:52:00 PM Rating: 5

No comments:

Powered by Blogger.