简体   繁体   English

无序二叉树遍历

[英]Unordered Binary Tree Traversal

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); 
}

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)

}

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符号:

    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. Fyi,我很懒,variable-> age等同于variable-> data-> birthday。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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