C Program to Implement Heap

This is a C Program to implement Heap. A heap data structure is a tree-based data structure that satisfies a property called heap property. If A is a parent node of B then the key of node A is ordered with respect to the key of node B with the same ordering applying across the heap. Either the keys of parent nodes are always greater than or equal to those of the children and the highest key is in the root node (this kind of heap is called max heap) or the keys of parent nodes are less than or equal to those of the children and the lowest key is in the root node (min heap).

A common implementation of a heap is the binary heap, in which the tree is a complete binary tree.

Here is source code of the C Program to Implement Heap. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include<stdio.h>
  2. #include<limits.h>
  3.  
  4. /*Declaring heap globally so that we do not need to pass it as an argument every time*/
  5. /* Heap implemented  here is Min Heap */
  6.  
  7. int heap[1000000], heapSize;
  8. /*Initialize Heap*/
  9. void Init() {
  10.     heapSize = 0;
  11.     heap[0] = -INT_MAX;
  12. }
  13.  
  14. /*Insert an element into the heap */
  15. void Insert(int element) {
  16.     heapSize++;
  17.     heap[heapSize] = element; /*Insert in the last place*/
  18.     /*Adjust its position*/
  19.     int now = heapSize;
  20.     while (heap[now / 2] > element) {
  21.         heap[now] = heap[now / 2];
  22.         now /= 2;
  23.     }
  24.     heap[now] = element;
  25. }
  26.  
  27. int DeleteMin() {
  28.     /* heap[1] is the minimum element. So we remove heap[1]. Size of the heap is decreased.
  29.      Now heap[1] has to be filled. We put the last element in its place and see if it fits.
  30.      If it does not fit, take minimum element among both its children and replaces parent with it.
  31.      Again See if the last element fits in that place.*/
  32.     int minElement, lastElement, child, now;
  33.     minElement = heap[1];
  34.     lastElement = heap[heapSize--];
  35.     /* now refers to the index at which we are now */
  36.     for (now = 1; now * 2 <= heapSize; now = child) {
  37.         /* child is the index of the element which is minimum among both the children */
  38.         /* Indexes of children are i*2 and i*2 + 1*/
  39.         child = now * 2;
  40.         /*child!=heapSize beacuse heap[heapSize+1] does not exist, which means it has only one
  41.          child */
  42.         if (child != heapSize && heap[child + 1] < heap[child]) {
  43.             child++;
  44.         }
  45.         /* To check if the last element fits ot not it suffices to check if the last element
  46.          is less than the minimum element among both the children*/
  47.         if (lastElement > heap[child]) {
  48.             heap[now] = heap[child];
  49.         } else /* It fits there */
  50.         {
  51.             break;
  52.         }
  53.     }
  54.     heap[now] = lastElement;
  55.     return minElement;
  56. }
  57.  
  58. int main() {
  59.     int number_of_elements;
  60.     printf("Program to demonstrate Heap:\nEnter the number of elements: ");
  61.     scanf("%d", &number_of_elements);
  62.     int iter, element;
  63.     Init();
  64.     printf("Enter the elements: ");
  65.     for (iter = 0; iter < number_of_elements; iter++) {
  66.         scanf("%d", &element);
  67.         Insert(element);
  68.     }
  69.     for (iter = 0; iter < number_of_elements; iter++) {
  70.         printf("%d ", DeleteMin());
  71.     }
  72.     printf("\n");
  73.     return 0;
  74. }

Output:

$ gcc Heap.c
$ ./a.out
 
5
 
Program to demonstrate Heap
Enter the number of elements: 5
Enter the elements: 645 897 612 849 643
Elements deleted in a sequence: 612 643 645 849 897

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.