简体   繁体   中英

C - Segmentation Fault while Creating Binary Tree Using recursion

I was trying to write a simple program of creating a binary tree using pointers in C , but I am unable to find the problem with this code. I am receiving Segmentation Fault on the second insertion.

The program takes input of five numbers and then creates a binary tree using the array input.Sample run :

Here is the output of the program

Enter 5 elements :

45 78 89 32 46

In generateBST

In insert Going to right sub tree

In insert

Segmentation fault

Please help me resolve this error. Thank you.

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

typedef struct node {
    int value;
    struct node * lst;
    struct node * rst;
}Node;

void printBST(Node *root){
    puts("In printBST");
    if(root == NULL){
        return;
    }
    printBST(root->lst);
    printf(" %d ", root->value);
    printBST(root->rst);
}

void insert(Node **root, int element){
    puts("In insert");
    if((*root)->value > element){
        puts("Going to left sub tree");
        insert(&(*root)->lst ,element);
    } else if ((*root)->value < element) {
        puts("Going to right sub tree");
        insert(&(*root)->rst ,element);
    } else {
        puts("Creating a new node to insert");
        Node * newNode = (Node*)malloc(sizeof(Node));
        newNode->value = element;
        newNode->lst = NULL;
        newNode->rst = NULL;
        (*root) = newNode;
    }
}

Node* generateBST(int *elements, int n){
    puts("In generateBST");
    int i =0;
    Node * root = NULL;
    root = (Node*)malloc(sizeof(Node));
    root->value = *(elements);
    root->lst = NULL;
    root->rst = NULL;
    for(i=1; i < n; i++){
        insert(&root, *(elements+i));
    }
    return root;
}

int main(){
    Node * root = NULL;
    int i = 0, element, *elements ;
    elements = (int*)malloc(sizeof(int)*5);
    puts("Enter 5 elements : ");
    fflush(stdin);
    for(i = 0; i < 5; i++){
        scanf("%d",&element);
        elements[i] = element;
    }
    root = generateBST(elements,5);
    printBST(root);
    //deallocBST(root);
    return 0;
}

In insert, if *root is null you get a segmentation fault when you dereference (*root)->value . You need to handle that case:

void insert(Node **root, int element){
    puts("In insert");
    if (*root == null || (*root)->value == element) {
        puts("Creating a new node to insert");
        Node * newNode = malloc(sizeof(Node));
        newNode->value = element;
        newNode->lst = NULL;
        newNode->rst = NULL;
        (*root) = newNode;
    } else if((*root)->value > element){
        puts("Going to left sub tree");
        insert(&(*root)->lst ,element);
    } else {  /* (*root)->value < element */
        puts("Going to right sub tree");
        insert(&(*root)->rst ,element);
    }
}

Just found your problem using gdb. You have an error on line 22. The problem is that you don't check whether root is null or not which is problem for the insertion of the first element.

To run your program through gdb simply compile and run using:

g++ -g YourPorgram.cpp
gdb a.out
run

When the programs stops (segfaultor other problems) type:

bt

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