Advertisement
ahmad_zizo

HEAP

Apr 5th, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.47 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define MAX_SIZE 100000
  4.  
  5. int array_size;
  6. int lastin;
  7.  
  8. typedef struct
  9. {
  10.     int priority,priority2,value;
  11. } Node;
  12.  
  13. void Swap(Node *a, Node *b);
  14. void enqueue(Node heap[],int priority,int value);
  15. Node dequeue(Node heap[]);
  16. void heapify(Node heap[]);
  17. void heapifyroot(Node heap[]);
  18.  
  19. int main()
  20. {
  21.     Node heap[MAX_SIZE];
  22.     Node bla;
  23.     initialize(heap);
  24.     int i;
  25.     enqueue(heap,10,15);
  26.     enqueue(heap,11,12);
  27.     enqueue(heap,19,11);
  28.     enqueue(heap,4,0);
  29.     enqueue(heap,9,1);
  30.     enqueue(heap,25,5);
  31.     enqueue(heap,4,7);
  32.     enqueue(heap,30,1);
  33.     enqueue(heap,30,2);
  34.     enqueue(heap,25,8);
  35.     enqueue(heap,4,18);
  36.  
  37.     for(i=0 ; i< array_size ; i++)
  38.     {
  39.         printf("%d\n",heap[i].priority);
  40.     }
  41.     printf("\n\n");
  42.     while(array_size)
  43.     {
  44.         bla = dequeue(heap);
  45.         printf("%d\t%d\n",bla.value,bla.priority);
  46.     }
  47.     return 0;
  48. }
  49. Node dequeue(Node heap[])
  50. {
  51.     if(array_size == 0) //check if heap is empty
  52.     {
  53.         printf("heap is empty\n");
  54.         return;
  55.     }
  56.     Node t = heap[0];
  57.     Swap(&heap[0],&heap[array_size-1]);
  58.     array_size = array_size-1;
  59.     heapifyroot(heap);
  60.     return t;
  61. }
  62.  
  63. void heapifyroot(Node heap[])
  64. {
  65.     int i = 0;
  66.     while(i< (array_size)/2 && (heap[i].priority <= heap[2*i+1].priority || heap[i].priority <= heap[2*i+2].priority))
  67.     {
  68.         if(heap[2*i+1].priority < heap[2*i+2].priority && array_size> 2*i+2) //check if the right child's priority is higher than left child & if the right child exists
  69.         {
  70.             if(heap[i].priority2 > heap[2*i+2].priority2 || heap[i].priority < heap[2*i+2].priority) //if the child is older than parent or its priority is higher
  71.             {
  72.                 Swap(&heap[i],&heap[2*i+2]);
  73.                 i = 2*i+2;
  74.             }
  75.             else  //the child has the same priority as its parent and the parent is older
  76.                 break;
  77.         }
  78.         else if(heap[2*i+1].priority > heap[2*i+2].priority) //if the left child's priority is higher
  79.         {
  80.             if(heap[i].priority2 > heap[2*i+1].priority2 || heap[i].priority < heap[2*i+1].priority) //if the child older than parent or its priority is higher
  81.             {
  82.                 Swap(&heap[i],&heap[2*i+1]);
  83.                 i = 2*i+1;
  84.             }
  85.             else   //the child has the same priority as its parent and the parent is older
  86.                 break;
  87.         }
  88.         else if(heap[2*i+1].priority == heap[2*i+2].priority) //the two children have the same priority
  89.         {
  90.  
  91.             if(heap[2*i+1].priority2 > heap[2*i+2].priority2 && array_size> 2*i+2) //the right child is older and exists
  92.             {
  93.                 if(heap[i].priority2 > heap[2*i+2].priority2 || heap[i].priority < heap[2*i+2].priority) //if the child is older than parent or its priority is higher
  94.                 {
  95.                     Swap(&heap[i],&heap[2*i+2]);
  96.                     i = 2*i+2;
  97.                 }
  98.                 else   //the child's priority is equal to parent's and the parent is older
  99.                     break;
  100.             }
  101.             else  //the left child is older than the right
  102.             {
  103.                 if(heap[i].priority2 > heap[2*i+1].priority2 || heap[i].priority < heap[2*i+1].priority) //if the child is older than parent or its priority is higher
  104.                 {
  105.                     Swap(&heap[i],&heap[2*i+1]);
  106.                     i = 2*i+1;
  107.                 }
  108.                 else   //the child's priority is equal to parent's and the parent is older
  109.                     break;
  110.             }
  111.         }
  112.         else
  113.             break;
  114.     }
  115.  
  116.     return;
  117. }
  118.  
  119. void enqueue(Node heap[],int priority,int value)
  120. {
  121.     if(array_size == MAX_SIZE) //if heap is full
  122.     {
  123.         printf("you reached max size");
  124.         return;
  125.     }
  126.     heap[array_size].priority = priority;
  127.     heap[array_size].value = value;
  128.     heap[array_size].priority2 = ++lastin;
  129.     heapify(heap);
  130.     array_size++;
  131.     return;
  132. }
  133.  
  134. void heapify(Node heap[])
  135. {
  136.     int j, i = array_size;
  137.     Node temp;
  138.     j = (i-1)/2;
  139.     while (i > 0 && heap[j].priority < heap[i].priority) //if node if bigger than its parent
  140.     {
  141.         Swap(&heap[j],&heap[i]);
  142.         i=j;
  143.         j=(j-1)/2;
  144.     }
  145.     return;
  146. }
  147.  
  148.  
  149. void Swap(Node *a, Node *b )
  150. {
  151.     Node t;
  152.     t = *a;
  153.     *a = *b;
  154.     *b = t;
  155.     return;
  156. }
  157.  
  158. void initialize(Node heap[])
  159. {
  160.     array_size = 0;
  161.     lastin = 0;
  162.     return;
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement