The value of the root node must be the smallest among all its descendant nodes and the same thing must be done for its left and right sub-tree also.
Internal Implementation of Min-Heap Data Structure
A heap can be efficiently represented using an array.
If a node is stored at index i:
Its left child is at index 2*i + 1.
Its right child is at index 2*i + 2.
The parent of a node at index i can be found at index [(i-1)/2].
Operations on Min-heap Data Structure and their Implementation:
Here are some common operations that can be performed on a Heap Data Structure,
Insertion - O(log n) Time and O(n) Space
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.
Illustration:
Suppose the Heap is a Min-Heap as:
C++
#include<iostream>#include<vector>usingnamespacestd;voidinsert(vector<int>&heap,intvalue){// Add the new element to the end of the heapheap.push_back(value);// Get the index of the last elementintindex=heap.size()-1;// Compare the new element with//its parent and swap if necessarywhile(index>0&&heap[(index-1)/2]>heap[index]){swap(heap[index],heap[(index-1)/2]);// Move up the tree to the//parent of the current elementindex=(index-1)/2;}}intmain(){vector<int>arr;vector<int>values={10,3,2,4,5,1};intn=values.size();for(inti=0;i<n;i++){insert(arr,values[i]);cout<<"Inserted "<<values[i]<<" into the min-heap: ";for(intj=0;j<arr.size();j++){cout<<arr[j]<<" ";}cout<<endl;}return0;}
Java
importjava.util.ArrayList;publicclassGFG{publicstaticvoidinsert(ArrayList<Integer>heap,intvalue){// Add the new element to the end of the heapheap.add(value);// Get the index of the last elementintindex=heap.size()-1;// Compare the new element with// its parent and swap if necessarywhile(index>0&&heap.get((index-1)/2)>heap.get(index)){inttemp=heap.get(index);heap.set(index,heap.get((index-1)/2));heap.set((index-1)/2,temp);// Move up the tree to the// parent of the current elementindex=(index-1)/2;}}publicstaticvoidmain(String[]args){ArrayList<Integer>arr=newArrayList<>();int[]values={10,3,2,4,5,1};intn=values.length;for(inti=0;i<n;i++){insert(arr,values[i]);System.out.print("Inserted "+values[i]+" into the min-heap: ");for(intj=0;j<arr.size();j++){System.out.print(arr.get(j)+" ");}System.out.println();}}}
Python
definsert(heap,value):# Add the new element to the end of the heapheap.append(value)# Get the index of the last elementindex=len(heap)-1# Compare the new element with# its parent and swap if necessarywhileindex>0andheap[(index-1)//2]>heap[index]:heap[index],heap[(index-1)//2]=heap[(index-1)//2],heap[index]# Move up the tree to the# parent of the current elementindex=(index-1)//2if__name__=="__main__":arr=[]values=[10,3,2,4,5,1]n=len(values)foriinrange(n):insert(arr,values[i])print(f"Inserted {values[i]} into the min-heap: ",end="")forjinrange(len(arr)):print(arr[j],end=" ")print()
C#
usingSystem;usingSystem.Collections.Generic;classGFG{staticvoidinsert(List<int>heap,intvalue){// Add the new element to the end of the heapheap.Add(value);// Get the index of the last elementintindex=heap.Count-1;// Compare the new element with// its parent and swap if necessarywhile(index>0&&heap[(index-1)/2]>heap[index]){inttemp=heap[index];heap[index]=heap[(index-1)/2];heap[(index-1)/2]=temp;// Move up the tree to the// parent of the current elementindex=(index-1)/2;}}staticvoidMain(){List<int>arr=newList<int>();int[]values={10,3,2,4,5,1};intn=values.Length;for(inti=0;i<n;i++){insert(arr,values[i]);Console.Write("Inserted "+values[i]+" into the min-heap: ");for(intj=0;j<arr.Count;j++){Console.Write(arr[j]+" ");}Console.WriteLine();}}}
JavaScript
functioninsert(heap,value){// Add the new element to the end of the heapheap.push(value);// Get the index of the last elementletindex=heap.length-1;// Compare the new element with// its parent and swap if necessarywhile(index>0&&heap[Math.floor((index-1)/2)]>heap[index]){lettemp=heap[index];heap[index]=heap[Math.floor((index-1)/2)];heap[Math.floor((index-1)/2)]=temp;// Move up the tree to the// parent of the current elementindex=Math.floor((index-1)/2);}}//Driver Codeletarr=[];letvalues=[10,3,2,4,5,1];letn=values.length;for(leti=0;i<n;i++){insert(arr,values[i]);process.stdout.write(`Inserted ${values[i]} into the min-heap: `);for(letj=0;j<arr.length;j++){process.stdout.write(arr[j]+" ");}console.log();}
Output
Inserted 10 into the min-heap: 10
Inserted 3 into the min-heap: 3 10
Inserted 2 into the min-heap: 2 10 3
Inserted 4 into the min-heap: 2 4 3 10
Inserted 5 into the min-heap: 2 4 3 10 5
Inserted ...
Deletion - O(log n) Time and O(n) Space
Removing the smallest element (the root) from a Min-Heap involves the following steps:
Replace the root (or the element to be deleted) with the last element in the heap.
Remove the last element from the heap, since it has been moved to the root.
Heapify-down: The element now at the root may violate the Min-Heap property, so perform heapify starting from the root to restore the heap property.
Illustration:
C++
#include<iostream>#include<vector>usingnamespacestd;voidinsert(vector<int>&heap,intvalue){// Add the new element to the end of the heapheap.push_back(value);// Get the index of the last elementintindex=heap.size()-1;// Compare the new element with its parent and swap if// necessarywhile(index>0&&heap[(index-1)/2]>heap[index]){swap(heap[index],heap[(index-1)/2]);// Move up the tree to the parent of the current// elementindex=(index-1)/2;}}// Function to delete a node from the min-heapvoiddeleteMin(vector<int>&heap,intvalue){// Find the index of the element to be deletedintindex=-1;for(inti=0;i<heap.size();i++){if(heap[i]==value){index=i;break;}}// If the element is not found, returnif(index==-1){return;}// Replace the element to be deleted with the last// elementheap[index]=heap[heap.size()-1];// Remove the last elementheap.pop_back();// Heapify the tree starting from the element at the// deleted indexwhile(true){intleft_child=2*index+1;intright_child=2*index+2;intsmallest=index;if(left_child<heap.size()&&heap[left_child]<heap[smallest]){smallest=left_child;}if(right_child<heap.size()&&heap[right_child]<heap[smallest]){smallest=right_child;}if(smallest!=index){swap(heap[index],heap[smallest]);index=smallest;}else{break;}}}intmain(){vector<int>arr;vector<int>values={13,16,31,41,51,100};intn=values.size();for(inti=0;i<n;i++){insert(arr,values[i]);}cout<<"Initial heap: ";for(intj=0;j<arr.size();j++){cout<<arr[j]<<" ";}cout<<endl;deleteMin(arr,13);cout<<"Heap after deleting 13: ";for(intj=0;j<arr.size();j++){cout<<arr[j]<<" ";}cout<<endl;return0;}
Java
importjava.util.ArrayList;publicclassGFG{publicstaticvoidinsert(ArrayList<Integer>heap,intvalue){// Add the new element to the end of the heapheap.add(value);// Get the index of the last elementintindex=heap.size()-1;// Compare the new element with its parent and swap if// necessarywhile(index>0&&heap.get((index-1)/2)>heap.get(index)){inttemp=heap.get(index);heap.set(index,heap.get((index-1)/2));heap.set((index-1)/2,temp);// Move up the tree to the parent of the current// elementindex=(index-1)/2;}}// Function to delete a node from the min-heappublicstaticvoiddeleteMin(ArrayList<Integer>heap,intvalue){// Find the index of the element to be deletedintindex=-1;for(inti=0;i<heap.size();i++){if(heap.get(i)==value){index=i;break;}}// If the element is not found, returnif(index==-1){return;}// Replace the element to be deleted with the last// elementheap.set(index,heap.get(heap.size()-1));// Remove the last elementheap.remove(heap.size()-1);// Heapify the tree starting from the element at the// deleted indexwhile(true){intleft_child=2*index+1;intright_child=2*index+2;intsmallest=index;if(left_child<heap.size()&&heap.get(left_child)<heap.get(smallest)){smallest=left_child;}if(right_child<heap.size()&&heap.get(right_child)<heap.get(smallest)){smallest=right_child;}if(smallest!=index){inttemp=heap.get(index);heap.set(index,heap.get(smallest));heap.set(smallest,temp);index=smallest;}else{break;}}}publicstaticvoidmain(String[]args){ArrayList<Integer>arr=newArrayList<>();int[]values={13,16,31,41,51,100};intn=values.length;for(inti=0;i<n;i++){insert(arr,values[i]);}System.out.print("Initial heap: ");for(intj=0;j<arr.size();j++){System.out.print(arr.get(j)+" ");}System.out.println();deleteMin(arr,13);System.out.print("Heap after deleting 13: ");for(intj=0;j<arr.size();j++){System.out.print(arr.get(j)+" ");}System.out.println();}}
Python
definsert(heap,value):# Add the new element to the end of the heapheap.append(value)# Get the index of the last elementindex=len(heap)-1# Compare the new element with its parent and swap if# necessarywhileindex>0andheap[(index-1)//2]>heap[index]:heap[index],heap[(index-1)//2]=heap[(index-1)//2],heap[index]# Move up the tree to the parent of the current# elementindex=(index-1)//2# Function to delete a node from the min-heapdefdeleteMin(heap,value):# Find the index of the element to be deletedindex=-1foriinrange(len(heap)):ifheap[i]==value:index=ibreak# If the element is not found, returnifindex==-1:return# Replace the element to be deleted with the last# elementheap[index]=heap[-1]# Remove the last elementheap.pop()# Heapify the tree starting from the element at the# deleted indexwhileTrue:left_child=2*index+1right_child=2*index+2smallest=indexifleft_child<len(heap)andheap[left_child]<heap[smallest]:smallest=left_childifright_child<len(heap)andheap[right_child]<heap[smallest]:smallest=right_childifsmallest!=index:heap[index],heap[smallest]=heap[smallest],heap[index]index=smallestelse:breakif__name__=="__main__":arr=[]values=[13,16,31,41,51,100]n=len(values)foriinrange(n):insert(arr,values[i])print("Initial heap: ",end="")forvalinarr:print(val,end=" ")print()deleteMin(arr,13)print("Heap after deleting 13: ",end="")forvalinarr:print(val,end=" ")print()
C#
usingSystem;usingSystem.Collections.Generic;classGFG{staticvoidinsert(List<int>heap,intvalue){// Add the new element to the end of the heapheap.Add(value);// Get the index of the last elementintindex=heap.Count-1;// Compare the new element with its parent and swap if// necessarywhile(index>0&&heap[(index-1)/2]>heap[index]){inttemp=heap[index];heap[index]=heap[(index-1)/2];heap[(index-1)/2]=temp;// Move up the tree to the parent of the current// elementindex=(index-1)/2;}}// Function to delete a node from the min-heapstaticvoiddeleteMin(List<int>heap,intvalue){// Find the index of the element to be deletedintindex=-1;for(inti=0;i<heap.Count;i++){if(heap[i]==value){index=i;break;}}// If the element is not found, returnif(index==-1){return;}// Replace the element to be deleted with the last// elementheap[index]=heap[heap.Count-1];// Remove the last elementheap.RemoveAt(heap.Count-1);// Heapify the tree starting from the element at the// deleted indexwhile(true){intleft_child=2*index+1;intright_child=2*index+2;intsmallest=index;if(left_child<heap.Count&&heap[left_child]<heap[smallest]){smallest=left_child;}if(right_child<heap.Count&&heap[right_child]<heap[smallest]){smallest=right_child;}if(smallest!=index){inttemp=heap[index];heap[index]=heap[smallest];heap[smallest]=temp;index=smallest;}else{break;}}}staticvoidMain(){List<int>arr=newList<int>();int[]values={13,16,31,41,51,100};intn=values.Length;for(inti=0;i<n;i++){insert(arr,values[i]);}Console.Write("Initial heap: ");for(intj=0;j<arr.Count;j++){Console.Write(arr[j]+" ");}Console.WriteLine();deleteMin(arr,13);Console.Write("Heap after deleting 13: ");for(intj=0;j<arr.Count;j++){Console.Write(arr[j]+" ");}Console.WriteLine();}}
JavaScript
functioninsert(heap,value){// Add the new element to the end of the heapheap.push(value);// Get the index of the last elementletindex=heap.length-1;// Compare the new element with its parent and swap if// necessarywhile(index>0&&heap[Math.floor((index-1)/2)]>heap[index]){lettemp=heap[index];heap[index]=heap[Math.floor((index-1)/2)];heap[Math.floor((index-1)/2)]=temp;// Move up the tree to the parent of the current// elementindex=Math.floor((index-1)/2);}}// Function to delete a node from the min-heapfunctiondeleteMin(heap,value){// Find the index of the element to be deletedletindex=-1;for(leti=0;i<heap.length;i++){if(heap[i]===value){index=i;break;}}// If the element is not found, returnif(index===-1)return;// Replace the element to be deleted with the last// elementheap[index]=heap[heap.length-1];// Remove the last elementheap.pop();// Heapify the tree starting from the element at the// deleted indexwhile(true){letleft_child=2*index+1;letright_child=2*index+2;letsmallest=index;if(left_child<heap.length&&heap[left_child]<heap[smallest]){smallest=left_child;}if(right_child<heap.length&&heap[right_child]<heap[smallest]){smallest=right_child;}if(smallest!==index){lettemp=heap[index];heap[index]=heap[smallest];heap[smallest]=temp;index=smallest;}else{break;}}}// Driver codeletarr=[];constvalues=[13,16,31,41,51,100];constn=values.length;for(leti=0;i<n;i++){insert(arr,values[i]);}process.stdout.write("Initial heap: ");for(letj=0;j<arr.length;j++){process.stdout.write(arr[j]+" ");}console.log();deleteMin(arr,13);process.stdout.write("Heap after deleting 13: ");for(letj=0;j<arr.length;j++){process.stdout.write(arr[j]+" ");}console.log();
To access the minimum element (i.e., the root of the heap), the value of the root node is returned.
C++
#include<iostream>#include<vector>usingnamespacestd;// Function to insert an element into min-heapvoidinsert(vector<int>&heap,intvalue){// Add the new element at the endheap.push_back(value);// Heapify-up to maintain min-heap propertyintindex=heap.size()-1;while(index>0&&heap[(index-1)/2]>heap[index]){swap(heap[index],heap[(index-1)/2]);index=(index-1)/2;}}// Function to get the peak element of min-heapinttop(constvector<int>&heap){if(!heap.empty())// Root elementreturnheap[0];return-1;}intmain(){vector<int>minHeap;// Insert elements into the min-heapinsert(minHeap,51);insert(minHeap,41);insert(minHeap,31);insert(minHeap,16);insert(minHeap,13);// Get the peak element (smallest in min-heap)intpeakElement=top(minHeap);cout<<"Peak element: "<<peakElement<<endl;return0;}
Java
importjava.util.ArrayList;publicclassGFG{// Function to insert an element into min-heappublicstaticvoidinsert(ArrayList<Integer>heap,intvalue){// Add the new element at the endheap.add(value);// Heapify-up to maintain min-heap propertyintindex=heap.size()-1;while(index>0&&heap.get((index-1)/2)>heap.get(index)){inttemp=heap.get(index);heap.set(index,heap.get((index-1)/2));heap.set((index-1)/2,temp);index=(index-1)/2;}}// Function to get the peak element of min-heappublicstaticinttop(ArrayList<Integer>heap){if(!heap.isEmpty())// Root elementreturnheap.get(0);return-1;}publicstaticvoidmain(String[]args){ArrayList<Integer>minHeap=newArrayList<>();// Insert elements into the min-heapinsert(minHeap,51);insert(minHeap,41);insert(minHeap,31);insert(minHeap,16);insert(minHeap,13);// Get the peak element (smallest in min-heap)intpeakElement=top(minHeap);System.out.println("Peak element: "+peakElement);}}
Python
# Function to insert an element into min-heapdefinsert(heap,value):# Add the new element at the endheap.append(value)# Heapify-up to maintain min-heap propertyindex=len(heap)-1whileindex>0andheap[(index-1)//2]>heap[index]:heap[index],heap[(index-1)//2]=heap[(index-1)//2],heap[index]index=(index-1)//2# Function to get the peak element of min-heapdeftop(heap):ifheap:# Root elementreturnheap[0]return-1if__name__=="__main__":minHeap=[]# Insert elements into the min-heapinsert(minHeap,51);insert(minHeap,41);insert(minHeap,31);insert(minHeap,16);insert(minHeap,13);# Get the peak element (smallest in min-heap)peakElement=top(minHeap)print("Peak element:",peakElement)
C#
usingSystem;usingSystem.Collections.Generic;classGFG{// Function to insert an element into min-heapstaticvoidinsert(List<int>heap,intvalue){// Add the new element at the endheap.Add(value);// Heapify-up to maintain min-heap propertyintindex=heap.Count-1;while(index>0&&heap[(index-1)/2]>heap[index]){inttemp=heap[index];heap[index]=heap[(index-1)/2];heap[(index-1)/2]=temp;index=(index-1)/2;}}// Function to get the peak element of min-heapstaticinttop(List<int>heap){if(heap.Count>0)// Root elementreturnheap[0];return-1;}staticvoidMain(){List<int>minHeap=newList<int>();// Insert elements into the min-heapinsert(minHeap,51);insert(minHeap,51);insert(minHeap,41);insert(minHeap,31);insert(minHeap,16);insert(minHeap,13);// Get the peak element (smallest in min-heap)intpeakElement=top(minHeap);Console.WriteLine("Peak element: "+peakElement);}}
JavaScript
// Function to insert an element into min-heapfunctioninsert(heap,value){// Add the new element at the endheap.push(value);// Heapify-up to maintain min-heap propertyletindex=heap.length-1;while(index>0&&heap[Math.floor((index-1)/2)]>heap[index]){lettemp=heap[index];heap[index]=heap[Math.floor((index-1)/2)];heap[Math.floor((index-1)/2)]=temp;index=Math.floor((index-1)/2);}}// Function to get the peak element of min-heapfunctiontop(heap){if(heap.length>0){// Root elementreturnheap[0];}return-1;}// driver codeletminHeap=[];// Insert elements into the min-heapinsert(minHeap,51);insert(minHeap,41);insert(minHeap,31);insert(minHeap,16);insert(minHeap,13);// Get the peak element (smallest in min-heap)letpeakElement=top(minHeap);console.log("Peak element:",peakElement);
Output
Peak element: 13
Heapify - O(n) Time and O(log n) Space
The heapify operation is used to place an element in its correct position within the heap so that the heap property is maintained. A heapify operation can also be used to create a min heap from an unsorted array. This is done by starting at the last non-leaf node and repeatedly performing the "heapify down" operation until all nodes satisfy the heap property.
C++
#include<iostream>#include<vector>usingnamespacestd;voidheapify(vector<int>&arr,inti,intn){intsmallest=i;intl=2*i+1;intr=2*i+2;// If left child exists and is smaller than rootif(l<n&&arr[l]<arr[smallest])smallest=l;// If right child exists and is smaller than smallest so farif(r<n&&arr[r]<arr[smallest])smallest=r;// If smallest is not root, //swap and continue heapifyingif(smallest!=i){swap(arr[i],arr[smallest]);// Recursively heapify heapify(arr,smallest,n);}}intmain(){vector<int>arr={2,3,10,4,5,1};cout<<"Original array: ";for(inti=0;i<arr.size();i++)cout<<arr[i]<<" ";// Build min-heap: perform heapify from last//non-leaf node up to rootfor(inti=arr.size()/2-1;i>=0;i--)heapify(arr,i,arr.size());// Print array after min-heapifycout<<"\nMin-Heap after heapify operation: ";for(inti=0;i<arr.size();i++)cout<<arr[i]<<" ";return0;}
Java
importjava.util.ArrayList;importjava.util.Arrays;publicclassGFG{publicstaticvoidheapify(int[]arr,inti,intn){intsmallest=i;intl=2*i+1;intr=2*i+2;// If left child exists and is smaller than rootif(l<n&&arr[l]<arr[smallest])smallest=l;// If right child exists and is smaller than smallest so farif(r<n&&arr[r]<arr[smallest])smallest=r;// If smallest is not root, swap and continue heapifyingif(smallest!=i){inttemp=arr[i];arr[i]=arr[smallest];arr[smallest]=temp;// Recursively heapifyheapify(arr,smallest,n);}}publicstaticArrayList<Integer>buildMinHeap(int[]arr){intn=arr.length;// Build min-heapfor(inti=n/2-1;i>=0;i--)heapify(arr,i,n);// Convert array to ArrayList before returningArrayList<Integer>result=newArrayList<>();for(intx:arr)result.add(x);returnresult;}publicstaticvoidmain(String[]args){int[]arr={2,3,10,4,5,1};System.out.print("Original array: ");for(intx:arr)System.out.print(x+" ");// Build min-heap: perform heapify from last//non-leaf node up to rootArrayList<Integer>minHeap=buildMinHeap(arr);// Print array after min-heapifySystem.out.print("\nMin-Heap after heapify operation: ");for(intx:minHeap)System.out.print(x+" ");}}
Python
defheapify(arr,i,n):smallest=il=2*i+1r=2*i+2# If left child exists and is smaller than rootifl<nandarr[l]<arr[smallest]:smallest=l# If right child exists and is smaller than smallest so farifr<nandarr[r]<arr[smallest]:smallest=r# If smallest is not root,# swap and continue heapifyingifsmallest!=i:arr[i],arr[smallest]=arr[smallest],arr[i]# Recursively heapifyheapify(arr,smallest,n)if__name__=="__main__":arr=[2,3,10,4,5,1]# Print original arrayprint("Original array: "," ".join(str(x)forxinarr))# Build min-heap: perform heapify from last non-leaf node up to rootforiinrange(len(arr)//2-1,-1,-1):heapify(arr,i,len(arr))print("Min-Heap after heapify operation: "," ".join(str(x)forxinarr))
C#
usingSystem;usingSystem.Collections.Generic;classGFG{staticvoidheapify(int[]arr,inti,intn){intsmallest=i;intl=2*i+1;intr=2*i+2;// If left child exists and is smaller than rootif(l<n&&arr[l]<arr[smallest])smallest=l;// If right child exists and is smaller than smallest so farif(r<n&&arr[r]<arr[smallest])smallest=r;// If smallest is not root, // swap and continue heapifyingif(smallest!=i){inttemp=arr[i];arr[i]=arr[smallest];arr[smallest]=temp;// Recursively heapify heapify(arr,smallest,n);}}staticList<int>buildMinHeap(int[]arr){intn=arr.Length;// Build min-heap: perform heapify from last// non-leaf node up to rootfor(inti=n/2-1;i>=0;i--)heapify(arr,i,n);// Convert array to List<int>List<int>result=newList<int>(arr);returnresult;}staticvoidMain(){int[]arr={2,3,10,4,5,1};Console.Write("Original array: ");for(inti=0;i<arr.Length;i++)Console.Write(arr[i]+" ");List<int>minHeap=buildMinHeap(arr);Console.Write("\nMin-Heap after heapify operation: ");for(inti=0;i<minHeap.Count;i++)Console.Write(minHeap[i]+" ");}}
JavaScript
functionheapify(arr,i,n){letsmallest=i;letl=2*i+1;letr=2*i+2;// If left child exists and is smaller than rootif(l<n&&arr[l]<arr[smallest])smallest=l;// If right child exists and is smaller than smallest so farif(r<n&&arr[r]<arr[smallest])smallest=r;// If smallest is not root, // swap and continue heapifyingif(smallest!==i){lettemp=arr[i];arr[i]=arr[smallest];arr[smallest]=temp;// Recursively heapify heapify(arr,smallest,n);}}// Driver codeletarr=[2,3,10,4,5,1];console.log("Original array: "+arr.join(" "));// Build min-heap: perform heapify from last// non-leaf node up to rootfor(leti=Math.floor(arr.length/2)-1;i>=0;i--){heapify(arr,i,arr.length);}console.log("Min-Heap after heapify operation: "+arr.join(" "));
Output
Original array: 2 3 10 4 5 1
Min-Heap after heapify operation: 1 3 2 4 5 10
Min-Heap Vs Max-Heap
Applications of Min-Heap Data Structure
Heap Sort: Min-heap is used to implement heap sort, an efficient O(n log n) sorting algorithm.
Priority Queue: Min-heap ensures the smallest element is always at the root, enabling efficient priority queue operations.
Dijkstra’s Algorithm: Min-heap stores vertices by minimum distance for efficient shortest-path computation.
Huffman Coding: Min-heap implements a priority queue to build optimal prefix codes.
Merge K Sorted Arrays: Min-heap efficiently merges K sorted arrays into one sorted array.
Advantages of Min-heap Data Structure
Fast Insertion and Deletion: Supports insertion and removal in O(log n) time, maintaining heap property efficiently.
Quick Access to Minimum: The smallest element is always at the root, retrievable in O(1) time.
Compact Storage: Can be implemented using arrays, saving memory compared to pointer-based structures.
Heap-based Sorting: Enables efficient sorting algorithms like heap sort with O(n log n) complexity.
Wide Applicability: Used in CPU/job scheduling (priority scheduling), graph algorithms like Dijkstra’s shortest path and Prim’s MST, and data compression like Huffman coding.