0

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); 
}
jiccan
  • 89
  • 12
  • Also, it's almost expected that you segfault. You never check your pointers before dereferencing them. Always check if o_left is not at NULL before doing : `o_left->data` – Khaldor May 01 '15 at 21:52
  • Wait so did that end up working fine? – Khaldor May 01 '15 at 22:15
  • Nope. Still seg-faulting. This seems very very complicated, with so many if/elses. Wow I am beyond confused. – jiccan May 01 '15 at 22:20
  • Im screwing up then. Let me go through this mess again. The problem i'm having is not being able to test this myself. :/ – Khaldor May 01 '15 at 22:23
  • I can give you all the code if you'd really like, but it's much more than just this piece. I don't mind, but you might lol – jiccan May 01 '15 at 22:24
  • Github? or another option? I just can't figure it out and be sure without testing this puppy. (Hell for all i now since i ain't even compiling my "solutions" don't even compile.) – Khaldor May 01 '15 at 22:26
  • Oh thank god... My head was starting to hurt there. FYI if this isn't patched before around 18:30 my time It's waiting for another day as far as i'm concerned. :-) – Khaldor May 01 '15 at 22:41
  • [cpp code](https://www.dropbox.com/s/oatyw85og34h7re/binarytree.cpp?dl=0) and [database file](http://rabbit.eng.miami.edu/class/een118/labs/152/dbfile1.txt) – jiccan May 01 '15 at 22:47
  • I'm not sure what time it is there, but I'll take whatever I can get. It's due sometime tomorrow which is why I'm having a panic attack, but after this bit is fixed, I'll only have one more command to code and then it's done. Your help is much, MUCH appreciated. – jiccan May 01 '15 at 22:48
  • Got another 2h30 something like that. Without breaks and work i am doing that doesn't involve stackOverflow. Out of curiosity seeing as i have to add headers and a `using namespace std;` what compiler are you using? – Khaldor May 01 '15 at 23:00
  • I'm compiling through unix, but I'm on my school's, uh, server or whatever. I'm really not sure how that works. – jiccan May 01 '15 at 23:09
  • their compiler version seems... a bit lax on checking stuff. – Khaldor May 01 '15 at 23:11

2 Answers2

2

This the pseudo-code you need:

node <Person*> * findoldest (node <Person*> * n)
{
    if n->right != null :
        right_oldest = findoldest(n->right)

    if n->left != null:
        left_oldest = findoldest(n->left)

    return the node that has max value in (right_oldest.data.birthday, left_oldest.data.birthday, n.data.birthday)

}
pjsofts
  • 170
  • 4
  • 18
1

Essentially, it's the same answer as your last post.

right_old = findoldestn(n->right);
left_old = findoldestn(n->left);

then figure out the oldest one between left/right and current, and return that value. And that can be put in place with

res = (right_old->age > left_old->age ? right_old : left_old);
finalRet = (res->age > oldest->age ? res : oldest);
return (finalRet);

Or an equivalent with if notation :

    if (right_old->age >left_old->age)
        res = right_old;
    else
        res = left_old;
    if (res->age > oldest->age)
        finalRes = res;
    else
        finalRes = oldest;

Fyi, i'm lazy, variable->age is equivalent to variable->data->birthday.

Khaldor
  • 294
  • 1
  • 16