Search code examples
clinked-listbinary-treebinary-search-treedoubly-linked-list

Some of the elements are not getting added to the Binary Search Tree


I'm implementing a binary search tree with doubly-linked-list in C. I'm not getting expected output. Some of the elements are not getting added to the tree, Specifically some right childs.

I need help to fix this. Here is my code,

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

struct node *root=NULL;
struct node *temp=NULL;

struct node* createnode(int data){
    struct node *newNode= (struct node*)malloc(sizeof(struct node));
    newNode->data= data;
    newNode->left= NULL;
    newNode->right= NULL;

    return newNode;
}

void createBinaryTree(){
    int arr[100],size,i;
    printf("Enter no.of elements:");
    scanf("%d",&size);
    printf("\nEnter elements:");
    for(i=0;i<size;++i){
        scanf("%d",&arr[i]);
    }
    root= createnode(arr[0]);
    for(i=1;i<size;i++){
        temp=root;
        while(arr[i]>(temp->data)){
            if(temp->right==NULL){
                temp->right=createnode(arr[i]);
                continue;
            }
            temp=temp->right;
            
        }
        while(arr[i]<(temp->data)){
            if(temp->left==NULL){
                temp->left=createnode(arr[i]);
                continue;
            }
            temp=temp->left;
            
        }
    }
}

Solution

  • Your Code:

    struct node{
        int data;
        struct node *left;
        struct node *right;
    };
    
    struct node *root=NULL;
    struct node *temp=NULL;
    
    struct node* createnode(int data){
        struct node *newNode= (struct node*)malloc(sizeof(struct node));
        newNode->data= data;
        newNode->left= NULL;
        newNode->right= NULL;
    
        return newNode;
    }
    
    void createBinaryTree(){
        int arr[100],size,i;
        printf("Enter no.of elements:");
        scanf("%d",&size);
        printf("\nEnter elements:");
        for(i=0;i<size;++i){
            scanf("%d",&arr[i]);
        }
        root= createnode(arr[0]);
        for(i=1;i<size;i++){
            temp=root;
            while(arr[i]>(temp->data)){
                if(temp->right==NULL){
                    temp->right=createnode(arr[i]);
                    continue;
                }
                temp=temp->right;
                
            }
            while(arr[i]<(temp->data)){
                if(temp->left==NULL){
                    temp->left=createnode(arr[i]);
                    continue;
                }
                temp=temp->left;
                
            }
        }
    }
    

    The reason some elements are skipped is because you are adding the wrong way. Those 2 while loops are the problem.

    Lets say you have the tree:

    
         10
        /  \
       5   15
      / \    \
     2   7   17
    
    

    And you want to put the number 6.

    Your 1st while will be skipped.

    Then with your 2nd while you will go to the node with number 5.

    After that you will exit that while and nothing will happen.

    You will not reach that NULL.

    So to add nodes without a problem just use a while loop that will work as long as NULL hasn't been reached and an if statement inside that loop to check which path you will have to follow in each node.