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(sddata)//left side branch

{

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!