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 a given state for its oldest resident; however, the residents are organized in the tree by alphabetical order, which does absolutely nothing for my search. Thus, I have to traverse the entire tree of residents, updating the node that saves the oldest person, and return it once the tree has been fully traversed. I have the first part of my code but am stuck on how to write the rest of the recursion.
The state tree's method:
node <Person*> * findoldest (int obd, node <Person*> * oldest, node <Person*> * n)
{
//FINAL WORKING CODE
if (root == NULL)
return NULL;
if (n == NULL)
return NULL;
else
{
if (n->data->birthday < obd)
{
obd = n->data->birthday;
oldest = n;
}
node <Person*> * o_left = findoldest(obd, oldest, n->left);
node <Person*> * o_right = findoldest(obd, oldest, n->right);
node <Person*> * res;
if (o_right && o_left)
if (o_right->data->birthday < o_left->data->birthday)
res = o_right;
else
res = o_left;
else
res = (o_right != NULL ? o_right : o_left);
if (res && oldest)
if (res->data->birthday < oldest->data->birthday)
return res;
else
return oldest;
else
return ((res != NULL ? res : oldest));
}
}
And then the public "wrapper" state tree method:
node <Person*> * findoldest ()
{ int oldest_bday = root->data->birthday;
node <Person*> * oldest_person = root;
findoldest(oldest_bday, oldest_person, root);
}