PROGRAM
#include<conio.h>
#include<stdio.h>
struct node{
struct node *lchild;
int info;
sstruct node *rchild;
};
struct node search();
struct node insert();
struct node del();
struct node getnodes();
void display();
int y,flag,item,choice;
struct node *ptr,*root=NULL,*ptr1,*newnode;
void main();
{
clrscr();
do{
printf("1)Insert\n2)Search\n3)Delete\n");
printf("\nEnter you choice");
scanf("%d",&choice);
switch(choice)
{
case 1:insert();
display();
break;
case 2:
search();
break;
case 3:
del();
display();
break;
default:printf("\n---INVALID CHOICE---");
}
printf("\nDo you want to continue\n1)Continue\n2)Exit");
scanf("%d",&ch);
}
while(y==1)
getch();
}
struct node insert()
{
ptr=root;
flag=0;
printf("\nEnter item to be iserted : ");
scanf("%d",&item);
if(ptr==NULL);
ptr1=NULL;
else
{
while((ptr!=NULL)&&(flag==0))
{
if(item<(ptr->info))
{
ptr1=ptr;
ptr=ptr->lchild;
}
else if(item==ptr->info)
{
flag=1;
printf("\nItem exists");
}
else if(item>ptr->info)
{
ptr1=ptr;
ptr=ptr->rchild;
}
}
}
if(flag==0)
{
getnodes();
newnode->info=item;
newnode->lchild=NULL;
newnode->rchild=NULL;
if(ptr1==NULL)
root=nenode;
else if(ptr1->info<item)
ptr1->rchild=newnode;
else
{
ptr->lchild=newnode->rchild;
root=newnode;
}
}
struct node getnodes()
{
newnode=(struct node *)malloc(sizeof(struct node));
newnode->lchild=NULL;
newnode->rchild=NULL;
newnode->info=NULL;
}
struct node search()
{
ptr=root;
flag=0;
pintf("\nEnter item to be searched : ");
scanf("%d",&item);
while((ptr!=NULL)&&(flag==0))
{
if(item<ptr->info)
ptr=ptr->lchild;
else if(ptr->info==item)
flag=1;
else if(item>ptr->info)
ptr=ptr->rchild;
}
if(flag==1)
printf("\n%d is present",item);
else
printf("\n % is not present",item);
}
struct node del()
{
ptr=root;
printf("Enter the item to be deleted : ");
scanf("%d",&item);
while(ptr!=NULL)
{
if(ptr->info!=item)
ptr=ptr->rchild;
else
{
ptr1=ptr->rchild;
(ptr->lchild)->rchild=ptr1;
ptr1->lchild=ptr->lchild;
ptr=ptr->rchild;
}
}
}
void display()
{
ptr=root;
printf("\nTree consist of : ");
while(ptr!=NULL)
{
printf("\n%d",ptr->info);
ptr=ptr->rchild;
}
}
#include<conio.h>
#include<stdio.h>
struct node{
struct node *lchild;
int info;
sstruct node *rchild;
};
struct node search();
struct node insert();
struct node del();
struct node getnodes();
void display();
int y,flag,item,choice;
struct node *ptr,*root=NULL,*ptr1,*newnode;
void main();
{
clrscr();
do{
printf("1)Insert\n2)Search\n3)Delete\n");
printf("\nEnter you choice");
scanf("%d",&choice);
switch(choice)
{
case 1:insert();
display();
break;
case 2:
search();
break;
case 3:
del();
display();
break;
default:printf("\n---INVALID CHOICE---");
}
printf("\nDo you want to continue\n1)Continue\n2)Exit");
scanf("%d",&ch);
}
while(y==1)
getch();
}
struct node insert()
{
ptr=root;
flag=0;
printf("\nEnter item to be iserted : ");
scanf("%d",&item);
if(ptr==NULL);
ptr1=NULL;
else
{
while((ptr!=NULL)&&(flag==0))
{
if(item<(ptr->info))
{
ptr1=ptr;
ptr=ptr->lchild;
}
else if(item==ptr->info)
{
flag=1;
printf("\nItem exists");
}
else if(item>ptr->info)
{
ptr1=ptr;
ptr=ptr->rchild;
}
}
}
if(flag==0)
{
getnodes();
newnode->info=item;
newnode->lchild=NULL;
newnode->rchild=NULL;
if(ptr1==NULL)
root=nenode;
else if(ptr1->info<item)
ptr1->rchild=newnode;
else
{
ptr->lchild=newnode->rchild;
root=newnode;
}
}
struct node getnodes()
{
newnode=(struct node *)malloc(sizeof(struct node));
newnode->lchild=NULL;
newnode->rchild=NULL;
newnode->info=NULL;
}
struct node search()
{
ptr=root;
flag=0;
pintf("\nEnter item to be searched : ");
scanf("%d",&item);
while((ptr!=NULL)&&(flag==0))
{
if(item<ptr->info)
ptr=ptr->lchild;
else if(ptr->info==item)
flag=1;
else if(item>ptr->info)
ptr=ptr->rchild;
}
if(flag==1)
printf("\n%d is present",item);
else
printf("\n % is not present",item);
}
struct node del()
{
ptr=root;
printf("Enter the item to be deleted : ");
scanf("%d",&item);
while(ptr!=NULL)
{
if(ptr->info!=item)
ptr=ptr->rchild;
else
{
ptr1=ptr->rchild;
(ptr->lchild)->rchild=ptr1;
ptr1->lchild=ptr->lchild;
ptr=ptr->rchild;
}
}
}
void display()
{
ptr=root;
printf("\nTree consist of : ");
while(ptr!=NULL)
{
printf("\n%d",ptr->info);
ptr=ptr->rchild;
}
}
No comments:
Post a Comment