Delete node from BST - iterative
Apr 16, 2012 at 9:26am UTC
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
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?
Topic archived. No new replies allowed.