title: Binary search tree deletion doesnot work in c
body: <pre><code>     void binarySearch(int);
       void createB(void);
       void binaryDel(int);
  
          int A[]={45,16,22,51,18,72,33,64,40};
          int B[32]={0};
          int i,j,x,y;
  
          int main(){
  
          scanf("%d",&amp;y);
          createB();
          binarySearch(y);
          binaryDel(y);
          return 0;
          }
  
  
  
          createB(){
          B[0]=A[0];
          for(i=1; i&lt;10; i++)
          {
              x=A[i];
              j=0;
              while(B[j]!=0)
              {
                  if (x&lt;B[j])
                  {
                      j=2*j+1;
                  }
                  else
                  {
                      j=2*j+2;
                  }
              }
              B[j]=x;
          }
      }}
  
  
          void binarySearch(int y){
          i=0;
  
            while((B[i]!=0)&amp;&amp;(y!=B[i])){
              if(y&lt;B[i]){
                  i=2i+1;
              }
              else{
                  i=2i+2;
              }
        }
  
      void binaryDel(int y){
          int k,l;
          binarySeerch(y);
          k=2*i+1;
          l=2*i+2;
  
      while(B[k]!=0){
              k=2*k+2;
          }
          k=(k-2)/2;
  
      while(B[l]!=0){
              l=2*l+1;
          }
  
          l=(l-1)/2;
          if((B[i]-B[k])&gt;B[l]-B[i]){
              B[i]=B[l];
              B[l]=0;
          }
          else{
           B[i]=B[k];
           B[k]=0;
  
          }
  </code></pre>
  
  <p>this code works sarch add and create. BUt as an argurment, i did not pass the array like <code>*B</code>, but i made global variable. IS that problem?</p>
  
  <p>Error is that, after a few deletions, it gives wrong. It doesnot delete, it moves without deleting.</p>
  
  <p>FOr example first i delete 16, it is correct.Then i say 51, it cant find that number in array. But it moves 40 and the root is 0. Very confusing.</p>
  
  <p>edit: real code. it is not in english. first press 1 then it will create binary, then press 4, and tell the number to delete:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  void binaryOlustur();
  void binaryArama(int);
  void binaryEkle(int);
  void binarySil(int);
  
  int A[]={45,16,22,51,18,72,33,64,40};
  int B[32]={0};
  int i,j,x;
  
  int main()
  {
      int y,menu;
  
  
      menu=0;
  
      while(menu!=5){
          printf("\n\n\n  Binary tree olusturma icin 1'e, \n  arama icin 2'ye,\n"
                   "  ekleme icin 3'e, \n  silme icin 4'e, \n  cikmak icin 5'e basin...\n");
          scanf("%d",&amp;menu);
  
          switch(menu){
      case 1:
      binaryOlustur();
          for(i=0;i&lt;32;i++){
          printf(" %d ",B[i]);
      }
      break;
  
      case 2:
          printf("\n Aramak istediginiz sayiyi giriniz...\n");
          scanf("%d",&amp;y);
          binaryArama(y);
            break;
  
      case 3:
          printf("\n Eklemek istediginiz sayiyi giriniz...\n");
          scanf("%d",&amp;y);
      binaryEkle(y);
         for(i=0;i&lt;32;i++){
          printf(" %d ",B[i]);
      }
        break;
  
      case 4:
          printf("\n Silmek istediginiz sayiyi giriniz...\n");
          scanf("%d",&amp;y);
      binarySil(y);
       for(i=0;i&lt;32;i++){
          printf(" %d ",B[i]);
      }
        break;
  
      case 5:
      exit(-1);
        break;
        }
      }
    return 0;
  }
  
  void binaryOlustur(void ){
  
     B[0]=A[0];
      for(i=1; i&lt;10; i++)
      {
          x=A[i];
          j=0;
          while(B[j]!=0)
          {
              if (x&lt;B[j])
              {
                  j=2*j+1;
              }
              else
              {
                  j=2*j+2;
              }
          }
          B[j]=x;
      }
  }
  
  
  void binaryArama(int y){
  i=0;
  while((B[i]!=0)&amp;&amp;(y!=B[i])){
      if(y&lt;B[i]){
          i=2*i+1;
      }
      else{
          i=2*i+2;
      }
  
  }
  if(y==B[i]){
  
      printf("\n Eleman %d. indiste bulundu...\n",i+1);
  }
  else{
      printf("\n Eleman bulunamadi...\n");
  }
        }
  
  
  
  void binaryEkle(int y){
      i=0;
  while(B[i]!=0){
      if(y&lt;B[i]){
          i=2*i+1;
      }
      else{
          i=2*i+2;
      }
  }
  B[i]=y;
  printf("\n Eleman %d. indise eklendi...\n",i+1);
  }
  
  
  void binarySil(int y){
      int k,l;
      binaryArama(y);
      k=2*i+1;
      l=2*i+2;
  
  while(B[k]!=0){
          k=2*k+2;
      }
      k=(k-2)/2;
  
  while(B[l]!=0){
          l=2*l+1;
      }
      l=(l-1)/2;
      if((B[i]-B[k])&gt;B[l]-B[i]){
          B[i]=B[l];
          B[l]=0;
      }
      else{
       B[i]=B[k];
       B[k]=0;
  
      }
  
  printf("\n %d silinip yerine %d tasindi...\n",y,B[i]);
  }
  </code></pre>