Binary Search Tree implementation.

Discussion in 'C Programming' started by jhava, Aug 5, 2008.

  1. jhava

    jhava

    Joined:
    Aug 5, 2008
    Messages:
    1
    I am trying to write a implmentation of binary search tree(BST) in C.
    * The implmentation takes several integers from user
    * Add the integers to the BST
    * Any duplicate number is discarded

    I implimented the program using linked list but i am getting segmentation fault instead.
    I tried inserting various printf for debugging but i am unable to find the exact error.
    It seems that the error is in the insert2 function.

    Here is the implementation:-

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


    /*----------Type definations-----------*/
    typedef struct node{
    int data; //Data
    struct node * left; //Left Node
    struct node * right; //Right Node
    }node;



    /*---------Function Prototypes----------*/
    void insert2(node *, node *); //insert node in tree
    node * new_Node();



    /*---------Main Function---------------*/
    int main()
    {
    int n;
    int i;
    int temp;
    node * temp_n;
    node * root;
    root == NULL;

    printf("Enter number of inputs to be given:");
    scanf("%u", &n);

    //Take input from user and insert it in the BST

    for(i = 0; i < n; i++){
    printf("?");
    scanf("%d", &temp);
    /*--Debug Code1--*/
    // printf("%d", temp);

    temp_n = new_Node();
    //printf("Temp_Node testing\n");
    temp_n->data = temp;

    //DEBUG Code2
    //printf("%d\n", temp_n);
    printf("Temp_n data %d\n", temp_n->data);

    //DEBUG CODE
    //printf("%d %d\n", &int_bst, sizeof(int_bst));
    printf("Calling insert2");

    insert2(root, temp_n);
    }
    }


    /*--------Function Declaration----------*/
    node * new_Node(){
    node * temp = (node *) malloc(sizeof(node));

    temp->data = 0;
    temp->left = NULL;
    temp->right = NULL;

    return temp;
    }



    void insert2(node * r, node * c)
    {
    //DEBUG CODE
    printf("In insert2");

    if(r == NULL){
    r = new_Node(); //BASE Condition
    r = c;
    }
    else{
    if(c->data == r->data){
    //DEBUG CODE
    printf("DUPLICATION DETECTED\n");
    }
    else if(c->data < r->data){
    insert2(r->left, c);
    }
    else if(c->data > r->data){
    insert2(r->right, c);
    }
    }
    }

    -----------------------

    Sample Output:-

    Enter number of inputs to be given:3
    ?2
    Temp_n data 2
    Segmentation fault

    ------------------------

    Please help me to find the actual bug in my implementation.
     
    jhava, Aug 5, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stub

    B tree, B+ tree and B* tree

    Stub, Nov 12, 2003, in forum: C Programming
    Replies:
    3
    Views:
    10,133
  2. sharan
    Replies:
    4
    Views:
    693
    CBFalconer
    Oct 30, 2007
  3. sharan
    Replies:
    2
    Views:
    835
    SM Ryan
    Oct 31, 2007
  4. sharan
    Replies:
    1
    Views:
    693
    CBFalconer
    Oct 30, 2007
  5. Bogdan

    Binary tree search vs Binary search

    Bogdan, Oct 18, 2010, in forum: C Programming
    Replies:
    22
    Views:
    3,086
    Michael Angelo Ravera
    Oct 21, 2010
Loading...

Share This Page