Hi all - any help would be appreciated
My main creates random numbers (per user input) for the number of nodes to add to tree. The first number works then I get this error msg:
The instruction at "0x0040115c" referenced memory at "0xcccccccc". The memory could not be "read". Click ok to terminate program.
Here's my main program, and below it is my add node function:
void main()
{
BasicTree rewt;
TreeNode temproot;
TreeNode addit;
int nodenumb;
srand(unsigned(time(NULL)));
cout << "How many nodes do you want to create?";
cin >> nodenumb;
for (int i = 0; i < nodenumb; i++)
{
addit.key = rand();
rewt.add_item_to_tree(&addit, &temproot); // this statement I think is giving me all the trouble.. what am I doing wrong?
cout << "Throw this in the binary tree: " << addit.key << endl;
}
}
Here's my add node function:
bool BasicTree::add_node_to_tree(TreeNode *item,TreeNode *subtree)
{
bool ret_val = true;
if (root == NULL)
{ //we are adding the first node to the tree
root = item;
}else
{
if (item->key <= subtree->key)
{
if (subtree->left == NULL)
{
subtree->left = item;
} else
{
ret_val = add_item_to_tree(item, subtree->left);
}
} else
{
if (subtree->right == NULL)
{
subtree->right = item;
} else
{
ret_val = add_item_to_tree(item, subtree->right);
}
}
}
return ret_val;
}