2015-06-07 209 views
0
#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; 
} 

在这个程序中,我试图构建二叉树(不是二叉搜索树)。为此,我编写了上面的代码,但是我遇到了分段错误。为什么我在这个程序中出现分段错误?

我在这做的是从stdin中获取输入并将其存储到输入数组中,我试图构建二叉树。


从评论更新

我输入的是:

1 2 3 4 -1 -1 5 
+1

如果您可以在观察问题时添加问题所用的输入,可能会有所帮助。 –

+2

当你建立你的树时,有三个选项可以设置左边的孩子,只有这样才能设置好。并且请使用调试器来查明问题发生的位置。 – Mat

+1

在哪个行动中,您正在获得seg。故障? – ANjaNA

回答

0

有在你的代码的一些错误。所以基本上正确的代码应该如下所示:

#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; 
} 

请检查您的输入现在。

+1

很好,你纠正了他们 - 但是学习效果等于空。请指出你改变了什么以及为什么。堆栈溢出是关于学习 - 它不是一个代码调试服务。 – idmean

+3

你至少应该列出你修改的内容和原因。 – alk

+0

也请正确缩进您的代码。 – alk

相关问题