#include<stdio.h>
#include<malloc.h>
struct tree
{
int data;
struct tree *left;
struct tree *right;
}*temp,*head;
void input(struct tree *root)
{
int a=0,ch;
struct tree *tmp,*p;
printf("Enter the data (enter '-1' to terminate):\n");
scanf("%d",&a);
root->data=a;
root->left=NULL;
root->right=NULL;
while(a!=-1)
{
scanf("%d",&a);
if(a==-1)break;
tmp=malloc(sizeof(struct tree));
tmp->data=a;
tmp->left=NULL;
tmp->right=NULL;
p=root;
while(1)
{
if(a<p->data)
{
if(p->left!=NULL)
p=p->left;
else{
p->left=tmp;
break;
}
}
else
{
if(p->right!=NULL)
p=p->right;
else{
p->right=tmp;
break;
}
}
}
}
}
void inorder(struct tree *tmp)
{
if(tmp->left!=NULL)
inorder(tmp->left);
printf("%d ",tmp->data);
if(tmp->right!=NULL)
inorder(tmp->right);
}
void del(struct tree *tmp)
{
int a=0,ch;
struct tree *p,*parent=NULL;
printf("\nEnter the data to be deleted:\n");
scanf("%d",&a);
while(1)
{
if(tmp==NULL)
{
printf("Value NOT Found.\n");
break;
}
else if(tmp->data==a)
{
if(tmp->left==NULL && tmp->right==NULL)
{
if(parent==NULL)
head=NULL;
else if(parent->left==tmp)
parent->left=NULL;
else
parent->right=NULL;
}
else if(tmp->right==NULL)
{
if(parent==NULL)
head=tmp->left;
else if(parent->left==tmp)
parent->left=tmp->left;
else
parent->right=tmp->left;
}
else
{
p=tmp->right;
parent=tmp;
while(p->left!=NULL)
{
parent=p;
p=p->left;
}
tmp->data=p->data;
if(parent!=tmp)
parent->left=p->right;
else
parent->right=p->right;
}
break;
}
else if(tmp->data>a)
{
parent=tmp;
tmp=tmp->left;
}
else if(tmp->data<=a)
{
parent=tmp;
tmp=tmp->right;
}
}
}
main()
{
head=malloc(sizeof(struct tree));
input(head);
printf("\nInorder:-\n ");
inorder(head);
del(head);
printf("\nAfter deletion\n ");
inorder(head);
}
#include<malloc.h>
struct tree
{
int data;
struct tree *left;
struct tree *right;
}*temp,*head;
void input(struct tree *root)
{
int a=0,ch;
struct tree *tmp,*p;
printf("Enter the data (enter '-1' to terminate):\n");
scanf("%d",&a);
root->data=a;
root->left=NULL;
root->right=NULL;
while(a!=-1)
{
scanf("%d",&a);
if(a==-1)break;
tmp=malloc(sizeof(struct tree));
tmp->data=a;
tmp->left=NULL;
tmp->right=NULL;
p=root;
while(1)
{
if(a<p->data)
{
if(p->left!=NULL)
p=p->left;
else{
p->left=tmp;
break;
}
}
else
{
if(p->right!=NULL)
p=p->right;
else{
p->right=tmp;
break;
}
}
}
}
}
void inorder(struct tree *tmp)
{
if(tmp->left!=NULL)
inorder(tmp->left);
printf("%d ",tmp->data);
if(tmp->right!=NULL)
inorder(tmp->right);
}
void del(struct tree *tmp)
{
int a=0,ch;
struct tree *p,*parent=NULL;
printf("\nEnter the data to be deleted:\n");
scanf("%d",&a);
while(1)
{
if(tmp==NULL)
{
printf("Value NOT Found.\n");
break;
}
else if(tmp->data==a)
{
if(tmp->left==NULL && tmp->right==NULL)
{
if(parent==NULL)
head=NULL;
else if(parent->left==tmp)
parent->left=NULL;
else
parent->right=NULL;
}
else if(tmp->right==NULL)
{
if(parent==NULL)
head=tmp->left;
else if(parent->left==tmp)
parent->left=tmp->left;
else
parent->right=tmp->left;
}
else
{
p=tmp->right;
parent=tmp;
while(p->left!=NULL)
{
parent=p;
p=p->left;
}
tmp->data=p->data;
if(parent!=tmp)
parent->left=p->right;
else
parent->right=p->right;
}
break;
}
else if(tmp->data>a)
{
parent=tmp;
tmp=tmp->left;
}
else if(tmp->data<=a)
{
parent=tmp;
tmp=tmp->right;
}
}
}
main()
{
head=malloc(sizeof(struct tree));
input(head);
printf("\nInorder:-\n ");
inorder(head);
del(head);
printf("\nAfter deletion\n ");
inorder(head);
}
OUTPUT:-
-By K K Mohanta.