mardi 18 juin 2013

Category tree, left node, right node.


Given a Category lambda, left node, right node., I want to know the parent node.

"""
node* get_parent(node* N)
{
    //define parent of nullptr to be nullptr
    if (!N)
        return nullptr;
    while (true)
    {
        if (N->left)
        {
            //N->left is either the previous sibling or the parent
            if (N->left->child == N) //N->left is the parent
                return N->left;
            else //N->left is the previous sibling
                N = N->left;
        }
        else //a node with left==nullptr is the root, so its parent is nullptr
        {
            return nullptr;
        }
    }
}
"""