2015-06-07 172 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

相關問題