I'm completely new to pointers. I've searched the net a little and found out that it has to do with wrongly allocated memory of some sort. While trying to debug this thing, I noticed it crashes as soon as the function "insert" finishes. What am I doing wrong?
In your destructor, you're trying to delete something you shouldn't be trying to delete. Replace your destructor with this and see if you can work it out :)
You are not using any of the member variables of the object that the function is called on which is a bit weird. You should not have to pass in the root node as argument to the functions. The node itself is the root of the sub tree.
When you create a node like you do on line 60, 63 and 68 the node will stop to exist when it goes out of scope. If you want to create a node object that stays alive after the current scope has ended you can use new. node temp(num, position); becomes node* temp = new node(num, position);
You have to keep at least one pointer to the object otherwise you will not be able to reach it later.
Well I removed the delete root_; command, and it seems to run without errors. But, why is it calling the destructor so many times? I don't understand. I thought it only calls the destructor when something isn't "reachable" anymore.
In addition, my insert method isn't working. I'm passing the values by reference, but it doesn't seem to change the "root" object in the main feature. Why is that?
void insert(int num)
{
node* position = find(num, *this);
node pos = *position;
if (pos.value < num)
{
pos.right = new node(num, position);
}
elseif (pos.value > num)
{
pos.left = new node(num, position);
}
}
//inserts element in correct position if not found.
pos will be deleted at the end of the insert function. If I understand it correctly you want to modify the node object pointed to by position. In that case just use position->right and position->left and remove pos from your code.
Everythings working now, and I've written my first datastructure, the binary tree, with basic functions like insert, remove, post-order traverse and height calculation. Thanks again for all the help I got.