Question:
BST c program problem?
?
2014-01-19 11:04:56 UTC
the compiler is showing no error in this program for insertion,deletion and display operation of BST but it shows processor fault when i run this program( right after i enter a second data).can you tell me where is the mistake?

#include
#include
#include

typedef struct node{
int info;
struct node *right,*left;
}*nodeptr;

void display1(nodeptr Root);
void display2(nodeptr Root);
void display3(nodeptr Root);
void insert(nodeptr root,int x);

void del(nodeptr root,int x); */*for deletion*/*

nodeptr find(nodeptr root,int x);
int subdel(nodeptr root);
void display(nodeptr root);


void main(){
int c,x;
nodeptr root=NULL;
do{
printf("1.insert,2.delete,3.display,4.exit");
scanf("%d",&c);
switch(c){
case 1:
scanf("%d",&x);
if(root==NULL){
root=(nodeptr)malloc(sizeof(struct node));
root->info=x;
root->left=NULL;
root->left=NULL;}
else{
insert(root,x);} break;
case 2:
scanf("%d",&x);
if(root==NULL){printf("empty");}
else{
del(root,x);} break;
case 3:
if(root==NULL){printf("empty");}
else{
display(root);} break;
case 4:
exit(1); break;
default:
printf("wrong"); break;
}}while(c!=4);}





void del(nodeptr root,int x){
int k=0;
root= find(root,x);
if(root==NULL){ return;}

k=subdel(root);
if(k==0){
nodeptr temp=root;
temp=temp->right;
while(temp->left!=NULL){
temp=temp->left;}
root->info=temp->info;
subdel(temp);}}

nodeptr find(nodeptr root,int x){
if(root==NULL){printf("element not found"); return(NULL);}
if(root->info==x){
return(root);}
if(xinfo){
find(root->left,x);}
if(x>root->info){
find(root->right,x);}
}

int subdel(nodeptr root){ int k=0;
if((root->right==NULL)&&(root->left==NULL)){
free(root);
root=NULL; k=1; return(k);}
if((root->right==NULL)||(root->left==NULL)){
if(root->right==NULL){root->info=root->left->info;
free(root->left);
root->left==NULL;}
else{ root->info=root->right->info;
free(root->right);
root->right==NULL;} k=1; return(k);}
return(k);}

void display(nodeptr Root){
printf("preorder");
display1(Root);
printf("\n postorder");
display2(Root);
printf("\n inorder");
display3(Root);}


void display1(nodeptr Root){
nodeptr root=Root;
if(root!=NULL){
printf("%6d",root->info);
display1(root->left);
display1(root->right);
}}



void display2(nodeptr Root){
nodeptr root=Root;
if(root!=NULL){
display2(root->left);
display2(root->right);
printf("%6d",root->info);}}


void display3(nodeptr Root){
nodeptr root=Root;
if(root!=NULL){
display3(root->left);
printf("%6d",root->info);
display3(root->right);
printf("%6d",root->info); }
}


void insert(nodeptr root,int x){
nodeptr temp;
if(xinfo){
if(root->left==NULL){
temp=(nodeptr)malloc(sizeof(struct node));
temp->info=x; temp->left=NULL; temp->right=NULL; root->left=temp; return;}
else{ insert(root->left,x);} }
if(x>root->info){
if(root->right==NULL){
temp=(nodeptr)malloc(sizeof(struct node));
temp->info=x; temp->left=NULL; temp->right=NULL; root->right=temp; return;}
else{ insert(root->right,x);} }

}
Three answers:
Laurence I
2014-01-19 11:21:08 UTC
This

if(root==NULL){

root=(nodeptr)malloc(sizeof(struct node));

root->info=x;

root->left=NULL;

root->left=NULL;}



looks wrong

the last two lines appear to set LEFT.
husoski
2014-01-19 20:27:50 UTC
I don't know what SadSongs is complaining about. Every malloc() call is correct.



I don't have a problem with named pointer types, either, but that's a matter of style.



As for the insert problem, it's hard to follow the logic when the code gets truncated. That's happening because of the 40-character rule that (along with removing all leading and "extra" whitespace) makes posting code difficult.



After fixing up and indenting in Code::Blocks, I don't see a problem in insert() at all. I do complaints about main not returning an int (required in Standard C), find() not returning a value in all cases (three if statements that should be if/else if/else instead) and code has no effect in subdel() (possible missing else to handle deleting internal node with both child links non-NULL.)



Try using a debugger, or add to your includes and add statements like:



assert(root != NULL); /* root should not be NULL here */



...to halt as close to the error as possible. Diagnostic printf() or fprintf(stderr,) statments can also be used to provide a trace of your program if it's not convenient to use a debugger.
?
2014-01-19 19:18:25 UTC
*nodeptr



Remove the *, declare pointers inside executable code when you need them.



Your malloc is allocating enough memory for a pointer, not for the structure itself.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...