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

In order traversal of nodes in the range x to y

Question : Write a function for in-order traversal of nodes in the range x to y from a binary search tree. This is quite a simple function. As a first solution we can just traverse our binary search tree in inorder and display only the nodes which are in the range x to y. But if the current node has a value less than x, do we have to traverse its left subtree? No. Because all the nodes in left subtree will be smaller than x. Similarly if the current node has a key value more than y, we need not visit its right subtree. Now we are ready to write our algorithm.     if nd is NOT NULL  if nd->val >=x then visit all the nodes of left subtree of nd recursively display nd->val if nd->val <y then visit all the nodes of right subtree of nd recursively  That's all. We have our function ready. void in_order_middle (NODEPTR nd, int x, int y) { if (nd) { if (nd -> val >= x) in_order_middle(nd...

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

Lowest common ancestor of binary search tree

Question : Write a function to print the lowest common ancestor of two nodes in a binary search tree.  Lowest common ancestor of two nodes x and y in a binary tree is the lowest node that has both x and y as descendants. Here lowest common ancestor of 1 and 7 is 3. LCA of 13 and 7 is root - 8. And LCA of 6 and 7 is 6 itself. The program to find gets complicated for an ordinary binary tree. But for a binary search tree, it is quite simple. As we see from the diagram above, the paths to 1 and 4 are common till the node 3. And at 3 they branch in different directions. So 3 is our LCA. That is lowest common ancestor is the node where the paths to x and y from root deviate. As long as they branch in same direction, we continue to traverse. When they branch in different directions, that is the lowest common ancestor. So let us write a simple algorithm, set temp=root if temp->val >x and temp->val>y temp = temp->left else if temp->val<x and ...