Tree Programming in C Language With Source Code
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node* create_tree(int info,struct node*);
int search_tree(int info,struct node*);
int update_tree(int info,struct node*);
struct node* delete_tree(int info,struct node*);
void preorder(struct node*);
void inorder(struct node*);
void postorder(struct node*);
int temp=0;
struct node
{
struct node *lptr;
int info;
struct node *rptr;
}*head;
void main()
{
int temp,info;
head=NULL;
do
{
clrscr();
printf("\nEnter 1 for INSERT");
printf("\nEnter 2 for SEARCH");
printf("\nEnter 3 for UPDATE");
printf("\nEnter 4 for DELETE");
printf("\nEnter 5 for Preorder Display");
printf("\nEnter 6 for Inorder Display");
printf("\nEnter 7 for Postorder Display");
printf("\nEnter 8 for Exit");
printf("\nEnter Your Choice:");
scanf("%d",&temp);
switch(temp)
{
case 1: printf("\nEnter the value:");
scanf("%d",&info);
head=create_tree(info,head);
break;
case 2: printf("\nEnter the Element for Search:");
scanf("%d",&info);
temp=search_tree(info,head);
if(temp==0)
{
printf("Value not found!!");
getch();
}
temp=0;
break;
case 3: printf("\nEnter the Element for Update:");
scanf("%d",&info);
temp=update_tree(info,head);
if(temp==0)
{
printf("Value not found!!");
getch();
}
temp=0;
break;
case 4: printf("\nEnter the Element for Delete:");
scanf("%d",&info);
head=delete_tree(info,head);
break;
case 5: preorder(head);
break;
case 6: inorder(head);
break;
case 7: postorder(head);
break;
}
}while(temp!=8);
}
struct node* create_tree(int info,struct node *node1)
{
if(node1==NULL)
{
node1=(struct node*)malloc(sizeof(struct node));
node1->info=info;
node1->lptr=NULL;
node1->rptr=NULL;
}
else
{
if(node1->info==info)
{
printf("Duplicate value cannot insert...");
getch();
return node1;
}
if(node1->info>=info)
node1->lptr=create_tree(info,node1->lptr);
else
node1->rptr=create_tree(info,node1->rptr);
}
return node1;
}
int search_tree(int info,struct node *node1)
{
if(node1!=NULL)
{
if(node1->info==info)
{
printf("The value found");
temp=1;
getch();
}
else
{
if(node1->info>=info)
search_tree(info,node1->lptr);
else
search_tree(info,node1->rptr);
}
}
return temp;
}
int update_tree(int info,struct node* node1)
{
if(node1!=NULL)
{
if(node1->info==info)
{
printf("\nEnter the value for update:");
scanf("%d",&node1->info);
printf("\nValue updated successfully...");
getch();
temp=1;
}
else
{
if(node1->info>=info)
update_tree(info,node1->lptr);
else
update_tree(info,node1->rptr);
}
}
return temp;
}
struct node * delete_tree(int info,struct node* node1)
{
if(node1->info==info)
{
if(node1->lptr==NULL && node1->rptr==NULL)
{
printf("the deleted value is:%d",node1->info);
free(node1);
node1=NULL;
}
else
{
printf("Node cannot be deleted...");
}
getch();
}
else
{
if(node1->info>=info)
node1->lptr=delete_tree(info,node1->lptr);
else
node1->rptr=delete_tree(info,node1->rptr);
}
return node1;
}
void preorder(struct node* node1)
{
if(node1!=NULL)
{
printf("\t%d",node1->info);
getch();
preorder(node1->lptr);
preorder(node1->rptr);
}
}
void inorder(struct node* node1)
{
if(node1!=NULL)
{
inorder(node1->lptr);
printf("\t%d",node1->info);
getch();
inorder(node1->rptr);
}
}
void postorder(struct node* node1)
{
if(node1!=NULL)
{
postorder(node1->lptr);
postorder(node1->rptr);
printf("\t%d",node1->info);
getch();
}
}
#include<conio.h>
#include<alloc.h>
struct node* create_tree(int info,struct node*);
int search_tree(int info,struct node*);
int update_tree(int info,struct node*);
struct node* delete_tree(int info,struct node*);
void preorder(struct node*);
void inorder(struct node*);
void postorder(struct node*);
int temp=0;
struct node
{
struct node *lptr;
int info;
struct node *rptr;
}*head;
void main()
{
int temp,info;
head=NULL;
do
{
clrscr();
printf("\nEnter 1 for INSERT");
printf("\nEnter 2 for SEARCH");
printf("\nEnter 3 for UPDATE");
printf("\nEnter 4 for DELETE");
printf("\nEnter 5 for Preorder Display");
printf("\nEnter 6 for Inorder Display");
printf("\nEnter 7 for Postorder Display");
printf("\nEnter 8 for Exit");
printf("\nEnter Your Choice:");
scanf("%d",&temp);
switch(temp)
{
case 1: printf("\nEnter the value:");
scanf("%d",&info);
head=create_tree(info,head);
break;
case 2: printf("\nEnter the Element for Search:");
scanf("%d",&info);
temp=search_tree(info,head);
if(temp==0)
{
printf("Value not found!!");
getch();
}
temp=0;
break;
case 3: printf("\nEnter the Element for Update:");
scanf("%d",&info);
temp=update_tree(info,head);
if(temp==0)
{
printf("Value not found!!");
getch();
}
temp=0;
break;
case 4: printf("\nEnter the Element for Delete:");
scanf("%d",&info);
head=delete_tree(info,head);
break;
case 5: preorder(head);
break;
case 6: inorder(head);
break;
case 7: postorder(head);
break;
}
}while(temp!=8);
}
struct node* create_tree(int info,struct node *node1)
{
if(node1==NULL)
{
node1=(struct node*)malloc(sizeof(struct node));
node1->info=info;
node1->lptr=NULL;
node1->rptr=NULL;
}
else
{
if(node1->info==info)
{
printf("Duplicate value cannot insert...");
getch();
return node1;
}
if(node1->info>=info)
node1->lptr=create_tree(info,node1->lptr);
else
node1->rptr=create_tree(info,node1->rptr);
}
return node1;
}
int search_tree(int info,struct node *node1)
{
if(node1!=NULL)
{
if(node1->info==info)
{
printf("The value found");
temp=1;
getch();
}
else
{
if(node1->info>=info)
search_tree(info,node1->lptr);
else
search_tree(info,node1->rptr);
}
}
return temp;
}
int update_tree(int info,struct node* node1)
{
if(node1!=NULL)
{
if(node1->info==info)
{
printf("\nEnter the value for update:");
scanf("%d",&node1->info);
printf("\nValue updated successfully...");
getch();
temp=1;
}
else
{
if(node1->info>=info)
update_tree(info,node1->lptr);
else
update_tree(info,node1->rptr);
}
}
return temp;
}
struct node * delete_tree(int info,struct node* node1)
{
if(node1->info==info)
{
if(node1->lptr==NULL && node1->rptr==NULL)
{
printf("the deleted value is:%d",node1->info);
free(node1);
node1=NULL;
}
else
{
printf("Node cannot be deleted...");
}
getch();
}
else
{
if(node1->info>=info)
node1->lptr=delete_tree(info,node1->lptr);
else
node1->rptr=delete_tree(info,node1->rptr);
}
return node1;
}
void preorder(struct node* node1)
{
if(node1!=NULL)
{
printf("\t%d",node1->info);
getch();
preorder(node1->lptr);
preorder(node1->rptr);
}
}
void inorder(struct node* node1)
{
if(node1!=NULL)
{
inorder(node1->lptr);
printf("\t%d",node1->info);
getch();
inorder(node1->rptr);
}
}
void postorder(struct node* node1)
{
if(node1!=NULL)
{
postorder(node1->lptr);
postorder(node1->rptr);
printf("\t%d",node1->info);
getch();
}
}
Tree Programming in C Language With Source Code
Reviewed by Unknown
on
5:51:00 PM
Rating:
No comments: