Hello,
I've posted a few topics on these forums and have received a large amount of help. I'm winding up the program, finally, and I've come to a huge snag. The last function I need to get working is deleting a node from a binary search tree. I understand the algorithm but i'm having really big issues implementing it in code.
Here is the code I have, currently:
Code:
void removeTree(TreeT tree, TreeInfoT data)
{
printf("Inside removeTree. Data= %s Node=%s \n",data->name,tree->root->info->name);
deleteNode(tree->root,data);
}
void deleteNode(TreeNodeT root,TreeInfoT data)
{
TreeNodeT ptr=findNode(root,data); //find the node
printf ("inside deleteNode. Pointer from findNode=%s \n",ptr->info->name);
if (ptr->L!=NULL && ptr->R!=NULL) //two children
{
printf("Entered inside of 'Two Children' if statement \n");
TreeNodeT max=ptr->L; //set a pointer, max, to the node in the left tree to prepare to look
// for the highest value in the left subtree
while (max->R !=NULL) //if the node to the right of max isn't equal to null, do the following
max=max->R; //set max equal to the node to the right of it (highest value in left subtree)
ptr->info=max->info; //move the info from max into ptr
deleteNode(ptr->L,max->info);
}
else
if (ptr->L==NULL)
{
printf("Entered inside ptr->L equal to null if statement \n");
if (ptr->R==NULL)
printf ("ptr->R equals NULL \n");
TreeNodeT tempRight=ptr->R;
if (tempRight==NULL)
printf("tempRight equals NULL \n");
printf ("ptr equals %s \n",ptr->info->name);
ptr=ptr->R;
if (ptr==NULL)
printf("ptr equals NULL \n");
free(tempRight);
}
else
if (ptr->R==NULL)
{
printf("Entered inside ptr->R equal to null if statement \n");
TreeNodeT tempLeft=ptr->L;
ptr=ptr->L;
free(tempLeft);
}
printf("Pre-exiting function check. \n");
if (ptr==NULL)
printf("PTR still equals NULL \n");
printf("Exiting Function \n");
return;
}
Note: There's still some print statements in there I was using for debugging (which I finally gave up on 5 hours later). I've tested findNode multiple times outside of the function and it IS correctly finding the nodes so that's not the issue.
Please help, anything to set me in the right direction would be amazing.
Anthony Vitello