0% found this document useful (0 votes)
20 views8 pages

Heap Data Structure

A Heap is a complete binary tree data structure that maintains the heap property, where in a Min-Heap the parent node is less than or equal to its children, while in a Max-Heap it is greater than or equal. Heaps are commonly used to implement priority queues and can be represented as arrays, allowing efficient insertion and deletion operations. Key applications include job scheduling in operating systems, Huffman coding for data compression, and algorithms like Dijkstra's shortest path.

Uploaded by

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

Heap Data Structure

A Heap is a complete binary tree data structure that maintains the heap property, where in a Min-Heap the parent node is less than or equal to its children, while in a Max-Heap it is greater than or equal. Heaps are commonly used to implement priority queues and can be represented as arrays, allowing efficient insertion and deletion operations. Key applications include job scheduling in operating systems, Huffman coding for data compression, and algorithms like Dijkstra's shortest path.

Uploaded by

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

Heap Data Structure

A Heap is a complete binary tree data structure that satisfies the heap property: for
every node, the value of its children is greater than or equal to its own value. Heaps are
usually used to implement priority queues, where the smallest (or largest) element is
always at the root of the tree.It either follows max heap or min heap property.

A Heap is a special Tree-based data structure in which the tree is a complete binary
tree. Since a heap is a complete binary tree, a heap with N nodes has log N height. It is
useful to remove the highest or lowest priority element. It is typically represented as
an array. There are two types of Heaps in the data structure.

Min-Heap

In a Min-Heap the key present at the root node must be less than or equal among the
keys present at all of its children. The same property must be recursively true for all
sub-trees in that Binary Tree. In a Min-Heap the minimum key element present at the
root. Below is the Binary Tree that satisfies all the properties of Min Heap.
A min-heap is a complete binary tree in which the value of each node is less than the
value of its left child and right child. This property is true for every node in the tree. In
this article, we will learn how we can implement the min heap data structure in C++.

Implementation of Min Heap in C++


A min heap is a tree-based data structure that is a complete binary tree which means
the nodes are inserted in left to right order. In a min heap the value of every root node
must be less than the value of its equivalent left and right child node.

To implement a min heap in C++, array data structure is used to represent the node
values of the min heap. So, for any node at index i in the array,

It's left child is present at index 2*i+1.

It's right child is present at index 2*i+2.

It's parent node is present at the (i-1)/2th index.

Here are some common operations that can be performed on a Heap Data Structure,
1. Insertion in Min-Heap Data Structure:
Elements can be inserted into the heap following a similar approach as discussed
above for deletion. The idea is to:
● The insertion operation in a min-heap involves the following steps:
● Add the new element to the end of the heap, in the next available position in
the last level of the tree.
● Compare the new element with its parent. If the parent is greater than the
new element, swap them.
● Repeat step 2 until the parent is smaller than or equal to the new element, or
until the new element reaches the root of the tree.
● The new element is now in its correct position in the min heap, and the heap
property is satisfied

A Min heap is typically represented as an array.


The root element will be at arr[0].

For any ith node arr[i]:

arr[(i -1) / 2] returns its parent node.

arr[(2 * i) + 1] returns its left child node.


arr[(2 * i) + 2] returns its right child node.

Deletion: To remove the minimum element from the min heap, we first swap the root
node with the last element in the array, remove the last element, and then adjust the
heap property by repeatedly swapping the element with its smallest child until it is in the
correct position.

Heapify: A heapify operation can be used to create a min heap from an unsorted

array.void minHeapify(vector<int> &arr, int i, int n) {


int smallest = i;
int l = 2*i + 1;
int r = 2*i + 2;
if (l < n && arr[l] < arr[smallest])
smallest = l;
if (r < n && arr[r] < arr[smallest])
smallest = r;
if (smallest != i) {
swap(arr[i], arr[smallest]);
minHeapify(arr, smallest, n);
}
}

Applications of Min Heap


Following are some common applications of Min Heap:
Operating Systems: Min Heaps are used in operating systems to implement
priority queues for job scheduling.

Huffman Coding: Min heap is used to build the Huffman tree which is used for
the data compression.

Dijkstra's Shortest Path Algorithm: Min heaps are used in Dijkstra's algorithm
in graph theory to find the next vertex with minimum distance from the source
vertex.

Prim's Minimum Spanning Tree Algorithm: To keep track of the minimum


weight edges min heap is used to build the minimum spanning tree using Prim's
Algorithm.

Advantages of Min-heap Data Structure:


● Efficient insertion and deletion: Min heap allows fast insertion and deletion
of elements with a time complexity of O(log n), where n is the number of
elements in the heap.
● Efficient retrieval of minimum element: The minimum element in a min
heap is always at the root of the heap, which can be retrieved in O(1) time.
● Space efficient: Min heap is a compact data structure that can be
implemented using an array or a binary tree, which makes it space efficient.
● Sorting: Min heap can be used to implement an efficient sorting algorithm
such as heap sort with a time complexity of O(n log n).
● Priority Queue: Min heap can be used to implement a priority queue, where
the element with the minimum priority can be retrieved efficiently in O(1)
time.
● Versatility: Min heap has several applications in computer science, including
graph algorithms, data compression, and database systems.

Max-Heap

In a Max-Heap the key present at the root node must be greater than or equal among
the keys present at all of its children. The same property must be recursively true for
all sub-trees in that Binary Tree. In a Max-Heap the maximum key element present at
the root. Below is the Binary Tree that satisfies all the properties of Max Heap.
Difference between Min Heap and Max Heap

Min Heap Max Heap

In a Min-Heap the key present at In a Max-Heap the key present at


1 the root node must be less than the root node must be greater
. or equal to among the keys than or equal to among the keys
present at all of its children. present at all of its children.

2 In a Min-Heap the minimum key In a Max-Heap the maximum key


. element present at the root. element present at the root.
3 A Min-Heap uses the ascending A Max-Heap uses the descending
. priority. priority.

In the construction of a In the construction of a


4
Min-Heap, the smallest element Max-Heap, the largest element
.
has priority. has priority.

In a Min-Heap, the smallest In a Max-Heap, the largest


5
element is the first to be popped element is the first to be popped
.
from the heap. from the heap.

Applications of Heaps:

Heap Sort: Heap Sort is one of the best sorting algorithms that use Binary Heap
to sort an array in O(N*log N) time.

Priority Queue: A priority queue can be implemented by using a heap because it


supports insert(), delete(), extractMax(), decreaseKey() operations in O(log N)
time.

Graph Algorithms: The heaps are especially used in Graph Algorithms like
Dijkstra’s Shortest Path and Prim’s Minimum Spanning Tree.

Let’s understand the maxHeapify function in detail:-


maxHeapify is the function responsible for restoring the property of the Max Heap. It
arranges the node i, and its subtrees accordingly so that the heap property is
maintained.
1. Suppose we are given an array, arr[] representing the complete binary tree.

The left and the right child of ith node are in indices 2*i+1 and 2*i+2.
2. We set the index of the current element, i, as the ‘MAXIMUM’.

3. If arr[2 * i + 1] > arr[i], i.e., the left child is larger than the current value, it is

set as ‘MAXIMUM’.
4. Similarly if arr[2 * i + 2] > arr[i], i.e., the right child is larger than the current

value, it is set as ‘MAXIMUM’.


5. Swap the ‘MAXIMUM’ with the current element.
6. Repeat steps 2 to 5 till the property of the heap is restored.

Performance Analysis of Min-Heap and Max-Heap:

Get Maximum or Minimum Element: O(1 ) Insert Element into Max-Heap or


Min-Heap: O(log N) Remove Maximum or Minimum Element: O(log N)

void MaxHeap::MaxHeapify(int i)
{
int l = lChild(i);
int r = rChild(i);
int largest = i;
if (l < heapSize && arr[l] > arr[i])
largest = l;
if (r < heapSize && arr[r] > arr[largest])
largest = r;
if (largest != i) {
swap(arr[i], arr[largest]);
MaxHeapify(largest);
}}

You might also like