The Wayback Machine - https://web.archive.org/web/20240818195508/https://www.geeksforgeeks.org/min-heap-meaning-in-dsa/
Open In App

Min Heap meaning in DSA

Last Updated : 17 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

A min heap is a binary tree-based data structure where the value of each node is less than or equal to its child nodes. 

In other words, the root node is always the minimum element in the heap. Here is an example of the min heap tree:

Example of min Heap

Example of min Heap

Array representation of the above min heap

Array representation of the above min heap

Characteristics of Min Heap :

  • A min heap is a complete binary tree. This property ensures that the tree is balanced and can be represented efficiently using an array.
  • The value of each node in a min heap is less than or equal to the values of its child nodes.
  • Insertion and deletion of elements take O(log n) time in a min heap, where n is the number of elements in the heap. This makes it efficient for large datasets.

Applications of Min Heap:

  • Priority queues: Min heap is commonly used to implement priority queues, where elements are stored based on their priority.
  • Heap sort: Heap sort is a sorting algorithm that uses a min heap to sort elements in ascending order. The elements are first added to a heap, and then removed in order to create a sorted list.
  • Graph algorithms: Min heap is used in graph algorithms like Dijkstra’s algorithm and Prim’s algorithm to maintain the set of vertices with the shortest distance or minimum weight. In these algorithms, the heap is used to store the unexplored vertices and to extract the vertex with the minimum distance/weight.
  • Memory management: Min heap can be used in memory management to allocate memory to processes in an efficient manner by extracting a block with the minimum size.

Advantages of Min Heap:

  • Constant-time access to the minimum element: The root of a min heap always holds the minimum element, which can be accessed in constant time, providing efficient access to the minimum element.
  • Efficient insertion and deletion: Insertion and deletion of elements take O(log n) time, where n is the number of elements in the heap. This makes it efficient for large datasets.
  • Space efficiency: A min heap can be implemented using an array

Disadvantages of Min Heap:

  • Accessing non-minimum elements in a min heap can be time-consuming, as there is no direct way to access them. It may require traversing the entire heap to find the desired element.
  • Although the insertion and deletion of elements take O(log n) time, building a heap from an unsorted array takes O(n) time, which can be time-consuming for large datasets.

What else can you read?



Similar Reads

Difference between Binary Heap, Binomial Heap and Fibonacci Heap
Binary Heap:A Binary Heap is a Binary Tree with following properties. It’s a complete binary tree i.e., all levels are completely filled except possibly the last level and the last level has all keys as left as possible. This property of Binary Heap makes them suitable to be stored in an array.A Binary Heap is either Min Heap or Max Heap. In a Min
2 min read
Convert Min Heap to Max Heap
Given an array representation of min Heap, convert it to max Heap. Examples: Input: arr[] = {3, 5, 9, 6, 8, 20, 10, 12, 18, 9} 3 / \ 5 9 / \ / \ 6 8 20 10 / \ /12 18 9 Output: arr[] = {20, 18, 10, 12, 9, 9, 3, 5, 6, 8} 20 / \ 18 10 / \ / \ 12 9 9 3 / \ /5 6 8 Input: arr[] = {3, 4, 8, 11, 13}Output: arr[] = {13, 11, 8, 4, 3} Approach: To solve the p
10 min read
Heap Sort for decreasing order using min heap
Given an array of elements, sort the array in decreasing order using min heap. Examples: Input : arr[] = {5, 3, 10, 1} Output : arr[] = {10, 5, 3, 1} Input : arr[] = {1, 50, 100, 25} Output : arr[] = {100, 50, 25, 1} Prerequisite: Heap sort using min heap. Algorithm : Build a min heap from the input data. At this point, the smallest item is stored
13 min read
Difference between Min Heap and Max Heap
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-HeapIn a Min-Heap the
3 min read
Max Heap meaning in DSA
A max heap is a complete binary tree in which every parent node has a value greater than or equal to its children nodes. Properties of a Max Heap :A max heap is a complete binary tree, meaning that all levels of the tree are completely filled, except possibly the last level which is filled from left to right.In a max heap, every parent node has a v
3 min read
What is DSA | DSA Full Form
What is DSA?DSA(Data Structures and Algorithms) is defined as a combination of two separate yet interrelated topics – Data Structure and Algorithms. DSA is one of the most important skills that every computer science student must have. It is often seen that people with good knowledge of these technologies are better programmers than others and thus
2 min read
When building a Heap, is the structure of Heap unique?
What is Heap? A heap is a tree based data structure where the tree is a complete binary tree that maintains the property that either the children of a node are less than itself (max heap) or the children are greater than the node (min heap). Properties of Heap: Structural Property: This property states that it should be A Complete Binary Tree. For
4 min read
What's the relationship between "a" heap and "the" heap?
A Heap: "A Heap" refers to the heap data structure where we can store data in a specific order. Heap is a Tree-based data structure where the tree is a complete binary tree. Heap is basically of two types: Max-Heap: The key at the Root node of the tree will be the greatest among all the keys present in that heap and the same property will be follow
15+ min read
Maximum element in min heap
Given a min heap, find the maximum element present in the heap. Examples: Input : 10 / \ 25 23 / \ / \ 45 30 50 40 Output : 50 Input : 20 / \ 40 28 Output : 40 Brute force approach: We can check all the nodes in the min-heap to get the maximum element. Note that this approach works on any binary tree and does not makes use of any property of the mi
8 min read
Fibonacci Heap - Deletion, Extract min and Decrease key
In the last post, we discussed the Insertion and Union of Fibonacci Heaps. In this post, we will discuss Extract_min(), Decrease_key() and Deletion() operations on Fibonacci heap. Prerequisites: Fibonacci Heap (Introduction) Fibonacci Heap - Insertion and Union Extract_min(): We create a function for deleting the minimum node and setting the min po
12 min read
Maximise the number of toys that can be purchased with amount K using min Heap
Given an array arr[] consisting of the cost of toys and an integer K depicting the amount of money available to purchase toys. The task is to find the maximum number of toys one can buy with the amount K.Note: One can buy only 1 quantity of a particular toy. Examples: Input: arr[] = {1, 12, 5, 111, 200, 1000, 10, 9, 12, 15}, K = 50 Output: 6 Toys w
5 min read
Min Heap in Python
A Min-Heap is a complete binary tree in which the value in each internal node is smaller than or equal to the values in the children of that node. Mapping the elements of a heap into an array is trivial: if a node is stored at index k, then its left child is stored at index 2k + 1 and its right child at index 2k + 2 for 0 based indexing and for 1 b
5 min read
Complexity analysis of various operations of Binary Min Heap
A Min Heap is a Complete Binary Tree in which the children nodes have a higher value (lesser priority) than the parent nodes, i.e., any path from the root to the leaf nodes, has an ascending order of elements. In the case of a binary tree, the root is considered to be at height 0, its children nodes are considered to be at height 1, and so on. Each
3 min read
Find min and max values among all maximum leaf nodes from all possible Binary Max Heap
Given a positive integer N, the task is to find the largest and smallest elements, from the maximum leaf nodes of every possible binary max-heap formed by taking the first N natural numbers as the nodes' value of the binary max-heap. Examples: Input: N = 2Output: 1 1Explanation: There is only one maximum binary heap with the nodes {1, 2}: In the ab
7 min read
Min Heap in JavaScript
A Min-Heap is a complete binary tree in which the value in each internal node is smaller than or equal to the values in the children of that node. Mapping the elements of a heap into an array is trivial: if a node is stored an index k, then its left child is stored at index 2k + 1 and its right child at index 2k + 2. How is Min Heap represented? Le
4 min read
Merge two sorted arrays in constant space using Min Heap
Given two sorted arrays, we need to merge them with O(1) extra space into a sorted array, when N is the size of the first array, and M is the size of the second array. Example : Input: arr1[] = {10}; arr2[] = {2, 3};Output: arr1[] = {2} arr2[] = {3, 10} Input: arr1[] = {1, 5, 9, 10, 15, 20}; arr2[] = {2, 3, 8, 13};Output: arr1[] = {1, 2, 3, 5, 8, 9
8 min read
Javascript Program To Merge K Sorted Linked Lists Using Min Heap - Set 2
Given k linked lists each of size n and each list is sorted in non-decreasing order, merge them into a single sorted (non-decreasing order) linked list and print the sorted linked list as output.Examples:  Input: k = 3, n = 4 list1 = 1->3->5->7->NULL list2 = 2->4->6->8->NULL list3 = 0->9->10->11->NULL Output: 0->1->2->3->4->5->6->7->8->9->10->11 Me
5 min read
K’th Least Element in a Min-Heap
Given a min-heap of size n, find the kth least element in the min-heap. Examples: Input : {10, 50, 40, 75, 60, 65, 45} k = 4 Output : 50 Input : {10, 50, 40, 75, 60, 65, 45} k = 2 Output : 40 Naive approach: We can extract the minimum element from the min-heap k times and the last element extracted will be the kth least element. Each deletion opera
15+ min read
Convert BST into a Min-Heap without using array
Given a Binary Search Tree, convert it into a Min-Heap containing the same elements in O(n) time. Do this in-place. Input: Binary Search Tree 8 / \ 4 12 / \ / \ 2 6 10 14 Output - Min Heap 2 / \ 4 6 / \ / \ 8 10 12 14 [Or any other tree that follows Min Heap properties and has same keys] If we are allowed to use extra space, we can perform inorder
15+ min read
Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
Given the level order traversal of a Complete Binary Tree, determine whether the Binary Tree is a valid Min-Heap Examples: Input : level = [10, 15, 14, 25, 30] Output : True The tree of the given level order traversal is 10 / \ 15 14 / \ 25 30 We see that each parent has a value less than its child, and hence satisfies the min-heap property Input :
6 min read
Convert BST to Min Heap
Given a binary search tree which is also a complete binary tree. The problem is to convert the given BST into a Min Heap with the condition that all the values in the left subtree of a node should be less than all the values in the right subtree of the node. This condition is applied to all the nodes, in the resultant converted Min Heap. Examples:
10 min read
Merge K Sorted Linked Lists using Min Heap
Given K linked lists each of size N and each list is sorted in non-decreasing order, merge them into a single sorted (non-decreasing order) linked list and print the sorted linked list as output. Examples: Input: K = 3, N = 4list1 = 1->3->5->7->NULLlist2 = 2->4->6->8->NULLlist3 = 0->9->10->11->NULL Output: 0->
13 min read
Print all nodes less than a value x in a Min Heap.
Given a binary min heap and a value x, print all the binary heap nodes having value less than the given value x. Examples : Consider the below min heap as common input two both below examples. 2 / \ 3 15 / \ / \ 5 4 45 80 / \ / \ 6 150 77 120 Input : x = 15 Output : 2 3 5 6 4 Input : x = 80 Output : 2 3 5 6 4 77 15 45 The idea is to do a preorder t
9 min read
What is Min Heap?
Min Heap is a type of binary heap where the key at the root must be the minimum among all keys present in the binary heap. The same property must be recursively true for all nodes in the binary tree. Min Heap Definition:Min Heap is a complete binary tree, where value of each node is smaller than or equal to the values of its children. Therefore, Mi
3 min read
Introduction to Min-Heap Data Structure
A Min-Heap is defined as a type of Heap Data Structure in which each node is smaller than or equal to its children.  The heap data structure is a type of binary tree that is commonly used in computer science for various purposes, including sorting, searching, and organizing data. Purpose and Use Cases of Min-Heap: Implementing Priority Queue: One o
15+ min read
Circular Linked List meaning in DSA
A circular linked list is a special type of linked list in which the last node is connected to the first node, creating a continuous loop. In a circular linked list, each node has a reference to the next node in the sequence, just like in a regular linked list, but the last node's reference points back to the first node. Characteristics of Circular
3 min read
Queue meaning in DSA
A Queue is defined as a linear data structure that is open at both ends and the operations are performed in the First In First Out (FIFO) order. Characteristics of Queue:The first item added to the queue is the first one to be processed (or can be firstly deleted/removed), and subsequent items are processed in the order they were added.Enqueue and
3 min read
Subarray meaning in DSA
A subarray is a portion of an array that consists of consecutive elements from the original array. Characteristics of a Subarray:Contiguity: The elements in a subarray are contiguous, meaning they are consecutive and in order in the original array.Length: The length of a subarray can be any positive integer, from 1 to the length of the original arr
2 min read
Disjoint Set meaning and definition in DSA
Disjoint Set is a data structure that keeps track of a set of elements partitioned into a number of disjoint subsets and it is used to efficiently solve problems that involve grouping elements into sets and performing operations on them. Characteristics of the Disjoint Set:It keeps a set partitioned into disjoint subsets.It allows the efficient uni
2 min read
Deque meaning in DSA
Deque, which stands for Double Ended Queue, is a special type of queue that allows adding and removing elements from both front and rear ends. Characteristics: of Deque:Dynamic size: The size of a deque can change dynamically during the execution of a program.Linear: Elements in a deque are stored linearly and can be accessed in a sequential manner
2 min read
Practice Tags :
three90RightbarBannerImg