I've got a function here that works perfect for deleting nodes that match a giving string.
My issue is I can't seem to logically find out where to put a statement nor signify that nothing was found... I need some way of letting an outside function know if something was found or not. as I need to do different things if nothing is found.
Thanks
Code:
struct Chapter *deletion(struct Chapter* p, char word [])
{
// Empty Tree case
if ( p == NULL)
return p;
// p->word BEFORE word
else if ( strcmp(p->word,word) > 0 )
p->left = deletion(p->left, word);
else if ( strcmp(p->word,word) < 0 )
p->right = deletion(p->right, word);
// found the node to be deleted
else if (p->left != NULL && p->right != NULL)
{
//if: more than one of that word exists, don't delete, just decrese count
if(p->count > 0)
p->count--;
// else: if only one exists, delete it
else{
// the node has 2 children
// replace the node with word that is lowest in alphabet in right subtree
p = smallest(p->right);
// now delete old copy of lowest element
p->right = deletion(p->right,p->word);
}
}
// node has ONE child OR both are NULL
else
{
//if: more than one of that word exists, don't delete, just decrese count
if(p->count > 0)
p->count--;
// else: if only one exists, delete it
else{
if(p->left != NULL)
p = p->left;
else
p = p->right;
}
}
return p;
}