Jump to content
  • Advertisement
Sign in to follow this  
Alpha_ProgDes

iterating and comparing pointers

This topic is 4584 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!