【问题标题】:Can't returning a tree node when is found找到时无法返回树节点
【发布时间】:2018-11-25 19:32:54
【问题描述】:

找到树节点后如何返回?

所以,我有一棵二叉树,当我在树中搜索我搜索的内容时,我想返回指向该节点的指针,这样我就可以在其他函数中使用该节点。有我的搜索功能:

Tnode *Tsearch(Tnode *r, char *word) {
    if(r == NULL) {
        printf("%s NOT FOUND\n", word);
        return NULL;
    }
    int comp = strcasecmp(r->word, word);
    if( comp == 0) {
        printf("%s FOUND\n", r->word);
        return r;
    }
    else if( comp > 0) {
        Tsearch(r->left, word);
    }
    else if( comp < 0) {
        Tsearch(r->right, word);
    }
    return 0;
}

我的问题是当我尝试使用函数 Tsearch 的返回时它不起作用,我真的不明白为什么以及如何解决它。

我想使用从搜索函数返回的节点的函数如下:

int Tsearch_ref(Tnode *r, char (*words)[30]) {
    if(r == NULL) {
        return 0;
    }
    printf("%s, %d", words[0], (int)strlen(words[0]));
    Tsearch(r,words[0]);
    auxT = Tsearch(r,words[0]);
    Lnode *aux = auxT->head;
    printf("Title: %s\n", ((Book *)aux->ref)->title);
    while(aux != NULL) {
        aux_arr[i].ref=aux->ref;
        printf("Title: %s\n", ((Book *)aux_arr[i].ref)->title);
        printf("%p\n", &(aux_arr[i].ref));
        aux = aux->next;
        i++;
    }
}

这个功能不完整,因为我试图解决返回问题,但基本上我想把里面有一个列表的树节点放在一个临时数组中。

结构如下:

typedef struct {
    char *title;
    char isbn13[ISBN13_SIZE];
    char *authors;
    char *publisher;
    int year;
} Book;

typedef struct lnode {
    struct lnode *next;
    void *ref;
} Lnode;


typedef struct tnode {
    struct tnode *left;
    struct tnode *right;
    char *word;
    Lnode *head;
} Tnode;

这是我在 StackOverflow 的第一个问题,所以如果您需要任何关于任何事情的更多信息,我显然会提供。

提前致谢!

【问题讨论】:

    标签: c return structure binary-tree


    【解决方案1】:

    为什么不使用 while 循环?

    Tnode *Tsearch(Tnode *r, char *word) {
        Tnode *cur = r;
        int comp;
        while (cur != NULL)
        {
            if ((comp = strcasecmp(cur->word, word)) == 0)
            {
                printf("%s FOUND\n", cur->word);
                return cur;
            }
            else if (comp > 0)
            {
                /* Move to the left child */
                cur = cur->left;
            }
            else
            {
                /* Move to the right child */
                cur = cur->right;
            }
        }
        printf("NOT FOUND\n");
        return NULL;
    }
    

    【讨论】:

    • 我的第一个版本就是这样,但我遇到了同样的问题,因为我没有制作cur = cur-&gt;left/right;,所以我制作了第二个版本并且遇到了同样的问题!但谢谢你的选择!
    【解决方案2】:

    您需要更改对 Tsearch 的递归调用以实际返回找到的节点。所以,而不是这个代码:

    else if( comp > 0) {
        Tsearch(r->left, word);
    }
    else if( comp < 0) {
        Tsearch(r->right, word);
    }
    

    这样做:

    else if( comp > 0) {
        return Tsearch(r->left, word);
    }
    else if( comp < 0) {
        return Tsearch(r->right, word);
    }
    

    请注意,如果您的树非常深,您可能会用完整个调用堆栈并引发异常。

    【讨论】:

    • 它解决了错误(我明白为什么),但现在我还有另一个问题,例如在printf("Title: %s\n", ((Book *)aux-&gt;ref)-&gt;title); 中它没有像我预期的那样打印任何内容。这可能是什么原因?再次感谢您!
    • 无法说明为什么没有打印出来。我猜你需要单步调试 gdb 或调试器中的代码,看看aux-&gt;ref 的值是多少。您可能必须创建一个局部变量 Book *b = aux-&gt;ref; 来检查调试器中的值。
    • 好的,这就是为什么在深夜工作不好的原因,Tsearch_ref 没有打印,因为我没有调用该函数。我意识到使用调试器后。非常感谢您的大力帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 2018-12-31
    • 1970-01-01
    • 2011-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多