2017-05-31 3 views
0

Ich versuche, ein Problem über binäre Baum zu lösen, aber ich habe etwas falsch mit dem Bau eines Baumes, vielleicht. Es scheint mir, verdrahtet, warum ich Segmentation fault bekam, wenn ich versuche, die Daten eines Knotens auszudrucken:Binärer Baum Draufsicht Segmentierung Fehler

//  3 
// / \ 
// 5  2 
///\ /\ 
// 1 4 6 7 
// \  /
// 9  8 

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

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

void top_view(struct node * root) { 

    int array[100] = {0}; 
    int count = 0; 
    struct node * temp; 

    if (root) { 
     if (root->left) { 
      temp = root; 
      while (temp->left) { 
       printf("%d ", temp->left->data); 
       // array[count] = temp->left->data; 
       count++; 
       temp = temp->left; 
      } 
     } 
     array[count] = root->data; 
     printf("%d\n", array[count]); 

     if (root->right != NULL) { 
      printf("right %d\n", root->right->data); 
      temp = root; 
      printf("%p\n", temp->right->right); 
      printf("%d\n", temp->right->right->data); 
     } 

     // for (int i = 0; i < 100; i++) { 
     //  printf("%d ", array[i]); 
     // } 

    } else { 
     return ; 
    } 

} 



int main(int argc, char const *argv[]) 
{ 
    // struct node *nine = (struct node*)malloc(sizeof(struct node*)); 
    // nine->data = 9; 
    // nine->left = NULL; 
    // nine->right = NULL; 

    // struct node *ten = (struct node*)malloc(sizeof(struct node*)); 
    // ten->data = 10; 
    // ten->left = NULL; 
    // ten->right = NULL; 

    // struct node *one = (struct node*)malloc(sizeof(struct node*)); 
    // one->data = 1; 
    // one->left = ten; 
    // one->right = nine; 

    // struct node *four = (struct node*)malloc(sizeof(struct node*)); 
    // four->data = 4; 
    // four->left = NULL; 
    // four->right = NULL; 

    // struct node *five = (struct node*)malloc(sizeof(struct node*)); 
    // five->data = 5; 
    // five->left = one; 
    // five->right = four; 

    struct node *eight = (struct node*)malloc(sizeof(struct node*)); 
    eight->data = 8; 
    eight->left = NULL; 
    eight->right = NULL; 

    struct node *six = (struct node*)malloc(sizeof(struct node*)); 
    six->data = 6; 
    six->left = NULL; 
    six->right = NULL; 

    struct node *seven = (struct node*)malloc(sizeof(struct node*)); 
    seven->data = 7; 
    seven->left = eight; 
    seven->right = NULL; 

    struct node *two = (struct node*)malloc(sizeof(struct node*)); 
    two->data = 2; 
    two->left = six; 
    two->right = seven; 

    struct node *three = (struct node*)malloc(sizeof(struct node*)); 
    three->data = 3; 
    three->left = NULL; 
    three->right = two; 

    top_view(three); 

    return 0; 
} 

die Ausgabe auf diese Weise ist:

3 
right 2 
0x7fc800000003 
[1] 2091 segmentation fault ./a.out 

, wo ich schief gelaufen ist mit ihm?

+3

1) 'acht = (struct node *) malloc (sizeof (struct node *));' -> 'acht = malloc (sizeof (struct node));' ' – BLUEPIXY

+2

Sollte sizeof (struct Knoten) '- Sie reservieren genug Speicher für nur einen Zeiger. –

+1

'printf ("% d \ n ", temp-> right-> right-> data);' Was passiert, wenn 'temp-> right-> right 'NULL ist? –

Antwort

0

Ich habe gerade bearbeitet auf den Code und es kompiliert und geben keinen Fehler. Ich änderte die Art, wie Sie malloc verwenden, um einen neuen Knoten zu initiieren und löschte die cast. Sie müssen auch prüfen, ob root->right->right existiert. Hier ist es mit den drei, die Sie an der Spitze des Codes zeigen.

/*  3 
* / \ 
* 5  2 
*/\ /\ 
* 1 4 6 7 
* \  /
* 9  8 
*/ 
#include <stdio.h> 
#include <stdlib.h> 

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

void top_view(struct node *root) { 

    int array[100] = {0}; 
    int count = 0; 
    struct node *temp; 

    if (root) { 
     if (root->left) { 
      temp = root; 
      while (temp->left) { 
       printf("%d ", temp->left->data); 
       // array[count] = temp->left->data; 
       count++; 
       temp = temp->left; 
      } 
     } 
     array[count] = root->data; 
     printf("%d\n", array[count]); 

     if (root->right != NULL) { 
      printf("right %d\n", root->right->data); 
      temp = root; 
      /*You must check it*/ 
      if(root->right->right != NULL){ 
       printf("%p\n", temp->right->right); 
       printf("%d\n", temp->right->right->data); 
      } 
     } 

      /*for (int i = 0; i < 100; i++) { 
      printf("%d ", array[i]); 
     }*/ 

    } else { 
     return ; 
    } 

} 



int main(int argc, char const *argv[]) 
{ 
    struct node *nine = malloc(sizeof(struct node)); 
    nine->data = 9; 
    nine->left = NULL; 
    nine->right = NULL; 

    /* struct node *ten = (struct node*)malloc(sizeof(struct node*)); 
    ten->data = 10; 
    ten->left = NULL; 
    ten->right = NULL;*/ 

    struct node *one = malloc(sizeof(struct node)); 
    one->data = 1; 
    one->left = NULL; 
    one->right = nine; 

    struct node *four = malloc(sizeof(struct node)); 
    four->data = 4; 
    four->left = NULL; 
    four->right = NULL; 

    struct node *five = malloc(sizeof(struct node)); 
    five->data = 5; 
    five->left = one; 
    five->right = four; 
    /*You should cast the result of a malloc just if you need it*/ 
    struct node *eight = malloc(sizeof(struct node)); 
    eight->data = 8; 
    eight->left = NULL; 
    eight->right = NULL; 

    struct node *six = malloc(sizeof(struct node)); 
    six->data = 6; 
    six->left = NULL; 
    six->right = NULL; 

    struct node *seven = malloc(sizeof(struct node)); 
    seven->data = 7; 
    seven->left = eight; 
    seven->right = NULL; 

    struct node *two = malloc(sizeof(struct node)); 
    two->data = 2; 
    two->left = six; 
    two->right = seven; 

    struct node *three = malloc(sizeof(struct node)); 
    three->data = 3; 
    three->left = five; 
    three->right = two; 

    top_view(three); 

    return 0; 
} 
0

Ihr printf versucht, Daten zu drucken, die an einen NULL-Zeiger angehängt sind. Wie @Johnny sagte, temp->right->right könnte so null sein temp->right->right->data segfault

Sie sollten einen Stop-Haken hinzufügen. Vorher:

printf(temp->right->right->data); 

Nach:

if (temp->right->right) 
    printf(temp->right->right->data);