Sign in to follow this  
Alpha_ProgDes

iterating and comparing pointers

Recommended Posts

this is what's happening: i have a binary tree i created where i traverse through a preorder function to find a particular node. this function returns a pointer to that node:
treeNode * preOrderFind (int find, treeNode* & node) {
    if (node != 0) {
       if (node->data == find)
          return node;
       preOrderFind(find, node->left);
       preOrderFind(find, node->right);
    }
}

//it is used like this 
int removeNode (int find) {

   treeNode* findNode = preOrderFind(find, root);
   //.....more code
}
now when i run this code i'm comparing findNode like this:
if (findNode == findNode->parent->left) {

   //remove node and adjust tree as necessary
}
unfortunately, the code never works the two don't seem equal.... can anyone provide an idea as to why?

Share this post


Link to post
Share on other sites
I think you need to give your recusive calls a way to communicate to the calling function.
I believe something like this would happen with your code:

first call -> not there
checkleft1 -> not there
checkleft2 -> not there
checkleft3 -> NULL
checkright3 -> node found, but calling function doesn't know
checkright2 -> not there
checkright1 -> not there
return nothing


I think you want something like this:

first call -> not there
checkleft1 -> not there
checkleft2-> not there
checkleft3 -> NULL
checkright3 -> node found
return node from checkright3
return node from checkleft1

Share this post


Link to post
Share on other sites
Quote:
Original post by Alpha_ProgDes

treeNode * preOrderFind (int find, treeNode* & node) {
if (node != 0) {
if (node->data == find)
return node;
preOrderFind(find, node->left);
preOrderFind(find, node->right);
}
}

You are not capturing the output of the recursive preOrderFind() calls.

Corrected:

treeNode * preOrderFind (int find, treeNode* & node)
{
treeNode* Result = 0;

if (node != 0)
{
if (node->data == find)
Result = node;
else
{
Result = preOrderFind(find, node->left);
if( Result == 0 )
Result = preOrderFind(find, node->right);
}
}

return Result;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by CGameProgrammer
Quote:
Original post by Alpha_ProgDes

treeNode * preOrderFind (int find, treeNode* & node) {
if (node != 0) {
if (node->data == find)
return node;
preOrderFind(find, node->left);
preOrderFind(find, node->right);
}
}

You are not capturing the output of the recursive preOrderFind() calls.

Corrected:

treeNode * preOrderFind (int find, treeNode* & node)
{
treeNode* Result = 0;

if (node != 0)
{
if (node->data == find)
Result = node;
else
{
Result = preOrderFind(find, node->left);
if( Result == 0 )
Result = preOrderFind(find, node->right);
}
}

return Result;
}


treeNode * preOrderFind (int find, treeNode* & node) {
if (node != 0) {
if (node->data == find)
return node;
preOrderFind(find, node->left);
preOrderFind(find, node->right);
}
}

Just so I understand where I went wrong in my thinking.
in the first line, i make sure the node being passed is not 0 (my base case)
check to see if the data in the node is equal to find return the node
else (granted I did not have that in actual code) passed the node from the left and passed the node from the right and check again.

...obviously my understanding of what's going on is wrong, but i'm still not understanding why exactly.

Share this post


Link to post
Share on other sites
Quote:
Original post by Alpha_ProgDes
Just so I understand where I went wrong in my thinking.
in the first line, i make sure the node being passed is not 0 (my base case)
check to see if the data in the node is equal to find return the node
else (granted I did not have that in actual code) passed the node from the left and passed the node from the right and check again.

...obviously my understanding of what's going on is wrong, but i'm still not understanding why exactly.


When you make the recursive call to preOrderFind() for either child, the result isn't stored anywhere. So when the recursion is "backing off" from the stack, it doesn't get passed back towards the original call - it is just thrown away.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this