Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
47 views14 pages

Heaps

Uploaded by

pronach123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views14 pages

Heaps

Uploaded by

pronach123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

A Heap is a special Tree-based data structure in which the tree is a complete

binary tree. Generally, Heaps can be of two types:


1. Max-Heap: In a Max-Heap the key present at the root node must be
greatest among the keys present at all of it’s children. The same
property must be recursively true for all sub-trees in that Binary Tree.
2. Min-Heap: In a Min-Heap the key present at the root node must be
minimum among the keys present at all of it’s children. The same
property must be recursively true for all sub-trees in that Binary Tree.

Insertion in the Heap tree

44, 33, 77, 11, 55, 88, 66

Suppose we want to create the max heap tree. To create the max heap tree,
we need to consider the following two cases:

o First, we have to insert the element in such a way that the property of
the complete binary tree must be maintained.
o Secondly, the value of the parent node should be greater than the
either of its child.

Step 1: First we add the 44 element in the tree as shown below:


Step 2: The next element is 33. As we know that insertion in the binary tree
always starts from the left side so 44 will be added at the left of 33 as shown
below:

Step 3: The next element is 77 and it will be added to the right of the 44 as
shown below:

As we can observe in the above tree that it does not satisfy the max heap
property, i.e., parent node 44 is less than the child 77. So, we will swap these
two values as shown below:
Step 4: The next element is 11. The node 11 is added to the left of 33 as
shown below:

Step 5: The next element is 55. To make it a complete binary tree, we will
add the node 55 to the right of 33 as shown below:
As we can observe in the above figure that it does not satisfy the property of
the max heap because 33<55, so we will swap these two values as shown
below:
Step 6: The next element is 88. The left subtree is completed so we will add
88 to the left of 44 as shown below:
As we can observe in the above figure that it does not satisfy the property of
the max heap because 44<88, so we will swap these two values as shown
below:

Again, it is violating the max heap property because 88>77 so we will swap
these two values as shown below:

Step 7: The next element is 66. To make a complete binary tree, we will add
the 66 element to the right side of 77 as shown below:

In the above figure, we can observe that the tree satisfies the
property of max heap; therefore, it is a heap tree.

Heap Sort Algorithm


First convert the array into heap data structure using heapify, then one by one
delete the root node of the Max-heap and replace it with the last node in the
heap and then heapify the root of the heap. Repeat this process until size of heap
is greater than 1.
 Build a heap from the given input array.
 Repeat the following steps until the heap contains only one element:
 Swap the root element of the heap (which is the largest element) with the last
element of the heap.
 Remove the last element of the heap (which is now in the correct position).
 Heapify the remaining elements of the heap.
 The sorted array is obtained by reversing the order of the elements in the
input array.
Detailed Working of Heap Sort
To understand heap sort more clearly, let’s take an unsorted array and try to
sort it using heap sort.
Consider the array: arr[] = {4, 10, 3, 5, 1}.
Build Complete Binary Tree: Build a complete binary tree from the array.

Heap sort algorithm | Build Complete Binary Tree


Transform into max heap: After that, the task is to construct a tree from that
unsorted array and try to convert it into max heap.
 To transform a heap into a max-heap, the parent node should always be
greater than or equal to the child nodes
 Here, in this example, as the parent node 4 is smaller than the child
node 10, thus, swap them to build a max-heap.
 Now, 4 as a parent is smaller than the child 5, thus swap both of these again
and the resulted heap and array should be like this:
Heap sort algorithm | Max Heapify constructed binary tree
Perform heap sort: Remove the maximum element in each step (i.e., move it to
the end position and remove that) and then consider the remaining elements and
transform it into a max heap.
 Delete the root element (10) from the max heap. In order to delete this node,
try to swap it with the last node, i.e. (1). After removing the root element,
again heapify it to convert it into max heap.
 Resulted heap and array should look like this:

Heap sort algorithm | Remove maximum from root and max heapify
 Repeat the above steps and it will look like the following:
Heap sort algorithm | Remove next maximum from root nad max heapify
 Now remove the root (i.e. 3) again and perform heapify.

Heap sort algorithm | Repeat previous step


 Now when the root is removed once again it is sorted. and the sorted array
will be like arr[] = {1, 3, 4, 5, 10}.
Heap sort algorithm | Final sorted array

Heap Sort Algorithm


Heap sort is one of the sorting algorithms used to arrange a list of elements in order. Heapsort

algorithm uses one of the tree concepts called Heap Tree. In this sorting algorithm, we use Max

Heap to arrange list of elements in Descending order and Min Heap to arrange list elements in

Ascending order.

Step by Step Process

The Heap sort algorithm to arrange a list of elements in ascending order is performed using following

steps...

 Step 1 - Construct a Binary Tree with given list of Elements.


 Step 2 - Transform the Binary Tree into Min Heap.

 Step 3 - Delete the root element from Min Heap using Heapify method.

 Step 4 - Put the deleted element into the Sorted list.

 Step 5 - Repeat the same until Min Heap becomes empty.

 Step 6 - Display the sorted list.


You might also like