An implementation of an Advanced Priority Queue in Java with dynamic min-heap/max-heap toggling capabilities.
This project implements a feature-rich priority queue data structure that extends traditional heap functionality with advanced operations including:
- Dynamic heap mode switching between min-heap and max-heap
- Flexible key/value updates with automatic heap restructuring
- Efficient merging of multiple priority queues
- Position-based access via
peekAt()for k-th smallest/largest elements - Generic type support for comparable keys and arbitrary values
APQImplementation/
├── APQ.java # Main APQ implementation
├── PriorityQueue.java # Priority queue interface
├── Entry.java # Key-value entry interface
├── AbstractPriorityQueue.java # Abstract base class
├── HeapPriorityQueue.java # Standard heap implementation
├── DynamicArray.java # Resizable array structure
├── DefaultComparator.java # Default comparison logic
└── APQTest.java # Comprehensive test suite
insert(K key, V value)- O(log n) insertion with automatic heap maintenanceremoveTop()- O(log n) removal of min/max element depending on modetop()- O(1) access to highest priority elementtoggle()- O(n) switch between min-heap and max-heap modesmerge(APQ other)- O(n) merge two priority queues
replaceKey(Entry e, K newKey)- Update key with automatic repositioningreplaceValue(Entry e, V newValue)- Update value without affecting heap structureremove(Entry e)- Remove arbitrary entry from heappeekAt(int i)- Access i-th smallest (min-heap) or largest (max-heap) element
size()- Current number of elementsisEmpty()- Check if queue is emptystate()- Returns current mode ("min" or "max")
- Underlying storage: Custom
DynamicArray<K,V>with automatic resizing - Initial capacity: 10 elements (doubles when full)
- Heap property: Maintained through
upheap()anddownheap()operations
- Upheap: Restores heap property after insertion or key decrease
- Downheap: Restores heap property after removal or key increase
- Toggle: Rebuilds entire heap structure in O(n) time
APQ<Integer, String> minHeap = new APQ<>();
// Insert elements
minHeap.insert(10, "Ten");
minHeap.insert(5, "Five");
minHeap.insert(15, "Fifteen");
// Access minimum
System.out.println(minHeap.top().getKey()); // Output: 5
// Remove minimum
Entry<Integer, String> min = minHeap.removeTop();
System.out.println(min.getValue()); // Output: "Five"APQ<Integer, String> heap = new APQ<>();
heap.insert(25, "TwentyFive");
heap.insert(15, "Fifteen");
heap.insert(35, "ThirtyFive");
System.out.println(heap.state()); // Output: "min"
System.out.println(heap.top().getKey()); // Output: 15
heap.toggle();
System.out.println(heap.state()); // Output: "max"
System.out.println(heap.top().getKey()); // Output: 35APQ<Integer, String> heap = new APQ<>();
Entry<Integer, String> entry = heap.insert(50, "Fifty");
heap.insert(30, "Thirty");
// Replace key (triggers automatic repositioning)
heap.replaceKey(entry, 10);
System.out.println(heap.top().getKey()); // Output: 10APQ<Integer, String> heapA = new APQ<>();
heapA.insert(10, "Ten");
heapA.insert(20, "Twenty");
APQ<Integer, String> heapB = new APQ<>();
heapB.insert(5, "Five");
heapB.insert(15, "Fifteen");
heapA.merge(heapB);
System.out.println(heapA.size()); // Output: 4
System.out.println(heapA.top().getKey()); // Output: 5APQ<Integer, String> heap = new APQ<>();
heap.insert(50, "Fifty");
heap.insert(20, "Twenty");
heap.insert(70, "Seventy");
heap.insert(10, "Ten");
System.out.println(heap.peekAt(1).getKey()); // Output: 10 (1st smallest)
System.out.println(heap.peekAt(2).getKey()); // Output: 20 (2nd smallest)
System.out.println(heap.peekAt(3).getKey()); // Output: 50 (3rd smallest)The project includes comprehensive test cases (APQTest.java) covering:
- Basic insertion and removal operations
- Min-heap and max-heap modes
- Key and value replacement
- Entry removal from various positions
- Heap merging scenarios
- Array resizing with 25+ elements
- Edge cases (empty heap, single element)
- Stress testing with 50+ operations
- String keys for lexicographic ordering
Run tests:
javac APQImplementation/*.java
java APQImplementation.APQTest| Operation | Average Case | Worst Case |
|---|---|---|
| insert() | O(log n) | O(log n) |
| removeTop() | O(log n) | O(log n) |
| top() | O(1) | O(1) |
| replaceKey() | O(log n) | O(log n) |
| replaceValue() | O(1) | O(1) |
| remove() | O(log n) | O(log n) |
| toggle() | O(n) | O(n) |
| merge() | O(n) | O(n) |
| peekAt() | O(n) | O(n) |
Java 8 or higher
Najlaa Achouhal