Good evening. I get an access violation exception error when trying to destroy my BST. There have been posts about this before and I copied their accepted answer's reply and still didn't get the expected result. So I have this binary search tree implementation. Everything works well, until my code reaches the "return 0" from my int main() function.
I'll leave some code for you.
PQBST::~PQBST()
{
destroy();
}
inline void PQBST::destroy()
{
if (root)
destroy(root);
}
void PQBST::destroy(Node* node)
{
if (node->left) // this is where it gives me and access violation exception 0xDDDDDDDD
destroy(node->left);
if (node->right)
destroy(node->right);
delete node;
}
I know that this kind of error is thrown when you try to delete something that had been already deallocated, but I can't figure out why it would try to destroy my BST twice when I call the destroy function just once in my application (when I'm done with it). I commented the part where I manually destroy my BST, and after reaching "return 0", it gives me again
Unhandled exception thrown: read access violation.
node was 0xFF12C6AB
so it is not 0xDDDDDDDD but still an error . :|
My nodes look like this:
struct Node
{
Human info;
Node * left;
Node * right;
Node() {};
Node(Human value)
: info(value), left(NULL), right(NULL)
{
}
};
My BST class only has Node* root . I hope I gave you enough information. Thank you.
Edit: My nodes look like this now:
struct Node
{
Human info;
Node * left;
Node * right;
Node() { left = NULL, right = NULL; }
Node(Human value): info(value), left(NULL), right(NULL){}
Node(Human value, Node* left, Node* right) : info(value), left(left), right(right) {}
Node& operator=(const Node& n)
{
info = n.info;
left = n.left;
right = n.right;
return *this;
}
Human getInfo() const { return info; }
Node* getLeft() { return left; }
Node* getRight() { return right; }
~Node() { };
};
My PQBST:
class PQBST
{
private:
Node * root;
int m; //spaceship size - given by the user
public:
PQBST() { root = NULL; }
PQBST(int m) : root(NULL), m(m) {}
PQBST(Node* root, int m);
~PQBST();
PQBST::PQBST(Node * root, int m)
{
this->root = root;
this->m = m;
}