A Min Heap is a complete binary tree where the value of each node is less than or equal to the values of its children. The root node contains the smallest element.
Min Heaps are commonly used in algorithms like Dijkstra's shortest path and Prim's minimum spanning tree.
Insert elements into the heap while maintaining the min heap property.
import java.util.PriorityQueue;
public class MinHeapExample {
public static void main(String[] args) {
PriorityQueue minHeap = new PriorityQueue<>();
minHeap.add(10);
minHeap.add(5);
minHeap.add(15);
minHeap.add(3);
System.out.println("Min Heap: " + minHeap);
}
}
Console Output:
Min Heap: [3, 5, 15, 10]
Remove the smallest element from the heap.
import java.util.PriorityQueue;
public class MinHeapExtract {
public static void main(String[] args) {
PriorityQueue minHeap = new PriorityQueue<>();
minHeap.add(10);
minHeap.add(5);
minHeap.add(15);
minHeap.add(3);
int minElement = minHeap.poll();
System.out.println("Extracted Minimum: " + minElement);
System.out.println("Min Heap after extraction: " + minHeap);
}
}
Console Output:
Extracted Minimum: 3
Min Heap after extraction: [5, 10, 15]
Decrease the value of a key and adjust the heap accordingly.
import java.util.PriorityQueue;
import java.util.Iterator;
public class MinHeapDecreaseKey {
public static void main(String[] args) {
PriorityQueue minHeap = new PriorityQueue<>();
minHeap.add(10);
minHeap.add(5);
minHeap.add(15);
minHeap.add(3);
// Decrease key by removing and adding
minHeap.remove(10);
minHeap.add(2);
System.out.println("Min Heap after decreasing key: " + minHeap);
}
}
Console Output:
Min Heap after decreasing key: [2, 3, 15, 5]
Construct a min heap from an existing array of elements.
import java.util.PriorityQueue;
public class MinHeapBuild {
public static void main(String[] args) {
int[] elements = {10, 5, 15, 3};
PriorityQueue minHeap = new PriorityQueue<>();
for (int element : elements) {
minHeap.add(element);
}
System.out.println("Built Min Heap: " + minHeap);
}
}
Console Output:
Built Min Heap: [3, 5, 15, 10]
Combine two min heaps into a single min heap.
import java.util.PriorityQueue;
public class MinHeapMerge {
public static void main(String[] args) {
PriorityQueue minHeap1 = new PriorityQueue<>();
minHeap1.add(10);
minHeap1.add(5);
PriorityQueue minHeap2 = new PriorityQueue<>();
minHeap2.add(15);
minHeap2.add(3);
minHeap1.addAll(minHeap2);
System.out.println("Merged Min Heap: " + minHeap1);
}
}
Console Output:
Merged Min Heap: [3, 5, 15, 10]
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies