-1

I'm having a problem with writing BST in C. I keep getting a segmentation-fault error. I believe the problem stems from the insertNode function. I added printf() statements in the function and directly after the function call to see if the newNode was being added. Please ignore the rest of the code as it is not finished, just trying to get the insertNode function to work.

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

//structure for node
struct btNode {
    int data;
    struct btNode *left;
    struct btNode *right;
};

//prototypes
struct btNode* createNode(int x);
void insertNode(struct btNode *tree, struct btNode *root);

int main(){
    int x,n=-1,i=0; //local variables

    struct btNode *head=NULL;

    while (n <= 0){
        printf("Enter the number of nodes in the Tree(>0): ");
        scanf("%i", &n);
    }


    while(i < n){
        printf("Enter an integer: ");
        scanf("%i", &x);
        struct btNode *newNode=createNode(x);
        insertNode(head,newNode);
        printf("%d",head->data); //breaks program here????

        i++;
    }

    while (x < 0){
        printf("Enter a integer from 0-5: ");
        scanf("%i",&x);

        if (x == 0){
            printf("Program Exit.\n");

            exit(0);

        }else if(x==1){


        }else if(x==2){


        }else if(x==3){

        }else if (x==4){

        }else if(x==5){

        }

        x=-1;
    }

    return 0;
}

//creates and returns a pointer to a new node
struct btNode* createNode(int x)
{
    struct btNode *newNode;
    newNode=(struct btNode*)malloc(sizeof(struct btNode));

    if (newNode == NULL){
        printf("Memory Allocation Failed./n");
        exit(20);
    }else{
        newNode->data=x;
        newNode->left=NULL;
        newNode->right=NULL;
        return newNode;
    }
}


void insertNode(struct btNode *tree, struct btNode *newNode){
    if (tree==NULL){
        tree=newNode;
        printf("%d",tree->data); //works fine here!
    }else if(tree->data <= newNode->data){
        insertNode(tree->right, newNode);
    }else if(tree->data > newNode->data){
        insertNode(tree->left, newNode);
    }
}
M.Rutan
  • 9
  • 1
  • You should test the return value from `scanf()`; your program won't stop if it gets EOF — or a non-digit (e.g. `a`) — in the first loop. Always test the result from `scanf()` etc. If you're expecting one value, test that it returns `1`; it might return `0` (indicating that what was entered wasn't a number) or `EOF`. – Jonathan Leffler Apr 23 '16 at 18:38
  • 1
    This question is effectively a duplicate of many others — lists and trees both run into the basic problem of 'how do I get the information back to the calling code'. – Jonathan Leffler Apr 23 '16 at 18:42

1 Answers1

1

You have to return the node after you insert it into your tree structure. So your correct function is:

struct btNode *insertNode(struct btNode *tree, struct btNode *newNode){
    if (tree==NULL){
        tree=newNode;
        printf("%d", tree->data); //works fine here!
        return tree;
    }else if(tree->data <= newNode->data){
        tree->right = insertNode(tree->right, newNode);
        return tree;
    }else if(tree->data > newNode->data){
        tree->left = insertNode(tree->left, newNode);
        return tree;      
    }
}

Also modify your call:

head = insertNode(head, newNode);
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Sumeet
  • 8,086
  • 3
  • 25
  • 45