Computing.Net > Forums > Programming > C++ Binary Tree Insert Help!@#$

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

C++ Binary Tree Insert Help!@#$

Reply to Message Icon

Name: SmittyZ3M
Date: March 30, 2003 at 11:06:16 Pacific
OS: Win2K
CPU/Ram: 650/392
Comment:

What in gods name is wrong with this code??

void BST::insertNode(nodetype *& root, nodetype * newnode)
{
if (root == NULL)
root = newnode;

else if (newnode->getKey() > root->getKey())
insertNode(root->getRight, newnode);

else
insertNode(root->getLeft(), newnode);
}


Notes:

1. The line of code that invokes this function is myTree.insert(newnode); The insert function looks like this....

void BST::insert(nodetype * newnode)
{
insertNode(root, newnode);
}


2. My questions are...When I invoke the insert function...control of the program is now inside that function...it makes a call to insertNode, and sends it root and newnode as parametrs. Now..since root is a private data member of class BST, the insert function has direct access to it, so that means its passing insertNode the memory address of root..correct?

3. Next, root comes in as a reference parameter into insertNode...My book says that it needs to be reference, other people tell me it doesnt because its a private data member and im in a member functoin and have direct access to it. But what is really being passed recursively everytime insertNode is called within itself?

3. This program compiles, but it gives me warnings about passing root by reference. After the tree is loaded up, i do a cout getData() and it works, but if i try to move beyond root (to its left or right subtree) i get a segmentation fault, almost like all that exists is the damn root of the tree.

Please help!!



Sponsored Link
Ads by Google

Response Number 1
Name: Aboroba
Date: March 31, 2003 at 13:39:15 Pacific
Reply:

in the first line of code in your message:
void BST::insertNode(nodetype *& root, nodetype * newnode)

The first parameter "nodetype *& root" is actually a pointer type, pointers are addresses, so when you do *& you are actually passing the address of an address. I think the correct syntax of this should be "nodetype* root" which is still passing by reference.
Take the & off and try it.


0

Response Number 2
Name: Matthew Smith
Date: April 3, 2003 at 09:11:53 Pacific
Reply:

Aboroba - I fixed it. However the root parameter DID need to be passed by reference. When I first started it, I thought the exact same thing you did. Taking off the & however does NOT still by it by reference. By eliminating the &, all you are doing is passing a "copy" of the memory address of the original call. This means that when passing a pointer by value, if you change the value of the thing that is being pointed to by the pointer, you WILL see that reflected after the call. If you change WHAT the pointer is pointing to, i.e. changing the memory address of the pointer, this will not be reflected after the caller.

If you pass the pointer by reference, you were correct, you are basically passing a pointer to the pointer that you are passing. (if that isnt confusing enough !@#$) So therefore, if you change the memory address of what the pointer was originally pointing to, this change will be reflected after the call. This is key and essential in the implementation of a binary search tree, since the parameter root changes each and every time you call the insert method recursively.

Thanks for your time,

Matt


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C++ Binary Tree Insert Help!@#$

C++ Binary Tree Insert www.computing.net/answers/programming/c-binary-tree-insert/6003.html

C binary search tree problem www.computing.net/answers/programming/c-binary-search-tree-problem/13647.html

Binary tree www.computing.net/answers/programming/binary-tree/14539.html