使用递归函数在二进制树中找到节点的父

发布于 2025-01-30 09:31:35 字数 1983 浏览 4 评论 0原文

我知道这是以前曾多次问过的,但是我找不到解决我实施中问题的答案。我正在使用的结构:

typedef struct STnode* link;
typedef struct STnode {Item item; link l, r; int N;}STnode;
link NULLnode;

插入功能:

link NEW(Item item, link l, link r, int N)
{
    link x = malloc(sizeof *x);
    x->item = item;
    x->l = l;
    x->r = r; 
    x->N = N;
    return x;
}

void InsertLeft (link node, Item item)
{
    node->l = NEW (item, NULLnode, NULLnode, 1); 
}

void InsertRight(link node, Item item)
{
    node->r = NEW(item, NULLnode, NULLnode, 1);
}

void BTinsertR (link node, Item item)
{
    InsertRight(node, item);
}

void BTinsertL (link node, Item item)
{
    InsertLeft(node, item);
}

最后,我一直在撞击了几个小时的问题功能。它应该返回给定节点的父件。

Item Parent(link root, link node)
{
    Item temp;
    if (root==node)
    {
        printf("The given node is root of a tree and thus has no parent\n");
        return NULLitem;
    }
    else if(root == NULLnode)
    {
        return temp;
    }
    else if(root->l==node || root->r==node)
    {
        temp = root->item;
        printf("Item of parent's node: %d\n", temp);
        return temp;
    }
    else
    {
        Parent(root->l, node);
        Parent(root->r, node);
    }
    return temp;
}

取而代之的是打印正确的项目,但返回了我插入的最后一个节点的项目。我唯一更改 temp 的值的时间是当我找到节点时,那么它在最后一个呼叫中如何改变?

编辑:忘记添加,我的项目是简单的整数,主要功能是

int main(int argc, char *argv[])
{
    Item item = ITEMscan();
    NULLmaker();
    link root1 = MakeTree(item);
    item = ITEMscan();
    link root2 = MakeTree(item);
    item = ITEMscan();
    BTinsertL(root1, item);
    item = ITEMscan();
    BTinsertR(root1, item);
    item = ITEMscan();
    BTinsertL(root1->l, item);
    item = ITEMscan();
    BTinsertR(root1->l, item);
    item = ITEMscan();
    BTinsertL((root1->l)->l, item);
    Parent(root1, (root1->l)->l);
    return 0;
}

maketree简单地将树初始化

I know this one has been asked many times before but I could not find an answer that solves the problem in my implementation. The struct I'm using:

typedef struct STnode* link;
typedef struct STnode {Item item; link l, r; int N;}STnode;
link NULLnode;

Insertion functions:

link NEW(Item item, link l, link r, int N)
{
    link x = malloc(sizeof *x);
    x->item = item;
    x->l = l;
    x->r = r; 
    x->N = N;
    return x;
}

void InsertLeft (link node, Item item)
{
    node->l = NEW (item, NULLnode, NULLnode, 1); 
}

void InsertRight(link node, Item item)
{
    node->r = NEW(item, NULLnode, NULLnode, 1);
}

void BTinsertR (link node, Item item)
{
    InsertRight(node, item);
}

void BTinsertL (link node, Item item)
{
    InsertLeft(node, item);
}

Finally the problem function that I have been banging my head against for hours. It's supposed to return the item of the parent of the given node.

Item Parent(link root, link node)
{
    Item temp;
    if (root==node)
    {
        printf("The given node is root of a tree and thus has no parent\n");
        return NULLitem;
    }
    else if(root == NULLnode)
    {
        return temp;
    }
    else if(root->l==node || root->r==node)
    {
        temp = root->item;
        printf("Item of parent's node: %d\n", temp);
        return temp;
    }
    else
    {
        Parent(root->l, node);
        Parent(root->r, node);
    }
    return temp;
}

Instead it prints the right item but returns the item of the last node I insert. The only time I alter the value of temp is when I find the node, so how is it possible that it changes at the last call?

EDIT: Forgot to add, my items are simple integers and the main function is

int main(int argc, char *argv[])
{
    Item item = ITEMscan();
    NULLmaker();
    link root1 = MakeTree(item);
    item = ITEMscan();
    link root2 = MakeTree(item);
    item = ITEMscan();
    BTinsertL(root1, item);
    item = ITEMscan();
    BTinsertR(root1, item);
    item = ITEMscan();
    BTinsertL(root1->l, item);
    item = ITEMscan();
    BTinsertR(root1->l, item);
    item = ITEMscan();
    BTinsertL((root1->l)->l, item);
    Parent(root1, (root1->l)->l);
    return 0;
}

MakeTree simply initializes the tree

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文