title: Delete max heap method
body: <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  #define n 10
  
  void swap(int * a , int * b);
  
  int parent(int i){
      return  ((i+1)/2)-1;
  }
  
  
  int left(int i){
      return  2*(i+1)-1;
  }
  
  
  int right(int i){
      return  (2*(i+1)+1)-1;
  }
  
  int printHeap(int *heap, int pos){
   int i;
   for (i=0;i&lt;pos;i++){
       printf("%d ",heap[i]);
       }
       printf("----\n");   
  }
  
  int insert(int *heap,int *pos, int value)
  {
      int i=*pos;
      int tmp;
      heap[i]=value;
      (*pos)++;
      while(parent(i)&gt;=0) {
         if(heap[parent(i)]&lt;heap[i]){
            tmp=heap[parent(i)];
            heap[parent(i)]=heap[i];
            heap[i]=tmp;                                          
         }else{
               break;     
         }                    
         i=parent(i);
      } 
  
  }
  
  void deleteMax(int *heap,int *pos)
  {
      int tmp;
      heap[0]=heap[*pos-1];
      (*pos)--;
      maxHeapify(heap,&amp;pos);
  }
  
  void maxHeapify(int *heap,int *pos)
  {
      int i=*pos,l=left(i),r=right(i),max;
      if (l&lt;i &amp;&amp; heap[l]&gt;heap[i])
          max=l;
      else
          max=i;
  
      if(r&lt;i &amp;&amp; heap[r]&gt;heap[max])
          max=r;
  
      if (max!=i)
      {
          swap(&amp;heap[i],&amp;heap[max]);
          maxHeapify(heap,max);
      }
  }
  
  void swap(int * a , int * b)
  {
      int tmp;
      tmp=*a;
      *a=*b;
      *b=tmp;
  }
  
  int main()
  {
      int heap[n];
      int pos=0;
      insert(heap,&amp;pos, 10);
      insert(heap,&amp;pos, 5);
      insert(heap,&amp;pos, 15);
      insert(heap,&amp;pos, 35);
      insert(heap,&amp;pos, 66);
      insert(heap,&amp;pos, 41);
      insert(heap,&amp;pos, 42);
      insert(heap,&amp;pos, 21);
      insert(heap,&amp;pos, 14);
      insert(heap,&amp;pos, 3);
      printHeap(heap,pos);
      deleteMax(heap,&amp;pos);
      printHeap(heap,pos);
      system("PAUSE");
      return 0;
  }
  </code></pre>
  
  <p>I have this heap implementation and I want to delete maximum heap (heap[0]) and then fix down the whole array , first I replace the max heap with the last element (min) and then I try to fix the structure with the maxHeapify method but I it seems something is wrong , any idea? thank you in advance!</p>