Heap
Heap is a special type binary tree where every node P is larger than its parent nodes. This is called max heap. In min heap, every node is smaller than its parent
In case of max heap, the largest element is always available at the root of the tree. In min heap, the smallest element is at the root of the tree.
A heap which is a complete binary tree is called binary heap. A complete binary tree is the one where each level of the tree is completely filled except for last level, which is filled from left to right.
Array implementation of binary heap
Because binary heap is a complete binary tree, it will be efficient store its elements in an array.
Such an array will have index of parent and its children as follows.
- For every node with index i, the left child is at index 2i+1 and right child is at index 2i+2
Heapify
The process of rearranging the elements to maintain the heap property is called heapify.
- Compare key of parent node with larger child
- If key of parent is smaller than larger of child nodes, swap parent with child node
- Recursively call heapify on the swapped child node because this node may not maintain heap property now.
//code from geeksforgeeks
void heapify(int arr[], int n, int i)
{
int largest = i; // Initialize largest as root
int left = 2*i + 1; // left = 2*i + 1
int right = 2*i + 2; // right = 2*i + 2
// If left child is larger than root
if (left < n && arr[left] > arr[largest])
largest = left;
// If right child is larger than largest so far
if (right < n && arr[right] > arr[largest])
largest = right;
// If largest is not root
if (largest != i)
{
swap(&arr[i], &arr[largest]);
// Recursively heapify the affected sub-tree
heapify(arr, n, largest);
}
}
Building a heap
To build a heap we need to compare the nodes with its children and if the children are larger, swap the parent node with larger child. And a parent can have heap property only if the children have heap property. So the process has to be done from the last parent node( i.e. index n/2 -1 ) to 0th node - root.
int i;
// Build heap (rearrange array)
for ( i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
printf("Now the heap is ");
printArray(arr,n);
Applications of heap
A heap is used in the following algorithms along with many more
- Heap sort
- Graph algorithm like Prim's minimal-spanning-tree algorithm and Dijkstra's shortest-path algorithm.
- Priority Queue
Heap sort using binary Heap
Heap sort is slower than well implemented quick sort but has a worst case run time of O(nlogn). And it is in place algorithm - which means you need not copy the elements to another location, thus requiring twice as much space.
Heap sort finds the largest element and places this at the end of array. Then it finds largest among the remaining elements and places this at end of array (in this case n-1th location). So it is similar to selection sort. But instead of comparing the elements to find largest, heap sort uses heapify to build heap and then just extracts root to get largest element
So let us write the algorithm
- Build a max heap from the array elements
- Now we have max as root of array i.e. arr[0]/ Now swap root with the last element of the list
- Reduce size of list by 1
- Repeat steps 2 and 3 until list size is just one.
#include <stdio.h>
void printArray(int *arr,int n);
void swap(int *a,int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
/* convert node at index i into a max heap */
void heapify(int arr[], int n, int i)
{
int largest = i;
int left = 2*i + 1; // left = 2*i + 1
int right = 2*i + 2; // right = 2*i + 2
if (left < n && arr[left] > arr[largest])
largest = left;
if (right < n && arr[right] > arr[largest])
largest = right;
if (largest != i) {
swap(&arr[i], &arr[largest]);
heapify(arr, n, largest);//now node at largest is out of order. heapify it
}
}
void heapSort(int arr[], int n)
{
int i;
// Build heap from array elements
for ( i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
//Extract 0th element (root) and move it at the end of array. Reduce the size of heap
for ( i=n-1; i>=0; i--)
{
// Move current root to end
swap(&arr[0], &arr[i]);
// call max heapify on the reduced heap
heapify(arr, i, 0);
}
}
void readArray(int arr[], int n)
{
int i;
for ( i=0; i<n; ++i)
{
printf("a[%d]",i);
scanf("%d",&arr[i]);
}
}
void printArray(int arr[], int n)
{
int i;
for ( i=0; i<n; ++i)
printf("%d ",arr[i]);
printf( "\n");
}
// Driver program
int main()
{
int arr[40];int n;
printf("What is array size");
scanf("%d",&n);
readArray(arr,n);
heapSort(arr, n);
printf("Sorted array is \n");
printArray(arr, n);
}
Comments
Post a Comment