Priority Queues Hard
Min-Max Heap
Insert keys into an array-backed heap whose levels alternate between minimum and maximum order.
Trace input
Bounded JSONEdit the lesson input, then build a deterministic source-linked trace.
Source scenarios
Complexity
BestO(1) swim
AverageO(log n) insert
WorstO(log n) insert
SpaceO(n)
Array state
Follow the active comparisons and writes.
Current operation
Trace stateReady to explore
Source behavior & common mistakes
Preserve alternating min/max levels while swimming each inserted key, then retrieve the minimum and maximum using the source methods.
- Using min-heap rules at every level instead of alternating min and max levels.
- Checking a grandparent before the source's parent comparison, or ignoring its `else if` priority.
- Swapping on equality; the source comparisons are strict, and max returns child 3 on a tie.
Algorithm code
JavaMinMaxHeap.java (pinned repository source)Step linked
The highlighted line follows the current step.
Ready to begin
Press Play to see what happens.
Detailed execution
Snapshot 1 / 1