I am having trouble coming up with code for removing a node in a binary search tree. This is what I have so far:
void remove(int n)
{
node *current=root;
node *gptr;
while(current != NULL){
if(n<current->key){
gptr=current;
current=current->left;
}else if(n>current->key){
gptr=current;
current=current->right;
}else if(n==current->key){
if(gptr->right->key==n){
cout<<"1st ==key = "<<gptr->key<<endl;
cout<<"1st ==current = "<<current->key<<endl;
delete current;
gptr->right=NULL;
}else if(gptr->left->key==n){
cout<<"1st ==key = "<<gptr->key<<endl;
cout<<"1st ==current = "<<current->key<<endl;
delete current;
gptr->left=NULL;
}
}
}
}
The problem I am having is lets say for example I enter
10 8 12 11 15
If I remove the 15 it works fine. When I go back and try to remove the 11 after I remove the fifteen it gives me a segmentation fault. Any help would be appreciated.