Hi
Could someone help me define a reasonable destructor for this class:
class Bintre {
private:
int tall;
Bintre* left;
Bintre* right;
};
If I use this class, to nest together lots of objects of "Bintre".. what is a possible destructor to make sure all
the objects are deleted and all pointers are deleted?
It would be helpful if you explained if the pointers formed an array or maybe a b-tree because of the naming. Is it something else?
If it were just an array of Bintre then...
delete [] right;
delete [] left;
If it were a b-tree, you would have to traverse to the bottom leaves and delete the on your way up until there was none left (could be done recursively as shown below).
void Bintre::deleteNode(Bintre *ptr)
{
if (ptr-getLeft())
deleteNode(ptr-getLeft());
if (ptr-getRight())
deleteNode(ptr-getRight());
deleteNode(ptr);
}
Every time I try to write some kind of destructor to "clean up", my program always crash upon exit or display the
"null pointer assignment" upon exit..
Was the pointer null when you tried to delete it?
--
Danny Dorris
|