C Program to Implement Binary Heap

This is a C program to implement the binary heap. It constructs a maximum binary heap of given array elements. Maximum binary heap is one in which the every child node has value less than the value of the parent.

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

  1. /* C program to build a binary heap */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #define MAX 20
  5. void maxheapify(int *, int, int);
  6. int* buildmaxheap(int *, int);
  7. void main()
  8. {
  9.     int i, t, n;
  10. 	int *a = calloc(MAX, sizeof(int));
  11.     int *m = calloc(MAX, sizeof(int));
  12.     printf("Enter no of elements in the array\n");
  13.     scanf("%d", &n);
  14.     printf("Enter the array\n");
  15.     for (i = 0; i < n; i++) {
  16.         scanf("%d", &a[i]);
  17.     }
  18.     m = buildmaxheap(a, n);
  19.     printf("The heap is\n");
  20.     for (t = 0; t < n; t++) {
  21.         printf("%d\n", m[t]);
  22.     }
  23. }
  24. int* buildmaxheap(int a[], int n)
  25. {
  26.     int heapsize = n;
  27.     int j;
  28.     for (j = n/2; j >= 0; j--) {
  29.         maxheapify(a, j, heapsize);
  30.     }
  31.     return a;
  32. }
  33. void maxheapify(int a[], int i, int heapsize)
  34. {
  35.     int temp, largest, left, right, k;
  36.     left = (2*i+1);
  37.     right = ((2*i)+2);
  38.     if (left >= heapsize)
  39.         return;
  40.     else {
  41.         if (left < (heapsize) && a[left] > a[i]) 
  42.             largest = left;
  43.         else
  44.             largest = i;
  45.         if (right < (heapsize) && a[right] > a[largest])  
  46.             largest = right;
  47.         if (largest != i) {
  48.             temp = a[i];
  49.             a[i] = a[largest];
  50.             a[largest] = temp;
  51.             maxheapify(a, largest, heapsize);
  52.         }
  53.     }
  54. }

Output

Enter no of elements in the array
5
Enter the array
7
5
9
3
2
The heap is :
9
5
7
3
2

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

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

If you find any mistake above, kindly email to [email protected]

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.