简体   繁体   English

在c中的二叉搜索树中删除

[英]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. 我编写了一个程序,该程序将使用两个参数f1和f2,两个文件都带有数字。该程序应可按如下方式调用:树f1 f2 f1具有数百万个唯一数字,每行一个。 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;
              }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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