A person want to store data of
cricketers and the names are in a text file in the following format .name,
country number of runs scored.
Use a binary tree to store the names and
print the names in order of scores. Allow the user to delete a name from the
binary tree.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 20
struct node
{
char name[MAX];
int runs;
struct node *lchild,*rchild;
}*start;
void inorder(struct node *t)
{
if(t!=NULL)
{
inorder(t->lchild);
printf("%d\t%s\t%d\t%d\t%d\n",t,t->name,t->runs,t->lchild,t->rchild);
inorder(t->rchild);
}
}
int create(char na[MAX],int run)
{
int i,st=0;
struct node *tmp,*p,*q;
tmp=(struct node *)malloc(sizeof(struct node));
strcpy(tmp->name,na);
tmp->runs=run;
tmp->lchild=tmp->rchild=NULL;
if(start==NULL)
{
start=tmp;
return 1;
}
q=start;
while(q!=NULL)
{
if(run<q->runs)
{
p=q;
q=q->lchild;
st=1;
}
else if(run>q->runs)
{
p=q;
q=q->rchild;
st=2;
}
else
{
printf("DUPLICATE VALUE");
return 0;
}
}
if(st==1)
p->lchild=tmp;
else if(st==2)
p->rchild=tmp;
return 0;
}
void delb(struct node *q,struct node *p)
{
struct node *s;
s=q->rchild;
if(s->lchild!=NULL)
s=s->lchild;
del(s->runs);
s->lchild=q->lchild;
s->rchild=q->rchild;
if(q==start)
start=s;
else if(p->lchild==q)
p->lchild=s;
else if(p->rchild==q)
p->rchild=s;
}
int del(int run)
{
struct node *q,*p;
int s;
q=start;
while(q!=NULL)
{
if(run<q->runs)
{
s=1;
p=q;
q=q->lchild;
}
else if(run>q->runs)
{
s=2;
p=q;
q=q->rchild;
}
else if(run==q->runs)
{
if(q->lchild==NULL&&q->rchild==NULL)
{
if(s==1)
p->lchild=NULL;
else if(s==2)
p->rchild=NULL;
else
start=NULL;
}
else if(q->lchild!=NULL&&q->rchild==NULL)
{
if(s==1)
p->lchild=q->lchild;
else if(s==2)
p->rchild=q->lchild;
else
start=q->lchild;
}
else if(q->rchild!=NULL&&q->lchild==NULL)
{
if(s==1)
p->lchild=q->rchild;
else if(s==2)
p->rchild=q->rchild;
else
start=q->rchild;
}
else if(q->lchild!=NULL&&q->rchild!=NULL)
delb(q,p);
//free(q);
return 1;
}
}
printf("NOT FOUND");
return 0;
}
void main()
{
FILE *f;
int i,n=0,j,x=0,run,op;
char c,name[MAX][MAX],code[MAX][MAX],runs[MAX][MAX];
start=NULL;
clrscr();
f=fopen("C:/cricket.txt","r");
if(f==NULL)
{
printf("Invalid directory");
}
//---------------------------------------------------------------------------
for(i=0;i<MAX;i++)
for(j=0;j<MAX;j++)
{
name[i][j]='\0';
code[i][j]='\0';
runs[i][j]='\0';
}
i=0;x=0;n=0;
while((c=fgetc(f))!=EOF)
{
if(c==' ')
{
i=-1;
x++;
}
if(c=='\n')
{
i=-1;
x=0;
n++;
}
if(x==0)
name[n][i]+=c;
if(x==1)
code[n][i]+=c;
if(x==2)
runs[n][i]+=c;
i++;
}
//--------------------------------------------------------------------------------
for(i=0;i<=n;i++)
{
printf("Name: %s Code: %s Runs: %s\n",name[i],code[i],runs[i]);
run=atoi(runs[i]);
create(name[i],run);
}
printf("Root :- %s",start->name);
printf("\nAddress\tName\tRuns\tLeft\tRight\n.................................\n");
inorder(start);
while(1)
{
printf("\n\n1.INSERT ITEM\n2.DELETE ITEM\n3.EXIT\n");
scanf("%d",&op);
switch(op)
{
case 1:
i++;
printf("NAME: ");
scanf("%s",name[i]);
printf("RUNS: ");
scanf("%d",&run);
create(name[i],run);
break;
case 2:
if(start==NULL)
{
printf("NO ITEM TO DELETE");
break;
}
i++;
printf("RUNS :");
scanf("%d",&run);
del(run);
break;
case 3:
exit(0);
}
printf("\n\nRoot :- %s",start->name);
printf("\nAddress\tName\tRuns\tLeft\tRight\n.................................\n");
inorder(start);
printf("\n");
}
}
File : C:/cricket.txt
SACHIN 10 250
SAVENG 11 150
YOUVI 13 175
RAHUL 12 100
GAUTHUM 90 50
DHONI 23 400
KOHLI 5 200