codemonkey codemonkey - 1 month ago 6
C++ Question

Data Leaking Occurs When Deleting a Node From BST

if(parent!=NULL)
{
if((parent->leftChildPtr)==nodePtr)
{
parent->leftChildPtr=nodePtr->leftChildPtr;
}
else if(parent->rightChildPtr==nodePtr)
{
parent->rightChildPtr=nodePtr->leftChildPtr;
}
}
delPtr = nodePtr;
nodePtr = nodePtr->leftChildPtr;

delPtr->leftChildPtr = NULL;
delete delPtr;


I have this if statement instruction inside my removeItem function. I don't know how data leaking occurs. As you see, I point the node to be deleted with another pointer,which is delPtr, then set nodeptr to it's right, and delete the delptr and it's contents. So the parent of this NodePtr should see "nodePtr's rightchildptr" as its child as well, but no.

DrC DrC
Answer

You'd need to show more code but it seems very likely to me that while you are setting nodePtr, you aren't updating the parent node's appropriate child pointer. So it still holds the former value of nodePtr which is now invalid as it has been deleted.