簡體   English   中英

在c中的二叉搜索樹中刪除

[英]Deletion in a Binary Search tree in c

我編寫了一個程序,該程序將使用兩個參數f1和f2,兩個文件都帶有數字。該程序應可按如下方式調用:樹f1 f2 f1具有數百萬個唯一數字,每行一個。 每個數字都應讀取並插入二叉樹中。 插入這些文件后,程序應從第二個文件中讀取數字。 對於每個數字,必須執行以下操作-在樹中搜索該數字-如果該數字存在,則將其從樹中刪除

現在,我用於插入和搜索的代碼給出了正確的結果,但是在刪除的一部分中存在一些錯誤。 請通過修改代碼來幫助我。

#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);

         }

沒有休假時不支持情況。 在比賽開始時也要處理:

              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