Stack Exchange
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.

Update from comment
My input is:
1 2 3 4 -1 -1 5


It might help if you could add to the question which input you used when you observed the problem.

When you build your tree, there are three alternatives that set the left child, only on that sets the right. And please use a debugger to pinpoint the line where the problem is happening.

At which action, you are getting seg. fault?

Run in a debugger, it will stop at the location of the crash and let you examine values of variables. If that can't help you solve the problem, then at least point out where in the source the crash happens.

my input is 1 2 3 4 -1 -1 5

Take care of right child. That means instead of newNode->left = NULL; You have to write newNode->right = NULL; in second condition.

leftChild > n should be leftChild >= n. Arrays index from 0 in C

After i edited as you mentioned in above comments it is working for some inputs but for large input like where N = 17   and Node are ( 1  2  3  4 -1  5 -1 6 -1 7 8 -1 9 -1 -1 10 11) it is not working.

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.

It’s nice you corrected them – however the learning effect is equal to null. Please point out what you changed and why. Stack Overflow is about learning – it is not a code debugging service.

You should at least list what you modfied and why.

Giving you a little love because you are trying.  Welcome to the community!
