Skip to main content

Binary heap and heap sorting

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
In the diagram, a binary heap implemented using an array is shown. The indices are written next to each node.

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

  1. Build a max heap from the array elements
  2. Now we have max as root of array i.e. arr[0]/ Now swap root with the last element of the list
  3. Reduce size of list by 1
  4. Repeat steps 2 and 3 until list size is just one.
To understand how this works, let us look at this diagram.


Image courtesy : http://staff.ustc.edu.cn/



Now let us write the code.


 
#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

Popular posts from this blog

Program to create a Linked List in C

An array is a commonly used data structure in most of the languages. Because it is simple, it needs O(1) time for accessing elements. It is also compact. But an array has a serious drawback - it can not grow or shrink. You need to estimate the array size and define it during compile time. This drawback is not present a linked list. A linked list is a data structure which can grow or shrink dynamically.  A linked list has nodes each of which contain  contain  data and a link to next node . These nodes are dynamically allocated structures. If you need more nodes, you just need to allocate memory for these and link these nodes to the existing list. The nodes of a linked list have to be defined as self-referential structures in C. That is structures with data members and one member which is a pointer to the structure of same type.  This pointer will work as a link to next node. struct node { int data; struct node * next; //pointer to another node }...

Mirror tree

Question : Write a function to mirror a binary tree. The mirror tree will have a left subtree and right subtree swapped. But this has to be done for each and every node. So to convert a tree to its mirror, we can write an algorithm like this               1) mirror left subtree recursively               2) mirror right subtree recursively               3) swap left and right children of the current node. Here is the function for it. void mirror_tree (NODEPTR root) { if (root != NULL ) { NODEPTR temp = root -> left; root -> left = root -> right; root -> right = temp; mirror_tree(root -> left); mirror_tree(root -> right); } }

Josephus problem

Question: Write a function to delete every k th node from circular linked list until only one node is left. This has a story associated with it. Flavius Josephus was Jewish Historian from 1st century. He and 40 other soldiers were trapped in a cave by Romans. They decided to kill themselves rather than surrendering to Romans. Their method was like this. All the soldiers will stand in a circle and every k th soldier will be shot dead. Josephus said to have calculated the starting point so that he would remain alive. So we have similar problem at hand. We delete every kth node in a circular list. Eventually only one node will be left. e.g. Let us say this is our list And we are deleting every third node.  We will delete 30. Then we delete 60. Next we delete 10. Next it will be 50. Next to be deleted is 20. Next 80. This continues. Implementation   We can count k-1 nodes and delete next node. This can be repeated in  a loop. What must be the termina...