I'm still working on my binary trees, and the insertion, lookup, maximum, minimum functions are all working great so far. So I want to do a deletion function next. I included a Stack which saves- ah hell, I'll just show the code:
class Tree
{
private:
int value_;
Tree *root_;
Tree *left_;
Tree *right_;
std::stack<Tree*> treeStack;
The delete function:
int Tree::eraseTree()
{
while( !treeStack.empty() )
{
Tree *temp = treeStack.top();
treeStack.pop();
delete temp;
}
if( treeStack.empty() )
return 1;
else
return -1;
}
I'm getting errors now. This wouldn't be much of a problem- I try to debug my own code- except this time it's telling me there is an error in the <deque>
library file which I am not even using.
Before the program shuts off I get System.AccessViolationException
and the faulty code points to the deque
file. Of course it can't be there, it has to be some pointer in my code. Which leads me to believe I am not working the stack correctly, or not pushing to the stack correctly.
What am I doing wrong here? Am I actually deleting the nodes when I call .pop on the stack?
EDIT:
if( !root_ )
{
root_ = new Tree(val, 0, 0);
treeStack.push(root_);
return val;
}
And
Tree *parent = findInsertionPoint(val, root_);
if( val < parent->value_ )
parent->left_ = new Tree(val, 0, 0);
else
parent->right_ = new Tree(val, 0,0);
treeStack.push(parent);
return val;
Is where I am pushing my elements on the stack.
Additional Question: Does a std::stack have to be built in the ctor?