0% found this document useful (0 votes)
5 views

Heap Data Structure

The heap data structure is a complete binary tree that can be classified as either a max heap, where each parent node is greater than its children, or a min heap, where each parent node is smaller than its children. Key operations on heaps include heapify, insertion, deletion, peek, and extract-max/min, each with specific algorithms to maintain the heap properties. The document provides detailed steps and examples for creating and manipulating max heaps and min heaps.

Uploaded by

soniyagupta1154
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Heap Data Structure

The heap data structure is a complete binary tree that can be classified as either a max heap, where each parent node is greater than its children, or a min heap, where each parent node is smaller than its children. Key operations on heaps include heapify, insertion, deletion, peek, and extract-max/min, each with specific algorithms to maintain the heap properties. The document provides detailed steps and examples for creating and manipulating max heaps and min heaps.

Uploaded by

soniyagupta1154
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Heap Data Structure

Heap data structure is a complete binary tree that satisfies the heap property, where
any given node is
 always greater than its child node/s and the key of the root node is the largest among
all other nodes. This property is also called max heap property.
 always smaller than the child node/s and the key of the root node is the smallest
among all other nodes. This property is also called min heap property.

Max-heap

Min-heap

This type of data structure is also called a binary heap.


Heap Operations

Some of the important operations performed on a heap are described below along with
their algorithms.

Heapify

Heapify is the process of creating a heap data structure from a binary tree. It is used to
create a Min-Heap or a Max-Heap.

1. Let the input array be

Initial Array

2. Create a complete binary tree from the array

Complete binary tree


3. Start from the first index of non-leaf node whose index is given by n/2 - 1 .

4. Set current element i as largest .

5. The index of left child is given by 2i + 1 and the right child is given by 2i + 2 .
If leftChild is greater than currentElement (i.e. element at ith index), set leftChildIndex as
largest.
6. Swap largest with currentElement

7. Repeat steps 3-7 until the subtrees are also heapified.


Algorithm
Heapify(array, size, i)
set i as largest
leftChild = 2i + 1
rightChild = 2i + 2

if leftChild > array[largest]


set leftChildIndex as largest
if rightChild > array[largest]
set rightChildIndex as largest

swap array[i] and array[largest]


To create a Max-Heap:

MaxHeap(array, size)
loop from the first index of non-leaf node down to zero
call heapify
For Min-Heap, both leftChild and rightChild must be larger than the parent for all
nodes.
Insert Element into Heap
Algorithm for insertion in Max Heap

If there is no node,
create a newNode.
else (a node is already present)
insert the newNode at the end (last node from left to right.)

heapify the array


1. Insert the new element at the end of the tree.
2. Heapify the tree.

For Min Heap, the above algorithm is modified so that parentNode is always smaller
than newNode .

Delete Element from Heap

Algorithm for deletion in Max Heap

If nodeToBeDeleted is the leafNode


remove the node
Else swap nodeToBeDeleted with the lastLeafNode
remove noteToBeDeleted

heapify the array


1. Select the element to be deleted.

2. Swap it with the last element.

3. Remove the last element.


4. Heapify the tree.

For Min Heap, above algorithm is modified so that both childNodes are greater
smaller than currentNode .

Peek (Find max/min)


Peek operation returns the maximum element from Max Heap or minimum element
from Min Heap without deleting the node.

For both Max heap and Min Heap

return rootNode
Extract-Max/Min

Extract-Max returns the node with maximum value after removing it from a Max
Heap whereas Extract-Min returns the node with minimum after removing it from
Min Heap.
Example-

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

You might also like