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

Delete a node from doubly linked list

Deletion operation in DLL is simpler when compared to SLL. Because we don't have to go in search of previous node of to-be-deleted node.  Here is how you delete a node Link previous node of node of to-be-deleted to next node. Link next node of node of to-be-deleted to previous node. Free the memory of node of to-be-deleted Simple, isn't it. The code can go like this. prevnode = delnode->prev; nextnode = delnode->next; prevnode->next = nextnode; nextnode->prev = prevnode; free(delnode); And that is it. The node delnode is deleted. But we should always consider boundary conditions. What happens if we are trying to delete the first node or last node? If first node is to be deleted, its previous node is NULL. Hence step 3 should not be used.  And also, once head is deleted, nextnode becomes head . Similarly if last node is to be deleted, nextnode is NULL. Hence step 4 is as strict NO NO. And we should set prevnode to tail. After we put these things together, we have...

Function to sort an array using bubble sort

Quick and dirty way of sorting an array is bubble sort. It is very easy to write and follow. But please keep in mind that it is not at all effecient. #include<iostream> using std::cin; using std::cout; void readArray(int arr[],int sz); void printArray(int arr[],int sz); void sortArray(int arr[],int sz); void swap(int &a,int &b); int main() {    int sz;    cout<<"Size of the array=";    cin>>sz;    int arr[sz];    readArray(arr,sz);     sortArray(arr,sz);   cout<<"Sorted array is ";   printArray(arr,sz); } void readArray(int arr[],int sz) {  for(int i=0;i<sz;i++)    {       cout<<"arr["<<i<<"]=";       cin>>arr[i];   } } void printArray(int arr[],int sz) {  for(int i=0;i<sz;i++)    {       cout<<"arr["<<i<<"]=";    ...

Merge two binary search trees

How do you merge two binary search trees? I googled about the solutions. Most solutions told me to convert both trees into linked lists. Merge the lists. Then create a tree from the elements of the list. But why lists? Why can't we store the elements in an array? Because if the data of the tree is larger - not just integer keys, array manipulation becomes difficult. But again, we need not convert both the trees into lists. We can convert one tree into list - a doubly linked list. Then insert the elements of this list into the other tree. I tried this approach. To convert a tree into a sorted doubly linked list Create a doubly linked list. Let the prev and next links of nodes in this list be called left and right respectively. This way we can directly use the binary tree nodes in the list. Use a static variable previousnode  call the function recursively for left child of current node. link current node to the previousnode set next pointer of previousnode to curre...