program runs, but freezes after I input the number I want to insert - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

program runs, but freezes after I input the number I want to insert

lady126lady126 Posts: 2Member
can anyone tell me what the problem? thank you...maybe there is something wrong with the insert or the menu..

typedef struct cell
{ int elem;
struct cell* lc;
struct cell* rc;
}*BST,bsize;

void insert(BST*,int);
int member(BST,int);
void display(BST);
int min(BST);
int max(BST);


int main()
{
BST T;
int choice=0;
char xx;
int x;

clrscr();


do
{

puts(" 1.INSERT
2.DELETE
3.DISPLAY
4.MEMBER
5.FIND MINIMUM
6.FIND MAXIMUM
7.MAKENULL
8.EXIT

Enter your choice:");

scanf("%d",&choice);

switch(choice)
{
case 1:
puts("Enter the element you want to insert.");
scanf(" %d",&x);
insert(&T,x);
break;

case 3:
display(T);
break;
case 4:
puts("Enter the element to find.");
scanf("%d",&x);

(member(T,x)==1)?printf("Element %d is found in the bst",x):printf("Element %d is not found in the bst",x);
break;
case 5:
printf("The minimum value is %d",min(T));
break;

case 6:
printf("The maximum value is %d",max(T));
break;


case 8:
puts("Thank You For Using The Program XD");


}

}while(choice != 8);

return 0;
getch();

}


void insert(BST *T,int x)
{
BST *pT,n;
int mem;
mem=member(*T,x);
pT=T;

if(*T == NULL)
{
*T = (BST)calloc(1,sizeof(bsize));
(*T)->elem=x;
}
else
{
if( mem == 0)
{ n=(BST)calloc(1,sizeof(bsize));
n->elem=x;
while(*pT != NULL)
{
pT=(x < (*pT)->elem)?&(*pT)->lc:&(*pT)->rc;
}
*pT=n;
}
}
}


int member(BST T,int x)
{
int flag=0;
while(T != NULL && flag == 0)
{
if(x == T->elem)
flag=1;
else
T=(x < T->elem)?T->lc:T=T->rc;
}
return flag;
}


void display(BST T)
{
if (T== NULL)
printf("Empty BST");
display(T->lc);
printf("%c ", T->elem);
display(T->rc);
}

int min(BST T)
{
if(T == NULL)
puts("Empty Tree");
else
{
while(T->lc != NULL)
{ T=T->lc; }
}

return T->elem;
}

int max(BST T)
{
if(T == NULL)
puts("Empty Tree");
else
{
while(T->rc != NULL)
{ T=T->rc; }
}

return T->elem;
}
Sign In or Register to comment.