What is Max Heap?

The max-heap is the heap where each and every node of a complete binary tree is greater than or equal to its child node. We can see the example of a max-heap in the below image.

Max Heap in C

The above heap can be represented in an array as given below.

arr[] = {17,15,10,6,10,7};

We denote,

  • parent(i)=(i-1)/2” for the parent node.
  • “left_child(i)=2*i+1” for the left child.
  • right_child(i)=2*i+2” for the right child.

Here, i is the index of the current node in the array.

For example, To find a parent of the 4th element of the array given below we use parent(4)=(4-1)/2=1th node or element at the index at 1 in an array. We are going to use these three formulas in the implementation of the min heap.

C Program to Implement Max Heap

In this article, we will learn the implementation of the max heap in the C programming language.

A heap is a data structure like a tree with some special properties. The basic requirement of the heap is that the value of a node must be greater than equal to (or smaller than equal to) the value of its children and the tree should be a complete binary tree.

Similar Reads

What is Max Heap?

The max-heap is the heap where each and every node of a complete binary tree is greater than or equal to its child node. We can see the example of a max-heap in the below image....

Basic Operations on Min Heap

Following are the basic operations that are performed on the min heap:...

Implementation of the Max Heap in C

To implement the max heap, we will use the structures in C to implement the heap data structure to write the implementation of the max heap to maintain the property of the max heap we have used the bottom-up and top-down approaches both. Let’s understand some important functions of the max heap program....