简体   繁体   English

C 二叉树搜索

[英]C binary tree search

I'm trying to find node in binary tree, but the function returns nothing, NULL, By the way, in printf, in我正在尝试在二叉树中查找节点,但是 function 什么都不返回,NULL,顺便说一下,在 printf 中,

if ((cond = strcmp(head->word, word_to_search)) == 0)

the result is right, it just does not return value, probably I'm making it wrong with recursion, I don't know.结果是正确的,它只是没有返回值,可能是我用递归弄错了,我不知道。 By the way if I wrap last return NULL in else, it does return valid pointer, but it causes a warning...顺便说一下,如果我将 last return NULL 包装在 else 中,它确实会返回有效指针,但会导致警告...

struct count_tree *
binaryt_search(struct count_tree *head, char *word_to_search)
{
    int cond = 0;
    if (head != NULL) {
        if ((cond = strcmp(head->word, word_to_search)) == 0) {
            printf("Found %s %d\n", head->word, head->count);
            return head;
        }
        else if (cond < 0) {
            binaryt_search(head->left, word_to_search);
        }
        else {
            binaryt_search(head->right, word_to_search);
        }
    }
    return NULL;
}

You need to return the results of the calls to binaryt_search() .您需要将调用的结果returnbinaryt_search() So,所以,

binaryt_search(head->left, word_to_search);

becomes成为

return binaryt_search(head->left, word_to_search);

and

binaryt_search(head->right, word_to_search);

becomes成为

return binaryt_search(head->right, word_to_search);

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

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