max min heap
max min heap
Heap Definition
Heap: A heap is a special type of binary tree that satisfies the heap property. There
are two main types:
Min-Heap: In a min-heap, the key at the root node is the smallest among all
keys in the binary tree. The same property must be recursively true for all
sub-trees.
Max-Heap: In a max-heap, the key at the root node is the largest among all
keys in the binary tree. The same property must be recursively true for all
sub-trees.
2. Usage
Min-Heap: Useful for implementing priority queues where you need quick
access to the smallest element. Commonly used in algorithms like Dijkstra's
shortest path.
Max-Heap: Useful for implementing priority queues where you need quick
access to the largest element. Commonly used in algorithms like heap sort.
Min-Heap
1. Insertion
java
Copy code
import java.util.PriorityQueue;
minHeap.add(10);
minHeap.add(4);
minHeap.add(7);
java
Copy code
import java.util.PriorityQueue;
minHeap.add(10);
minHeap.add(4);
minHeap.add(7);
minHeap.add(10);
minHeap.add(4);
minHeap.add(7);
Max-Heap
1. Insertion
java
Copy code
import java.util.PriorityQueue;
import java.util.Comparator;
maxHeap.add(10);
maxHeap.add(4);
maxHeap.add(7);
System.out.println("Max Heap: " + maxHeap); // Output: [10, 4,
7]
}
}
java
Copy code
import java.util.PriorityQueue;
import java.util.Comparator;
maxHeap.add(10);
maxHeap.add(4);
maxHeap.add(7);
java
Copy code
import java.util.PriorityQueue;
import java.util.Comparator;
public class MaxHeapExample {
public static void main(String[] args) {
PriorityQueue<Integer> maxHeap = new
PriorityQueue<>(Comparator.reverseOrder());
maxHeap.add(10);
maxHeap.add(4);
maxHeap.add(7);
4. LeetCode Problems
java
Copy code
import java.util.*;
class MedianFinder {
private PriorityQueue<Integer> minHeap;
private PriorityQueue<Integer> maxHeap;
public MedianFinder() {
minHeap = new PriorityQueue<>();
maxHeap = new
PriorityQueue<>(Collections.reverseOrder());
}
java
Copy code
import java.util.PriorityQueue;
class Solution {
public int findKthLargest(int[] nums, int k) {
PriorityQueue<Integer> maxHeap = new PriorityQueue<>();
for (int num : nums) {
maxHeap.add(num);
}
for (int i = 1; i < k; i++) {
maxHeap.poll();
}
return maxHeap.peek();
}
}
These examples should give you a good starting point for working with heaps in
Java and applying them to problems.
1. PriorityQueue:
o Definition: A queue that orders its elements based on their priority.
By default, it behaves like a min-heap.
o Usage: Useful for efficiently accessing the smallest (min-heap) or
largest (max-heap) element.
o Example: PriorityQueue<Integer> minHeap = new
PriorityQueue<>();
2. Collections.reverseOrder():
o Definition: A comparator that provides a reverse order for sorting.
When used with a PriorityQueue, it creates a max-heap.
o Usage: Used to turn a min-heap into a max-heap by reversing the
order.
o Example: PriorityQueue<Integer> maxHeap = new
PriorityQueue<>(Collections.reverseOrder());
3. poll():
o Definition: Removes and returns the highest-priority element (in a
min-heap, this is the smallest element; in a max-heap, it is the largest).
o Usage: Used to retrieve and remove the root of the heap.
o Example: int min = minHeap.poll();
4. peek():
o Definition: Retrieves, but does not remove, the highest-priority
element.
o Usage: Used to view the root of the heap without altering the heap
structure.
o Example: int min = minHeap.peek();
5. offer():
o Definition: Adds an element to the heap.
o Usage: Used to insert a new element into the priority queue.
o Example: minHeap.offer(10);
6. add():
o Definition: Similar to offer(), it adds an element to the heap.
o Usage: Can be used interchangeably with offer() in PriorityQueue.
o Example: minHeap.add(10);
7. size():
o Definition: Returns the number of elements in the heap.
o Usage: Useful for checking the number of elements present.
o Example: int size = minHeap.size();
8. Comparator:
o Definition: An interface used to define a custom ordering for
elements.
o Usage: Allows customization of sorting order, such as reversing order
or sorting based on specific criteria.
o Example: Comparator<Integer> comp =
Collections.reverseOrder();
Examples in Context
Min-Heap Example
java
Copy code
import java.util.PriorityQueue;
minHeap.offer(10);
minHeap.offer(4);
minHeap.offer(7);
Max-Heap Example
java
Copy code
import java.util.PriorityQueue;
import java.util.Collections;
maxHeap.offer(10);
maxHeap.offer(4);
maxHeap.offer(7);
System.out.println("Max Heap: " + maxHeap); // Output: [10, 4,
7]
System.out.println("Removed Max Element: " +
maxHeap.poll()); // Output: 10
System.out.println("Max Heap after removal: " + maxHeap); //
Output: [7, 4]
}
}
These terms and methods are often used in LeetCode problems that involve heaps
or priority queues. Understanding them will help you effectively solve problems
that require heap operations.
4o mini