简体   繁体   中英

Binary Search Tree in C Segmentation Fault

I've been trying to implement a simple binary search tree in C just as an exercise. I can insert elements into the tree, but at certain points (I haven't been able to figure out where) I'm getting a segmentation fault.

Here is my code:

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

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

void insert(struct node *treeNode, int key);
void outputTree(struct node *root);

int main(){
    //Store how many numbers the user will enter
    printf("How many numbers will you enter? > ");
    int numNumbers;
    scanf("%d", &numNumbers);

    //Create a root node
    struct node root;
    root.key = -1; //-1 Means the root node has not yet been set
    root.right = NULL;
    root.left = NULL;

    //Now iterate numNumbers times
    int i;
    for(i = 1; i <= numNumbers; ++i){
        int input;
        scanf("%d", &input);
        insert(&root, input);
    }

    outputTree(&root);

    return 0;
}

void insert(struct node *treeNode, int key){
    //First check if the node is the root node
    if((*treeNode).key == -1){
        printf("Root node is not set\n");
        (*treeNode).key = key; //If the root node hasn't been initialised
    }
    else {
        //Create a child node containing the key
        struct node childNode;
        childNode.key = key;
        childNode.left = NULL;
        childNode.right = NULL;

        //If less than, go to the left, otherwise go right
        if(key < (*treeNode).key){
            if((*treeNode).left != NULL){ 
                printf("Left node is not null, traversing\n");
                insert((*treeNode).left, key);
            }
            else {
                printf("Left node is null, creating new child\n");
                (*treeNode).left = &childNode;
            }
        }
        else {
            //Check if right child is null
            if((*treeNode).right != NULL){
                printf("Right node is not null, traversing...\n");
                insert((*treeNode).right, key);
            }
            else {
                printf("Right node is null, creating new child\n");
                (*treeNode).right = &childNode;
            }
        }
    }
}

void outputTree(struct node *root){
    //Traverse left
    if((*root).left != NULL){
        outputTree((*root).left);
    }
    printf("%d\n", (*root).key);
    if((*root).right != NULL){
        outputTree((*root).right);
    }
}

As of writing this question, I've just had the thought, are the child nodes being created on the stack, so when the recursive calls return, the references in the tree are pointing to a struct that no longer exists?

What is wrong here?

Thank you

You create childs node on the stack by static allocation. When the insert method is finished, the child reference become invalid. You should use dynamic allocation with malloc.

struct node *new_node(int key, struct node *left, struct node *right) {
    struct node *this = malloc(sizeof *this);
    this->key = key;
    this->left = left;
    this->right = right;
    return this;
}

don't forget to free all of your allocations with the free function.

edit : so to create the root just use

struct node *root = new_node(-1, NULL, NULL);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM