0

I have tried to build a binary tree for integer storage as the following code. There seems no error message in Eclipse. But when I tried to run it, the program stop instantly with windows stop error notification.

Node* insertNode(Node *root, int value){
Node* temp = root;
if(temp != NULL){
    while(temp->left != NULL || temp->right != NULL){
        if(value < temp->data){
            temp = temp->left;
        }
        else{
            temp = temp->right;
        }
    }
}
Node* new = (Node*)malloc(sizeof(Node));
if(value < temp->data){
    new->data = value;
    new->left = new->right = NULL;
    temp->left = new;
    return temp;
}
else{
    new->data = value;
    new->left = new->right = NULL;
    temp->right = new;
    return temp;
    }
}

This is a function for creating the binary tree. I would like to get a value any save it to the existing binary tree. After the right place has been found in the tree. I would like to create a new node with the value and store that as the pointer of parent. Here is the struct of Node.

struct Node{
int data;
struct Node *left;
struct Node *right;
};
typedef struct Node Node;

Here is the error message

4
  • 5
    Please provide minimal reproducible example Commented Nov 9, 2020 at 19:39
  • 1
    would help to see the error message, yes? most likely a null pointer issue Commented Nov 9, 2020 at 19:47
  • 4
    If you are inserting the 1st value, root = NULL, then in your insert function you derefernce temp, e.g. if(value < temp->data) invoking Undefined Behavior (and a likely SegFault). Handle the case where you are adding the 1st value to your tree. Commented Nov 9, 2020 at 19:57
  • 1
    Thank you for the comment very much. I have tried to add the case as you say. The problem is solved now. Commented Nov 9, 2020 at 20:08

1 Answer 1

1
if(temp != NULL){
    while(temp->left != NULL || temp->right != NULL){
        if(value < temp->data){
            temp = temp->left;
        }
        else{
            temp = temp->right;
        }
    }
}
Node* new = (Node*)malloc(sizeof(Node));
if(value < temp->data){

The if tests if temp is NULL. So apparently, temp might be NULL. Since the if only runs if temp isn't NULL, if temp was NULL, it still is when we get to the malloc.

But then we access temp->data unconditionally. But what if temp is NULL?

if(temp != NULL){ // temp might be NULL here
    while(temp->left != NULL || temp->right != NULL){
        if(value < temp->data){
            temp = temp->left;
        }
        else{
            temp = temp->right;
        }
    }
}
// if temp was NULL there, it still is here
Node* new = (Node*)malloc(sizeof(Node));
if(value < temp->data){ // Uh oh, we dereference temp here, what if it's NULL?

Boom.

Sign up to request clarification or add additional context in comments.

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.