Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define MAX_SIZE 100000
- int array_size;
- int lastin;
- typedef struct
- {
- int priority,priority2,value;
- } Node;
- void Swap(Node *a, Node *b);
- void enqueue(Node heap[],int priority,int value);
- Node dequeue(Node heap[]);
- void heapify(Node heap[]);
- void heapifyroot(Node heap[]);
- int main()
- {
- Node heap[MAX_SIZE];
- Node bla;
- initialize(heap);
- int i;
- enqueue(heap,10,15);
- enqueue(heap,11,12);
- enqueue(heap,19,11);
- enqueue(heap,4,0);
- enqueue(heap,9,1);
- enqueue(heap,25,5);
- enqueue(heap,4,7);
- enqueue(heap,30,1);
- enqueue(heap,30,2);
- enqueue(heap,25,8);
- enqueue(heap,4,18);
- for(i=0 ; i< array_size ; i++)
- {
- printf("%d\n",heap[i].priority);
- }
- printf("\n\n");
- while(array_size)
- {
- bla = dequeue(heap);
- printf("%d\t%d\n",bla.value,bla.priority);
- }
- return 0;
- }
- Node dequeue(Node heap[])
- {
- if(array_size == 0) //check if heap is empty
- {
- printf("heap is empty\n");
- return;
- }
- Node t = heap[0];
- Swap(&heap[0],&heap[array_size-1]);
- array_size = array_size-1;
- heapifyroot(heap);
- return t;
- }
- void heapifyroot(Node heap[])
- {
- int i = 0;
- while(i< (array_size)/2 && (heap[i].priority <= heap[2*i+1].priority || heap[i].priority <= heap[2*i+2].priority))
- {
- 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
- {
- 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
- {
- Swap(&heap[i],&heap[2*i+2]);
- i = 2*i+2;
- }
- else //the child has the same priority as its parent and the parent is older
- break;
- }
- else if(heap[2*i+1].priority > heap[2*i+2].priority) //if the left child's priority is higher
- {
- 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
- {
- Swap(&heap[i],&heap[2*i+1]);
- i = 2*i+1;
- }
- else //the child has the same priority as its parent and the parent is older
- break;
- }
- else if(heap[2*i+1].priority == heap[2*i+2].priority) //the two children have the same priority
- {
- if(heap[2*i+1].priority2 > heap[2*i+2].priority2 && array_size> 2*i+2) //the right child is older and exists
- {
- 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
- {
- Swap(&heap[i],&heap[2*i+2]);
- i = 2*i+2;
- }
- else //the child's priority is equal to parent's and the parent is older
- break;
- }
- else //the left child is older than the right
- {
- 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
- {
- Swap(&heap[i],&heap[2*i+1]);
- i = 2*i+1;
- }
- else //the child's priority is equal to parent's and the parent is older
- break;
- }
- }
- else
- break;
- }
- return;
- }
- void enqueue(Node heap[],int priority,int value)
- {
- if(array_size == MAX_SIZE) //if heap is full
- {
- printf("you reached max size");
- return;
- }
- heap[array_size].priority = priority;
- heap[array_size].value = value;
- heap[array_size].priority2 = ++lastin;
- heapify(heap);
- array_size++;
- return;
- }
- void heapify(Node heap[])
- {
- int j, i = array_size;
- Node temp;
- j = (i-1)/2;
- while (i > 0 && heap[j].priority < heap[i].priority) //if node if bigger than its parent
- {
- Swap(&heap[j],&heap[i]);
- i=j;
- j=(j-1)/2;
- }
- return;
- }
- void Swap(Node *a, Node *b )
- {
- Node t;
- t = *a;
- *a = *b;
- *b = t;
- return;
- }
- void initialize(Node heap[])
- {
- array_size = 0;
- lastin = 0;
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement