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

Linked list in C++

A linked list is a versatile data structure. In this structure, values are linked to one another with the help of addresses. I have written in an earlier post about how to create a linked list in C.  C++ has a library - standard template library which has list, stack, queue etc. data structures. But if you were to implement these data structures yourself in C++, how will you implement? If you just use new, delete, cout and cin, and then claim it is your c++ program, you are not conforming to OOPS concept. Remember you have to "keep it together". Keep all the functions and variables together - in a class. You have to have class called linked list in which there are methods - append, delete, display, insert, find, find_last. And there will also be a data - head. Defining node We need a structure for all these nodes. A struct can be used for this purpose, just like C. struct node { int val; struct node * next; }; Next we need to define our class. W

Swap nodes of a linked list

Qn: Write a function to swap the adjacent nodes of a singly linked list.i.e. If the list has nodes as 1,2,3,4,5,6,7,8, after swapping, the list should be 2,1,4,3,6,5,8,7 Image from: https://tekmarathon.com Though the question looks simple enough, it is tricky because you don't just swap the pointers. You need to take care of links as well. So let us try to understand how to go about it. Take two adjacent nodes p1 and p2 Let prevnode be previous node of p1 Now link prevnode to p2 Link p2 to p1 Link p1 to next node of p2 So the code will be prevnode -> next = p2; p1 -> next = p2 -> next; p2 -> next = p1; But what about the start node or head? head node does not have previous node If we swap head with second node, modified head should be sent back to caller  To take care of swapping first and second nodes, we can write p1 = head; p2 = head -> next; p1 -> next = p2 -> next; p2 -> next = p1; head = p2;  Now we are read

Binary tree deletion - non-recursive

In the previous post we have seen how to delete a node of a binary search tree using recursion. Today we will see how to delete a node of BST using a non-recursive function. Let us revisit the 3 scenarios here Deleting a node with no children just link the parent to NULL Deleting a node with one child link the parent to  non-null child of node to be deleted Deleting a node with both children select the successor of node to be deleted copy successor's value into this node delete the successor In order to start, we need a function to search for a node in binary search tree. Did you know that searching in  a BST is very fast, and is of the order O(logn). To search Start with root Repeat until value is found or node is NULL If the search value is greater than node branch to right If the search value is lesser than node branch to left.  Here is the function NODEPTR find_node (NODEPTR root,NODEPTR * parent, int delval) { NODEPTR nd = root; NODEPTR pa = root; if (root -> v