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

Introduction to AVL tree

AVL tree is a balanced binary search tree where the difference between heights of two sub trees is maximum 1. Why balanced tree A binary tree is good data structure because search operation here is of the order of O(logn). But this is true if the tree is balanced - which means the left and right subtrees are almost equal in height. If not balanced, search operation will take longer.  In worst case, if the tree has only one branch, then search is of the order O(n). Look at this example.  Here all nodes have only right children.  To search a value in this tree, we need upto 7 iterations, which is O(n). So this tree is very very inefficient. One way of making the tree efficient is to, balance the tree and make sure that height of two branches of each node are almost equal. Height of a node Height of a node is the distance between the node and its extreme child.  In the above a diagram, height of 37 is 3 and height of left child of 37 is 0 and right child of 37 is 2. Bal...

Reverse a singly linked list

One of the commonly used interview question is - how do you reverse a linked list? If you talk about a recursive function to print the list in reverse order, you are so wrong. The question is to reverse the nodes of list. Not print the nodes in reverse order. So how do you go about reversing the nodes. You need to take each node and link it to previous node. But a singly linked list does not have previous pointer. So if n1 is current node, n2 = n1->next, you should set     n2->next = NULL But doing this would cut off the list at n2. So the solution is recursion. That is to reverse n nodes  n1,n2,n3... of a list, reverse the sub list from n2,n3,n4.... link n2->next to n1 set n1->next to NULL The last step is necessary because, once we reverse the list, first node must become last node and should be pointing to NULL. But now the difficulty is regarding the head? Where is head and how do we set it? Once we reach end of list  viz n1->next ==NULL, th...

Balanced brackets

Have you observed something? When ever you are writing code using any IDE, if you write mismatched brackets, immediately an error is shown by IDE. So how does IDE  know if an expression is having balanced brackets? For that, the expression must have equal number of opening and closing brackets of matching types and also they must be in correct order. Let us look at some examples (a+b)*c+d*{e+(f*g)}   - balanced (p+q*[r+u )] - unbalanced (p+q+r+s) ) - unbalanced (m+n*[p+q]+{g+h}) - balanced So we do we write a program to check if an expression is having balanced brackets? We do need to make use of stack to store the brackets. The algorithm is as follows Scan a character - ch from the expression If the character is opening bracket, push it to stack If the character is closing bracket pop a character from stack If popped opening bracket and ch are not of same type ( ( and ) or [ and ] ) stop the function and return false Repeat steps 2 and 3 till all characters are scanned. On...