简体   繁体   English

为什么在此程序中出现分段错误?

[英]Why I am getting segmentation fault in this program?

#include<stdio.h>
#include<stdlib.h>

struct Btree{
    int data;
    struct Btree *left;
    struct Btree *right;
};

struct Btree *root = NULL;

struct Btree *createTree(int i,int *input,int n){

    int leftChild = 2*i+1,rightChild = 2*i+2;
    struct Btree *newNode = NULL;
    newNode = (struct Btree *)malloc(sizeof(struct Btree));
    if(input[i] == -1){
           return NULL
    }else{
          newNode->data = input[i];
          newNode->left = NULL;
          newNode->right = NULL;
    }
    if(root == NULL){
        root = newNode;
    }
    if(leftChild > n || input[leftChild] == -1 ){
        newNode->left = NULL;
    }else{
        newNode->left = createTree(leftChild,input,n);      
    }
    if(rightChild > n || input[rightChild] == -1 ){
        newNode->right = NULL;
    }else{
        newNode->right = createTree(rightChild,input,n);
    }
    return newNode;
}

void inorder(struct Btree *root){
    if(root){
        inorder(root->left);
        printf("%d",root->data);
        inorder(root->right);
    }
}


int main(){
    int n,i;
    printf("Enter values of N : \t");
    scanf("%d",&n);
    int input[n];
    printf("enter input nodes");
    for(i=0;i<n;i++){
        scanf("%d",&input[i]);
    }   
    for(i=0;i<n;i++){
        printf("%d ",input[i]);
    }       
    printf("\n");   
    root = createTree(0,input,n);           
    inorder(root);
    return 0;
}

In this program I was trying to construct Binary tree (not Binary Search tree). 在这个程序中,我试图构造二叉树(不是二叉搜索树)。 For that I wrote above code, but I am getting segmentation fault. 为此,我编写了上面的代码,但是遇到了分段错误。

What I was doing in this is taking inputs from stdin and storing into input array from that I was trying to construct Binary tree. 我这样做的目的是从stdin接收输入,然后将其存储到我试图构造二叉树的输入数组中。


Update from comment 从评论更新

My input is: 我的输入是:

1 2 3 4 -1 -1 5

There are some errors in your code. 您的代码中有一些错误。 So basically the correct code should be like as below: 因此,基本上正确的代码应如下所示:

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



struct Btree *createTree(int i,int *input,int n){
    int leftChild = 2*i+1,rightChild = 2*i+2;
    struct Btree *newNode = NULL;
    newNode = (struct Btree *)malloc(sizeof(struct Btree));
    newNode->data = input[i];
    newNode->left = NULL;
    newNode->right = NULL;
    if(leftChild >= n || input[leftChild] == -1 ){
        newNode->left = NULL;
    }else{
        newNode->left = createTree(leftChild,input,n);//you were passing the data of node which can vary to any integer       
    }
    if(rightChild >= n || input[rightChild] == -1 ){
        newNode->right = NULL;//While processing rightchild you have put the left child as null which was basically the reason for segmentation fault.
    }else{
        newNode->right = createTree(rightChild,input,n);//passing data of node instead of position 
    }
    return newNode;
}

void inorder(struct Btree *root){
    if(root){
        inorder(root->left);
        printf("%d\n",root->data);
        inorder(root->right);
   }
   return;
}


int main(){
    int n,i;
    printf("Enter values of N : \t");
    scanf("%d",&n);
    int input[n];
    struct Btree *root = NULL;
    printf("enter input nodes");
    for(i=0;i<n;i++){
        scanf("%d",&input[i]);
    }   
    for(i=0;i<n;i++){
        printf("%d ",input[i]);
    }       
    printf("\n");   
    root = createTree(0,input,n);           
    inorder(root);
    return 0;
}

Please check with your input now. 请立即检查您的输入。

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

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