Question
Does anyone know what's wrong with my code & why I can't make it work? It's written in C #include struct tree{ int data; struct
Does anyone know what's wrong with my code & why I can't make it work?
It's written in C
#include
struct tree{
int data;
struct tree * lptr;
struct tree * gptr;
};
void branch(struct tree *,int);
void main(void)
{
int ct;
int somedata;
struct tree * root;
root=malloc(sizeof(struct tree));
root->data=NULL;
root->gptr=NULL;
root->lptr=NULL;
printf("The root's address is %p ",root);
for(ct=1;ct<=10;++ct)
{
printf("Enter an integer: ");
scanf("%d",&somedata);
if(root->data==NULL)
root->data=somedata;
else
branch(root,somedata);
}
return;
}
void branch(struct tree * r,int sd)
{
struct tree * temp;
if(sd==r->data)
{
printf("Duplicate value %d at address %p ",sd,r);
return;
}
if(sd
{
if(r->lptr==NULL)//make a new branch on left
{
temp=malloc(sizeof(struct tree));
temp->data=sd;
temp->lptr=NULL;
temp->gptr=NULL;
r->lptr=temp;
printf("Address %p has an lptr address of %p with data of %d ",r,temp,sd);
return;
}
else //in the case of occupied lptr, start again there.
{
branch(r->lptr,sd);
return;
}
}
if(sd>r->data)//right side branch
{
if(r->gptr==NULL)//make a new branch on right
{
temp=malloc(sizeof(struct tree));
temp->data=sd;
temp->lptr=NULL;
temp->gptr=NULL;
r->gptr=temp;
printf("Address %p has an gptr address of %p with data of %d ",r,temp,sd);
return;
}
else //in the case of occupied lptr, start again there.
{
branch(r->gptr,sd);
return;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started