简体   繁体   中英

Binary Tree Traversal (Mostly) Failing

For class, I have to create a binary tree of state objects, each of which includes a binary tree of resident objects organizing the people who live in each state. I'm trying to search the overall tree of states for a person by name (both state and resident trees are organized alphabetically by name), which involves traversing the entire tree of states and searching each state's resident tree for said person. Clearly my state tree traversal isn't working because most of the time, it tells me the person is not in the database (ie my stateforperson method, listed below, of the tree returns NULL) when I know for a fact that they do exist in the database. I am certain that my searchfor() method works.

node <Person*> * stateforperson (string nm, node <T> * n)
{    if (n !=  NULL)
     {
         node <Person*> * person = n->data->residents->searchfor(nm);
         if (person != NULL)
             return person;
         return stateforperson(nm, n->left);
         return stateforperson(nm, n->right);
     }
     else
         return NULL;
}

Attempted Update:

node <Person*> * stateforperson (string nm, node <T> * n)
{        if (n !=  NULL)
         {
             node <Person*> * person = n->data->residents->searchfor(nm);
             if (person != NULL)
                 return person;
             // Here, you explore the left branch and get the results.
             node <Person*> * left_ret = stateforperson(nm, n->left);
             // Here, same with right branch.
             node <Person*> * right_ret = stateforperson(nm, n->right);
             // You now have both results.
             if (left_ret != NULL) // If a result was found in left branches, you return that person. 
                 return left_ret;
             else if (right_ret != NULL) // Same with right branch.
                 return right_ret;
             else // The problem was here. Before you returned uninitialized memory. (because there wasn't a specified return value.
             // Now, you return a NULL pointer if nothing was found. 
             //So you detect that no person was found and don't use unitialized memeory.
                 return (NULL);
              }
              else
                  return NULL;
    }

I believe that what's happening is that you only explore the left nodes. Never go right because you return before :

     return stateforperson(nm, n->left); 
     // Here you just return the left part. Never reaching the following line
     return stateforperson(nm, n->right);

Try actually storing the values and something more like :

    left_ret = stateforperson(nm, n->left);
    right_ret = stateforperson(nm, n->right);

Than do whatever checks you need done on your variables to return the correct one.

(I believe that is the problem at least. Haven't done any recursive programming in a while so i could be mistaken.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM