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

struct node
{
    int data;
    struct node *right, *left;
};
struct node *root = NULL;

int flag = 0;

void insert(int val){
    struct node *t, *p;
    t = (struct node*)malloc(sizeof(struct node));
    t->data = val;
    t->left = NULL;
    t->right = NULL;
    p = root;
    if( root == NULL ){
        root = t;
        return;
    }
    struct node *curn = root;

    while(curn){
        p = curn;
        if(t->data > curn->data){
            curn = curn->right;
        }
        else{
            curn = curn->left;
        }
    }
    if(t->data > p->data){
        p->right = t;
    }
    else{
        p->left = t;
    }
}
int search(int val){

    if(root->data == val){
        return 1;
    }
    struct node *curn;
    curn = root;
    while(curn){
        if(curn->data < val){
            curn = curn->right;
            if(curn->data == val){
                return 1;
            }
        }
        if(curn->data > val){
            curn = curn->left;
            if( curn->data == val ){
                return 1;
            }
        }
    }
    return 0;
}
int main(void){
    char opt[5] = "yes";
    int val, sear;
    while(1){
        printf("Enter the key number:\n");
        scanf("%d",&val);
        insert(val);
        printf("Do you want to create another junction?\n");
        scanf("%s",opt);        
        if(strcmp(opt,"yes") == 0){
            continue;
        }
        if(strcmp(opt, "no") == 0){
            printf("Enter the key to be searched\n");
            scanf("%d",&sear);
            flag = search(sear);
            if(flag == 1){
                printf("%d found",sear);
                return 0;
            }
                printf("%d not found",sear);
                return 0;
        }
    }

}

During search, if the search key is available, it shows key found, it doesn't throw any error.

But if search key is not present means it throws an error segmentation fault( core dumped ), Why this segmentation fault comes in my code?

Win Key
  • 49
  • 1
  • 7
  • Possible duplicate of [What is a segmentation fault?](https://stackoverflow.com/questions/2346806/what-is-a-segmentation-fault) – ventiseis Jul 20 '17 at 18:13

2 Answers2

1

Your code here:

if(curn->data < val){
    curn = curn->right;
    if(curn->data == val){
        return 1;
    }
}
if(curn->data > val){
    curn = curn->left;
    if( curn->data == val ){
        return 1;
    }
}

curn = curn->left; after this line, curn might be NULL, so curn->data will throw a segmentation fault. You want to check for curn->data == val in an if, else if control, like this:

if(curn->data < val){
    curn = curn->right;
}
else if(curn->data > val){
    curn = curn->left;
}
else {
    return 1;
}

No need to check for curn->data == val since if it's not less than or greater than, it has to be equal to.

Felix Guo
  • 2,700
  • 14
  • 20
0

In your while loop check if curn is not NULL if(!curn && curn->data < val)

N Tesfai
  • 21
  • 7