Last active
June 3, 2021 07:24
-
-
Save aatishnn/8265656 to your computer and use it in GitHub Desktop.
Priority Queue and Max-heap implementation in C (Inspired from https://gist.github.com/martinkunev/1365481)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Array representation according to Wikipedia article but starts from 0 index. | |
#include <stdio.h> | |
#include <stdlib.h> | |
struct heap { | |
int size; | |
int count; | |
int *heaparr; | |
}; | |
int *heap, size, count; | |
int initial_size = 4; | |
void heap_init(struct heap *h) | |
{ | |
h->count = 0; | |
h->size = initial_size; | |
h->heaparr = (int *) malloc(sizeof(int) * 4); | |
if(!h->heaparr) { | |
printf("Error allocatinga memory...\n"); | |
exit(-1); | |
} | |
} | |
void max_heapify(int *data, int loc, int count) { | |
int left, right, largest, temp; | |
left = 2*(loc) + 1; | |
right = left + 1; | |
largest = loc; | |
if (left <= count && data[left] > data[largest]) { | |
largest = left; | |
} | |
if (right <= count && data[right] > data[largest]) { | |
largest = right; | |
} | |
if(largest != loc) { | |
temp = data[loc]; | |
data[loc] = data[largest]; | |
data[largest] = temp; | |
max_heapify(data, largest, count); | |
} | |
} | |
void heap_push(struct heap *h, int value) | |
{ | |
int index, parent; | |
// Resize the heap if it is too small to hold all the data | |
if (h->count == h->size) | |
{ | |
h->size += 1; | |
h->heaparr = realloc(h->heaparr, sizeof(int) * h->size); | |
if (!h->heaparr) exit(-1); // Exit if the memory allocation fails | |
} | |
index = h->count++; // First insert at last of array | |
// Find out where to put the element and put it | |
for(;index; index = parent) | |
{ | |
parent = (index - 1) / 2; | |
if (h->heaparr[parent] >= value) break; | |
h->heaparr[index] = h->heaparr[parent]; | |
} | |
h->heaparr[index] = value; | |
} | |
void heap_display(struct heap *h) { | |
int i; | |
for(i=0; i<h->count; ++i) { | |
printf("|%d|", h->heaparr[i]); | |
} | |
printf("\n"); | |
} | |
int heap_delete(struct heap *h) | |
{ | |
int removed; | |
int temp = h->heaparr[--h->count]; | |
if ((h->count <= (h->size + 2)) && (h->size > initial_size)) | |
{ | |
h->size -= 1; | |
h->heaparr = realloc(h->heaparr, sizeof(int) * h->size); | |
if (!h->heaparr) exit(-1); // Exit if the memory allocation fails | |
} | |
removed = h->heaparr[0]; | |
h->heaparr[0] = temp; | |
max_heapify(h->heaparr, 0, h->count); | |
return removed; | |
} | |
int emptyPQ(struct heap *pq) { | |
int i; | |
while(pq->count != 0) { | |
printf("<<%d", heap_delete(pq)); | |
} | |
} | |
int main() { | |
struct heap h; | |
heap_init(&h); | |
heap_push(&h,1); | |
heap_push(&h,5); | |
heap_push(&h,3); | |
heap_push(&h,7); | |
heap_push(&h,9); | |
heap_push(&h,8); | |
heap_display(&h); | |
heap_display(&h); | |
emptyPQ(&h); | |
return 0; | |
} |
...
I just want to notice you that there is an unused variable i in emptyPQ~
In heap_init(), should we use the following?
h->heaparr = (int *) malloc(sizeof(int) * initial_size); // use the value of initial_size to control the array size during init
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
emptyPQ is incomplete, thanks tho!