Ok then, thanks for all that insight on pointers.
Now I'm having problems with the real issue on this topic... I've already did my iterative version of the delete function but I don't like some things about it, maybe they can be improved (or not) but I can't see how. I've also tried to code the case it was missing, deleting a node with 2 childs, but it's not working as it should...
I've commented the whole code where I think the code can be improved and where's the problem. I've also named those problems as A, B, C and D so we can reference to them easily.
Code:
bool deleteClientFromTree(ClientTree *cTree, char *cName) {
if(!cTree) {
return FALSE;
}
ClientTree currPtr = *cTree;
ClientTree prevPtr = NULL;
int nCompare;
while(1) {
nCompare = strcmp(currPtr->clientName, cName);
if(nCompare > 0) {
prevPtr = currPtr;
currPtr = currPtr->leftTree;
} else if(nCompare < 0) {
prevPtr = currPtr;
currPtr = currPtr->rightTree;
} else {
/*
* A)
*
* The following 5 cases have 3 lines in common, the free()
* calls and return statement. Is there anyway to improve
* this code and make it more compact?
*
* Of course, the printf's are to be removed...
*/
if(!prevPtr) {
printf("CASE #1: DELETE ROOT NODE\n");
*cTree = NULL;
free(currPtr->clientProfile);
free(currPtr);
return TRUE;
} else if(!currPtr->leftTree && !currPtr->rightTree) {
printf("CASE #2: DELETE LEAF (NO CHILDREN)\n");
/*
* B)
*
* Any better way to merge this 2 statements into one?
*
* I only need to set one of them as null depending
* on which node I'm deleting, I could place an if
* to check which one, but it would be almost the same
* as what I'm doing now.
*
* Maybe is there a better way to do this?
*/
prevPtr->leftTree = NULL;
prevPtr->rightTree = NULL;
free(currPtr->clientProfile);
free(currPtr);
return TRUE;
} else if(!currPtr->leftTree) {
printf("CASE #3: DELETE NODE WITH RIGHT CHILD\n");
prevPtr->leftTree = currPtr->rightTree;
free(currPtr->clientProfile);
free(currPtr);
return TRUE;
} else if(!currPtr->rightTree) {
printf("CASE #4: DELETE NODE WITH LEFT CHILD\n");
prevPtr->rightTree = currPtr->leftTree;
free(currPtr->clientProfile);
free(currPtr);
return TRUE;
} else {
printf("CASE #5: DELETE NODE WITH 2 CHILDS\n");
// Finds the logical successor and places it into: tempPtr
ClientTree tempPtr = currPtr->rightTree;
while(tempPtr->leftTree) {
tempPtr = tempPtr->leftTree;
}
/*
* C)
*
* This has a big problem...
*
* If you take a look at the ClientProfile structure,
* in the first post, you'll see two ints
* (clientNIF/clientAge) and one char* (clientName).
*
* The problem is that the following code line is only
* copying the integer data, not the string. For some
* reason, the string remains the old one.
*
* I tried to use strdup() directly on clientName like:
* currPtr->clientProfile->clientName = strdup(tempPtr->clientProfile->clientName);
* but it still doesn't work.
*
* Why everything is being copied but the strings?
*/
currPtr->clientProfile = tempPtr->clientProfile;
/*
* D)
*
* Is there anyway to not call the function itself
* and make the while loop once again and delete the
* corresponding leaf?
*/
return deleteClientFromTree(&currPtr->rightTree, tempPtr->clientProfile->clientName);
}
}
}
}