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.

`#include<stdio.h>`

`#include<limits.h>`

`/*Declaring heap globally so that we do not need to pass it as an argument every time*/`

`/* Heap implemented here is Min Heap */`

int heap[1000000], heapSize;

`/*Initialize Heap*/`

void Init() {

heapSize = 0;

heap[0] = -INT_MAX;

`}`

`/*Insert an element into the heap */`

void Insert(int element) {

`heapSize++;`

heap[heapSize] = element; /*Insert in the last place*/

`/*Adjust its position*/`

int now = heapSize;

while (heap[now / 2] > element) {

heap[now] = heap[now / 2];

now /= 2;

`}`

heap[now] = element;

`}`

int DeleteMin() {

`/* heap[1] is the minimum element. So we remove heap[1]. Size of the heap is decreased.`

`Now heap[1] has to be filled. We put the last element in its place and see if it fits.`

`If it does not fit, take minimum element among both its children and replaces parent with it.`

`Again See if the last element fits in that place.*/`

int minElement, lastElement, child, now;

minElement = heap[1];

lastElement = heap[heapSize--];

`/* now refers to the index at which we are now */`

for (now = 1; now * 2 <= heapSize; now = child) {

`/* child is the index of the element which is minimum among both the children */`

`/* Indexes of children are i*2 and i*2 + 1*/`

child = now * 2;

`/*child!=heapSize beacuse heap[heapSize+1] does not exist, which means it has only one`

`child */`

if (child != heapSize && heap[child + 1] < heap[child]) {

`child++;`

`}`

`/* To check if the last element fits ot not it suffices to check if the last element`

`is less than the minimum element among both the children*/`

if (lastElement > heap[child]) {

heap[now] = heap[child];

} else /* It fits there */

`{`

break;

`}`

`}`

heap[now] = lastElement;

return minElement;

`}`

int main() {

int number_of_elements;

printf("Program to demonstrate Heap:\nEnter the number of elements: ");

scanf("%d", &number_of_elements);

int iter, element;

Init();

printf("Enter the elements: ");

for (iter = 0; iter < number_of_elements; iter++) {

scanf("%d", &element);

Insert(element);

`}`

for (iter = 0; iter < number_of_elements; iter++) {

printf("%d ", DeleteMin());

`}`

printf("\n");

return 0;

`}`

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.

**Next Steps:**

- Get Free Certificate of Merit in Data Structure I
- Participate in Data Structure I Certification Contest
- Become a Top Ranker in Data Structure I
- Take Data Structure I Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

**Related Posts:**

- Buy Computer Science Books
- Buy Data Structure Books
- Practice Programming MCQs
- Apply for Information Technology Internship
- Buy Programming Books