Here's my code:
///////////////////////////////////////////////////////////
// Name: ~node
// Purpose: Basic Destructor; Calls DeleteTree which traverses the tree and
// deletes all of the nodes
// Recieve: NOTHING
// Return: NOTHING
///////////////////////////////////////////////////////////
node::~node()
{
DeleteTree(Root); //Passing a Ptr to the root node
}
///////////////////////////////////////////////////////////
// Name: DeleteTree
// Purpose: Called by the destructor, traverses the tree and deletes all of the nodes
// Recieve: Node, which is the root node the first time the function is called
// Return: NOTHING
///////////////////////////////////////////////////////////
void node:eleteTree(NODE * Node)
{
if(Node->Left != NULL)
DeleteTree(Node->Left);
if(Node->Right != NULL)
DeleteTree(Node->Right);
delete Node;
Node = NULL;
}
// END OF CODE
My problem is that by the time the Root is passed into my recursive function it is already deleted by the destructor. (The Root is set to NULL before it is passed in). As far as I know this will cause memory leaks because Ive lost access to all the spots in memory that the Root node gave me access to. Please Help if you can! Criticism of my coding style is welcome as long as you can help me out
I do know that my delete function works because I've tested it in other cases such as when I want to clear all the nodes and build a new tree.
Thanks!
Jonathan
///////////////////////////////////////////////////////////
// Name: ~node
// Purpose: Basic Destructor; Calls DeleteTree which traverses the tree and
// deletes all of the nodes
// Recieve: NOTHING
// Return: NOTHING
///////////////////////////////////////////////////////////
node::~node()
{
DeleteTree(Root); //Passing a Ptr to the root node
}
///////////////////////////////////////////////////////////
// Name: DeleteTree
// Purpose: Called by the destructor, traverses the tree and deletes all of the nodes
// Recieve: Node, which is the root node the first time the function is called
// Return: NOTHING
///////////////////////////////////////////////////////////
void node:eleteTree(NODE * Node)
{
if(Node->Left != NULL)
DeleteTree(Node->Left);
if(Node->Right != NULL)
DeleteTree(Node->Right);
delete Node;
Node = NULL;
}
// END OF CODE
My problem is that by the time the Root is passed into my recursive function it is already deleted by the destructor. (The Root is set to NULL before it is passed in). As far as I know this will cause memory leaks because Ive lost access to all the spots in memory that the Root node gave me access to. Please Help if you can! Criticism of my coding style is welcome as long as you can help me out
I do know that my delete function works because I've tested it in other cases such as when I want to clear all the nodes and build a new tree.
Thanks!
Jonathan