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.
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.
Contact Us