Can';t在找到时返回树节点

Can';t在找到时返回树节点,c,return,structure,binary-tree,C,Return,Structure,Binary Tree,找到树节点后如何返回它 因此,我有一个二叉树,当我在树中搜索时,我想返回指向该节点的指针,这样我就可以在其他函数中使用该节点。这是我的搜索功能: Tnode *Tsearch(Tnode *r, char *word) { if(r == NULL) { printf("%s NOT FOUND\n", word); return NULL; } int comp = strcasecmp(r->word, word); if

找到树节点后如何返回它

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

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;
}
这个函数并没有完成,因为我试图解决返回问题,但基本上我想把那个有列表的树节点放在一个临时数组中

结构如下:

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中的第一个问题,所以如果您需要关于任何内容的更多信息,我将提供它


提前谢谢

您需要更改对Tsearch的递归调用,以实际返回找到的节点。因此,与此代码不同:

else if( comp > 0) {
    Tsearch(r->left, word);
}
else if( comp < 0) {
    Tsearch(r->right, word);
}
else if(comp>0){
t搜索(右->左,单词);
}
否则如果(组件<0){
t搜索(右->右,单词);
}
这样做:

else if( comp > 0) {
    return Tsearch(r->left, word);
}
else if( comp < 0) {
    return Tsearch(r->right, word);
}
else if(comp>0){
返回搜索(r->左,word);
}
否则如果(组件<0){
返回搜索(r->右,word);
}

请注意,如果树很深,则可能会耗尽整个调用堆栈并引发异常。

为什么不使用而使用循环

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

它解决了错误(我理解了原因),但现在我仍然有另一个问题,例如在
printf(“Title:%s\n”,((Book*)aux->ref)->Title)中它没有像我期望的那样打印任何内容。这是什么原因呢?再次感谢你!很难说你为什么没有打印出来。我想您需要在gdb或一些调试器中逐步检查代码,看看
aux->ref
的值是什么。您可能需要创建一个局部变量
Book*b=aux->ref
检查调试器中的值。好的,这就是为什么深夜工作不好,
Tsearch\u ref
没有打印,因为我没有调用该函数。我意识到在使用调试器之后。非常感谢您的大力帮助!我的第一个版本是这样的,但我有同样的问题,因为我没有使
cur=cur->左/右所以我做了第二个版本,也遇到了同样的问题!但是谢谢你的选择!