Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Get Root Node Deleted Properly in Binary Search Tree

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 458
    Answer it

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

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

    int num=0,num1;

    void insert();
    struct node *del(struct node *,int);
    void display(struct node *);
    struct node *max(struct node *);


    void main()
    {
      int ch;

       do
        {

          printf("\n1.insert\n2.delete\n3.display\n4.exit");
          scanf("%d",&ch);

          switch(ch)
         {
          
          case 1:
          insert();
          break;

          case 2:
          printf("\nenter the number that you want to delete:-");
          scanf("%d",&num);
          del(root,num);
          break;

          case 3:
          display(root);
          break;

        

          case 4:
          exit(0);
          break;
         }
     
       }while(ch!=4);

    }


     void insert()
    {

        printf("\nenter the value:-");
        scanf("%d",&num);
     
       new=(struct node *)malloc(sizeof(struct node));
       
       new->right=NULL;
       new->data=num;
       new->left=NULL;
        
     
        if(root==NULL)
         root=new;

        else
        {
                temp=root;
        while(temp!=NULL)
         {
             if(temp->data<num)
           {
                if(temp->right==NULL)
                 {  temp->right=new;  return;}

            else    
            temp=temp->right;
             }

             else if(temp->data>=num)
           {

            if(temp->left==NULL)
            {temp->left=new; return;}

            else
            temp=temp->left;
            
           }
         }

        }
    }
                                                                                                                                                                                                                                                                                                                                                                 struct node *del(struct node *root,int num)
    {
            
            struct node *temp1;
            if(root==NULL)
              return root;        
            

            else if(root->data<num)
            {     
                    root->right=del(root->right,num);
            }

            else if(root->data>num)
            {root->left=del(root->left,num);}

              else if(root->data==num)
             {        
                if(root->right==NULL&&root->left==NULL)
                     {
                    
                    free(root); root=NULL; return NULL;;
                   
                    
                }
                if(root->right==NULL&&root->left!=NULL)

                     {
                    temp1=root;
                    root=root->left;
                    free(temp1);
                }
                if(root->left==NULL&&root->right!=NULL)
                     {
                    temp1=root;
                    root=root->right;
                    free(temp1);
                }

                if(root->left!=NULL&&root->right!=NULL)
                     {
                    temp1=max(root->left);                
                    root->data=temp1->data;
                    root->left=del(root->left,root->data);
                }
                
            }        
       return root;
     }     
        
     void display(struct node *root)
    {

          if(root==NULL)
            return;
         else{
                
            printf("%d",root->data);
            display(root->left);
               display(root->right);
        
              }
    }

    struct node *max(struct node *temp)
    {
            if(temp->right==NULL&&temp->left==NULL)
                return temp;
        
            else
        temp=max(temp->right);

        return temp;
    }
     

    deletion of node code is not working properly. root node isn't delete properly.

 0 Answer(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: