C中的函数总是返回0,尽管值为真

C中的函数总是返回0,尽管值为真,c,C,我正在编写一个名为isElementInBinaryTree的函数,该函数返回1(true)表示元素存在,否则返回0。我不明白为什么函数总是返回0,尽管数字4存在于二叉树中?执行递归调用时没有返回任何内容。因此,如果在树的根目录下找到该项,它将只返回1 此外,当到达树的底部时,如果没有找到任何内容,则不会返回任何内容,这需要返回0以指示失败 typedef struct BinaryTreeNode { int data; BinaryTreeNode * left; BinaryTreeNod

我正在编写一个名为isElementInBinaryTree的函数,该函数返回1(true)表示元素存在,否则返回0。我不明白为什么函数总是返回0,尽管数字4存在于二叉树中?

执行递归调用时没有返回任何内容。因此,如果在树的根目录下找到该项,它将只返回
1

此外,当到达树的底部时,如果没有找到任何内容,则不会返回任何内容,这需要返回
0
以指示失败

typedef struct BinaryTreeNode {
int data;
BinaryTreeNode * left;
BinaryTreeNode * right;
} BinaryTreeNode;

int isElementInBinaryTree(BinaryTreeNode *root, int search_item) {
    if(root) {
        if(search_item == root -> data) return 1;
        isElementInBinaryTree(root -> left, search_item);
        isElementInBinaryTree(root -> right, search_item);
    }
}

int main() {
    BinaryTreeNode one = {1, NULL, NULL}; // root of the binary tree
    BinaryTreeNode two = {2, NULL, NULL};
    BinaryTreeNode three = {3, NULL, NULL};
    BinaryTreeNode four = {4, NULL, NULL};
    BinaryTreeNode five = {5, NULL, NULL};
    BinaryTreeNode six = {6, NULL, NULL};
    BinaryTreeNode seven = {7, NULL, NULL};

    one.left = &two;
    one.right = &three;

    two.left = &four;
    two.right = &five;

    three.left = &six;
    three.right = &seven;

    printf("%d ", isElementInBinaryTree(&one, 4));
    printf("\n");

    return 0;
}

在执行递归调用时,没有返回任何内容。因此,如果在树的根目录下找到该项,它将只返回
1

此外,当到达树的底部时,如果没有找到任何内容,则不会返回任何内容,这需要返回
0
以指示失败

typedef struct BinaryTreeNode {
int data;
BinaryTreeNode * left;
BinaryTreeNode * right;
} BinaryTreeNode;

int isElementInBinaryTree(BinaryTreeNode *root, int search_item) {
    if(root) {
        if(search_item == root -> data) return 1;
        isElementInBinaryTree(root -> left, search_item);
        isElementInBinaryTree(root -> right, search_item);
    }
}

int main() {
    BinaryTreeNode one = {1, NULL, NULL}; // root of the binary tree
    BinaryTreeNode two = {2, NULL, NULL};
    BinaryTreeNode three = {3, NULL, NULL};
    BinaryTreeNode four = {4, NULL, NULL};
    BinaryTreeNode five = {5, NULL, NULL};
    BinaryTreeNode six = {6, NULL, NULL};
    BinaryTreeNode seven = {7, NULL, NULL};

    one.left = &two;
    one.right = &three;

    two.left = &four;
    two.right = &five;

    three.left = &six;
    three.right = &seven;

    printf("%d ", isElementInBinaryTree(&one, 4));
    printf("\n");

    return 0;
}

您的代码在递归时实际上不会返回任何内容,因此“返回值”通常是用于该目的的寄存器中剩余的内容

此代码

int isElementInBinaryTree(BinaryTreeNode *root, int search_item) {
    if(root) {
        if(search_item == root -> data) {
            return 1;
        }
        return isElementInBinaryTree(root -> left, search_item) || 
               isElementInBinaryTree(root -> right, search_item);
    } else {
        return 0;
    }
}
需要看起来更像这样

if(root) {
        if(search_item == root -> data) return 1;
        isElementInBinaryTree(root -> left, search_item);
        isElementInBinaryTree(root -> right, search_item);
    }
最后的
返回0确保在提供空指针时返回合理的内容

编译代码时,它应该显示有关类型化函数在没有返回语句的情况下终止的警告。您需要注意这些警告并解决它们

整个过程实际上可以简化为一行代码(尽管不太清晰)

if(root) 
{
    if(search_item == root -> data) return 1;

    if (isElementInBinaryTree(root -> left, search_item)) return 1;
    return isElementInBinaryTree(root -> right, search_item);
}
return 0;

它依赖于快捷方式求值,只进行所需的运算。

您的代码在递归时实际上不会返回任何内容,因此“返回值”通常是用于该目的的寄存器中剩余的内容

此代码

int isElementInBinaryTree(BinaryTreeNode *root, int search_item) {
    if(root) {
        if(search_item == root -> data) {
            return 1;
        }
        return isElementInBinaryTree(root -> left, search_item) || 
               isElementInBinaryTree(root -> right, search_item);
    } else {
        return 0;
    }
}
需要看起来更像这样

if(root) {
        if(search_item == root -> data) return 1;
        isElementInBinaryTree(root -> left, search_item);
        isElementInBinaryTree(root -> right, search_item);
    }
最后的
返回0确保在提供空指针时返回合理的内容

编译代码时,它应该显示有关类型化函数在没有返回语句的情况下终止的警告。您需要注意这些警告并解决它们

整个过程实际上可以简化为一行代码(尽管不太清晰)

if(root) 
{
    if(search_item == root -> data) return 1;

    if (isElementInBinaryTree(root -> left, search_item)) return 1;
    return isElementInBinaryTree(root -> right, search_item);
}
return 0;

它依赖于快捷方式计算,只根据需要执行。

执行递归时不返回任何内容。启动警告级别。您有一个
返回1,是的。您没有任何
返回0任何地方!执行递归时不返回任何内容。启动警告级别。您有一个
return1,是的。您没有任何
返回0任何地方!不需要
else
,因为没有通过
if
分支的路由,该分支不会在
return
语句中终止。它还迫使读者明白,如果不返回某些内容,就不能真正退出函数。我实际上喜欢使用
if
else
的二进制结构来涵盖所有情况。我考虑将
分支内的代码更改为使用
else
以及递归步骤。不需要
else
,因为没有通过
if
分支的路由,它不会在
return
语句中终止。它还迫使读者明白,如果不返回某些内容,就不能真正退出函数。我实际上喜欢使用
if
else
的二进制结构来涵盖所有情况。我考虑将
root
分支中的代码更改为使用
else
以及递归步骤。我几乎发布了该快捷版本,但我不想让OP大吃一惊我差点发布了那个快捷版本,但我不想让OP大吃一惊