|
-
November 14th, 2008, 05:08 AM
#1
Pointer Panic
Hi all,
I'm writing a program that implements a binary search tree datastructure using pointers.
However, i have encountered a problem i can't seem to figure out...
Code:
void bst::addnode(int val)
{
findaspot(val);
node* newnode;
if(theplacetobe == root)
{
cout << "root is tha place to be" << endl;
newnode = root;
}
else
{
if(val >= theplacetobe->value)
{
newnode = theplacetobe->right;
}
else
{
newnode = theplacetobe->left;
}
}
newnode = new node;
newnode->value = val;
cout << endl <<"newnode value:" << newnode->value << endl;
cout << "root value: " << root->value << endl;
}
void findspot(int) sets pointer theplacetobe to the leaf of the tree where i want to add the new node. (for those who don't know: a binary search tree is a tree in wich new nodes have at most 2 children, the right one bigger than the node itself, the left one smaller.)
now here's the problem:
when the first node is added (and newnode therefore == root), newnode->value is equal to val
root->value, however is nonsense (somewhere around 9.000.000).
Why does this happen, when i have set newnode to be equal to root?
Many thanks in advance!!
-Mohammed
Last edited by Moghammed; November 16th, 2008 at 04:50 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|