简体   繁体   中英

Deletion in a Binary Search tree in c

i have written a program that will take two arguments f1 and f2, both files with numbers.the program should callable as follows tree f1 f2 f1 has millions of unique numbers, one per line. Each number should be read and inserted in a binary tree. after these are inserted, the program should read numbers from the second file. For each number, the following has to be done - search for the number in the tree - if it exists, delete it from the tree

Now my code for insertion and searching is giving correct results but in part of deletion there is some error. please help me out by modifyng my code.

#include<stdio.h>
#include<stdlib.h>
struct node
{
   int info;
   struct node *left, *right;
};
struct node* insert(struct node* root, int item)
{
     struct node *temp,*temp1,*pre;
     temp =  (struct node *)malloc(sizeof(struct node));
     temp->info = item;
     temp->left = temp->right = NULL;
     if (root == NULL)
            root=temp;

     else
     {
           temp1=root;
           while(temp1!=NULL)
           {
                   pre=temp1;
                   if(item<temp1->info)
                       temp1=temp1->left;
                   else
                       temp1=temp1->right;

          }
          if(item<pre->info)
              pre->left=temp;
          else
              pre->right=temp;
       }            
       return root;

    }

    struct node *create(struct node *root)
    {
           int num; 
           root=NULL;
           FILE *fp1=fopen("myFile1.txt","r");
           if (fp1 == NULL)
           {
                printf("cannot open this file");
                exit(0);
           }
           while(fscanf(fp1,"%d",&num)==1)
               root=insert(root,num);

        return root;
        fclose(fp1);
       }
       struct node * min(struct node* ptr)
       {
                struct node* current = ptr;
                while (current->left != NULL)
                current = current->left;

                return current;
        }
        struct node* delete(struct node* root,int n)
        {
           if(root==NULL)
               return root;
           if(n<root->info)
               root->left=delete(root->left,n);
           else if(n>root->info)
               root->right=delete(root->right,n);
           else
           {
                  if(root->left==NULL)
                  {
                       struct node *p;
                       p=root->right;
                       free(root);
                       return p;
                  }
                  else
                  if(root->right==NULL)
                  {
                        struct node *p;
                        p=root->left;
                        free(root);
                        return p;
                  }

                  struct node *p;
                   p=min(root->right);
                   root->info=p->info;
                   root->right=delete(root->right,p->info);
           }

           return root;
        }        

        void search(struct node *root)
        {
              int Y,X;
              struct node *t;
              t=root;
              char ch='n';
              FILE *fp2=fopen("myFile2.txt","r");
              if (fp2 == NULL)
              {
                  printf("cannot open this file");
                  exit(0);
              } 
              X=0;  
              while(fscanf(fp2,"%d",&Y)==1)
              {
                    while(t!=NULL && X==0)
                    {                       
                       if(Y==t->info)
                       {
                             X=1;
                             break;
                       }
                       else
                         if(Y<t->info)
                             t=t->left;
                         else
                             t=t->right;
                 }

                 if(X==1)
                    printf(" %d is found %d\n",Y,X);
                    printf("if you want to delete a number ");
                    scanf("%c",&ch);
                    if(ch=='y')
                    {
                         root=delete(root,Y);
                         return root;

                    }

                    else
                        printf("%dNot found %d\n",Y,X);

             }


             fclose(fp2);   

         }

         void inorder(struct node *root)
          {
                  if (root != NULL)
                  {
                       inorder(root->left);
                       printf("%d ", root->info);
                       inorder(root->right);
                  }
          }




         void main()
         {
               struct node *root = NULL;
               struct node *ptr = NULL;
               root=create(root);
               inorder(root);
               search(root);
               inorder(root);

         }

It doesn't support case when there isn't any leave. Handle this too at the beginning of match:

              if(root->rigt == NULL && root->left==NULL)
              {
                   free(root);
                   return NULL;
              }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM