Heap Data Structure
Heap Data Structure
Heap data structure is a complete binary tree that satisfies the heap property, where
any given node is
always greater than its child node/s and the key of the root node is the largest among
all other nodes. This property is also called max heap property.
always smaller than the child node/s and the key of the root node is the smallest
among all other nodes. This property is also called min heap property.
Max-heap
Min-heap
Some of the important operations performed on a heap are described below along with
their algorithms.
Heapify
Heapify is the process of creating a heap data structure from a binary tree. It is used to
create a Min-Heap or a Max-Heap.
Initial Array
5. The index of left child is given by 2i + 1 and the right child is given by 2i + 2 .
If leftChild is greater than currentElement (i.e. element at ith index), set leftChildIndex as
largest.
6. Swap largest with currentElement
MaxHeap(array, size)
loop from the first index of non-leaf node down to zero
call heapify
For Min-Heap, both leftChild and rightChild must be larger than the parent for all
nodes.
Insert Element into Heap
Algorithm for insertion in Max Heap
If there is no node,
create a newNode.
else (a node is already present)
insert the newNode at the end (last node from left to right.)
For Min Heap, the above algorithm is modified so that parentNode is always smaller
than newNode .
For Min Heap, above algorithm is modified so that both childNodes are greater
smaller than currentNode .
return rootNode
Extract-Max/Min
Extract-Max returns the node with maximum value after removing it from a Max
Heap whereas Extract-Min returns the node with minimum after removing it from
Min Heap.
Example-
Suppose we want to create the max heap tree. To create the max heap tree, we need to
consider the following two cases:
o First, we have to insert the element in such a way that the property of the
complete binary tree must be maintained.
o Secondly, the value of the parent node should be greater than the either of its
child.
Step 2: The next element is 33. As we know that insertion in the binary tree always starts
from the left side so 44 will be added at the left of 33 as shown below:
Step 3: The next element is 77 and it will be added to the right of the 44 as shown
below:
As we can observe in the above tree that it does not satisfy the max heap property, i.e.,
parent node 44 is less than the child 77. So, we will swap these two values as shown
below:
Step 4: The next element is 11. The node 11 is added to the left of 33 as shown below:
Step 5: The next element is 55. To make it a complete binary tree, we will add the node
55 to the right of 33 as shown below:
As we can observe in the above figure that it does not satisfy the property of the max
heap because 33<55, so we will swap these two values as shown below:
Step 6: The next element is 88. The left subtree is completed so we will add 88 to the
left of 44 as shown below:
As we can observe in the above figure that it does not satisfy the property of the max
heap because 44<88, so we will swap these two values