The goal of this assignment it to work with binary search tr
The goal of this assignment it to work with binary search trees, you must implement what is stated at the top of the code
Link
http://pastebin.com/jphLEJ5p
Solution
bool DeleteElementFromBSTree (int nTargetElement);
{
int found ;
struct btreenode *parent, *z, *zuccessor ;
/* we will first see if the tree is empty */
if ( *nTargetElement == NULL )
{
printf ( \"\ The given tree is empty\" ) ;
return ;
}
parent = z = NULL ;
/* This is a call to search function to find the node to be deleted */
search ( nTargetElement, num, &parent, &z, &found ) ;
/* if the node to deleted is not found */
if ( found == FALSE )
{
printf ( \"\ The data to be deleted is not found\" ) ;
return ;
}
/* now we will check if the node to be deleted has two children */
if ( z -> leftchild != NULL && z -> rightchild != NULL )
{
parent = z ;
zuccessor = z -> rightchild ;
while ( zuccessor -> leftchild != NULL )
{
parent = zuccessor ;
zuccessor = zuccessor -> leftchild ;
}
z -> data = zuccessor -> data ;
z = zuccessor ;
}
/* now we will check if the node to be deleted has no child */
if ( z -> leftchild == NULL && z -> rightchild == NULL )
{
if ( parent -> rightchild == z )
parent -> rightchild = NULL ;
else
parent -> leftchild = NULL ;
free ( z ) ;
return ;
}
/* On the other hand ,if the node to be deleted has only rightchild */
if ( z -> leftchild == NULL && z -> rightchild != NULL )
{
if ( parent -> leftchild == z )
parent -> leftchild = z -> rightchild ;
else
parent -> rightchild = z -> rightchild ;
free ( z ) ;
return ;
}
/*The last step is to check if the node to be deleted has only left child */
if ( z -> leftchild != NULL && z -> rightchild == NULL )
{
if ( parent -> leftchild == z )
parent -> leftchild = z -> leftchild ;
else
parent -> rightchild = z -> leftchild ;
free ( z ) ;
return ;
}
}

