0

I was working on Pre-order traverse Binary Tree algorithm. But I meet with Segmentation fault:11 error. The codes are shown as follow.

I want to know why this error happens. By the way, I have tried non-recursion algorithm preorder(),segmentation fault still happen.

The environment: macOS, clang-800.0.38

struct Node{
    char val;
    Node* left;
    Node* right;
};

void preorder(Node *T){
    if(T!=NULL){
        cout << T->val;
        preorder(T->left);
        preorder(T->right);
    }
}

int main(){
    Node *T = (Node *)malloc(sizeof(Node));
    Node *p = T;
    p->val = 'A';
    p->left = (Node *)malloc(sizeof(Node));
    p->left->val = 'B';
    p->right = (Node *)malloc(sizeof(Node));
    p->right->val = 'C'; 
    preorder(T);
    return 0;
}
2
  • 1
    malloc won't zero-out the memory for you. Which means, the Node malloc-ed could have non-NULL left and right so your preorder() will incorrectly traverse into invalid memory location. As you are using C++, why don't you write a constructor for your class and use new, instead of using malloc ? Commented Mar 22, 2019 at 6:47
  • Both answers have pointed out the cause. You could do self-diagnosis here in the future: segfault.stensal.com/a/e4e7lMyfNQ62slZH Commented Mar 22, 2019 at 8:21

2 Answers 2

2

You need to initialize left and right member variables of your nodes to null pointers.


Anyway, if you use C++, use C++ and not C constructs. This is an exemplary C++14 version of your code:

#include <iostream>
#include <memory>

struct Node{
   Node(char a) : val(a) { };
   char val;
   std::unique_ptr<Node> left, right;
};

void preorder(Node* p) {
   if (p) {
      std::cout << p->val;
      preorder(p->left.get());
      preorder(p->right.get());
   }
}

int main() {
   auto root = std::make_unique<Node>('A');
   root->left = std::make_unique<Node>('B');
   root->right = std::make_unique<Node>('C');

   preorder(root.get());
}
Sign up to request clarification or add additional context in comments.

2 Comments

sry for asking, but isn't this plain c++11??
being in the good position, to be able to use always the newest features. i lost track of the versions. thy for the answer =)
0

The problem is left and right of your child nodes are not initialized to NULL. Due to previous value stored in that memory location the program is encountering non-null value in your if(T!=nullptr) statement and executing the if block.

struct Node{
    char val;
    Node* left;
    Node* right;
};

void preorder(Node *T){
    if(T!=nullptr){
        cout << T->val;
        preorder(T->left);
        preorder(T->right);
    }
}

int main(){
    Node *T = (Node *)malloc(sizeof(Node));
    Node *p = T;
    p->val = 'A';
    p->left = (Node *)malloc(sizeof(Node));
    p->left->val = 'B';
    p->left->left = nullptr;   //Initialize to NULL
    p->left->right = nullptr;  //Initialize to NULL
    p->right = (Node *)malloc(sizeof(Node));
    p->right->val = 'C'; 
    p->right->left = nullptr;  //Initialize to NULL
    p->right->right = nullptr; //Initialize to NULL
    preorder(T);
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.