Hello
I have write program in c++ which delete node from BST. It work for some level.Mean it wrok well when some nodes are delte but when i delete root node program print garbage value infinite time... Here is my code
Code:
void del(int num)
{
struct list *current,*pre,*target;
current=root;
if(current==NULL)
return;
while((current->left !=NULL) && (current->right!=NULL))
{
if(current->data==num)
target=current;
if(num < current->data)
{
if(current->left ==NULL)
break;
pre=current;
current=current->left;
}
else
{
if(current->right==NULL)
break;
pre=current;
current=current->right;
}
}
if(target == NULL)
return;
else
{
if(pre==NULL)
{
free(current);
root=NULL;
}
else
{
target->data=current->data;
if(pre->left==current)
pre->left=current->right;
else
pre->right=current->left;
free (current);
}
}
}
anyone help me! and also tell how can i find leaf node in BST....any idea or algorithm?