无法理解为什么输出为假

问题描述 投票:0回答:1

在这里,我无法理解为什么我的 BST 给出输出 false,因为我现在已经将输入创建为 BST,当我通过检查函数传递此输入 BST 时,它首先检查四件事,它检查是否 **1)**左子节点中的数据小于当前子节点中的数据 **2)**右侧的数据大于当前子级的数据则 **3)**它将左子节点的地址作为下一个操作的根传递,它将在下一个操作中运行,并对左子节点执行相同的操作(现在作为根传递),这里它将再次检查根(现在根之前的左孩子)的左孩子比现在小,右侧也比现在大 **4)**我们将右侧地址作为节点传递,以便我们可以检查它的左侧是否较小,右侧是否较大,并且在所有操作之后返回 true (如果有效),否则返回 false 。 它应该返回 true,但是输出是 false,我不明白为什么?

#include<stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct tree{
    int data;
    struct tree* left;
    struct tree* right;
};


struct tree* push(int d,struct tree*head1) {
    if(head1==NULL){
        head1=(struct tree*) malloc(sizeof(struct tree));
        head1->data=d;
        head1->left=head1->right=NULL;
        return head1;
    }
    else if(head1->data>d){;
        head1->right=push(d,head1->right);
    }
    else{
        head1->left=push(d,head1->left);
    }
    return head1;
}  
bool small(struct tree* a,int b){
    if(a==NULL){return true;}
    if(a->data<b){return true;}
    else {return false;}
}
bool large(struct tree* a,int b){
    if(a==NULL){return true;}
    if(a->data>b ){return true;}
    else {return false;}
}
bool check(struct tree*head ){
    if(head==NULL){
        printf("empty\n");
        return true ;}
    printf("2");
    if(small(head->left,head->data)&& large(head->right,head->data) && check(head->left) && check(head->right)){
        printf("true");
        return true;}
    else {
        printf("false\n");
        return false;
    }}



int main(){
    struct tree*head;
    head=push(3,head);
    head=push(10,head);
    head=push(5,head);
    check(head);
    // printf("%d",head->data);
    return 0;
}
c tree binary-search-tree
1个回答
0
投票

在功能中

main()
你有

struct tree*head;
head=push(3,head);

但是编译器反对你传递未初始化的变量。

push()
功能中,您有

if(head1==NULL)

但这没有用,因为

head1
的值是不确定的。在
main()
你应该有

struct tree *head = NULL;
head = push(3, head);

但可能还有其他问题。

© www.soinside.com 2019 - 2024. All rights reserved.