Priority Queues Hard
Linked-List Min-Heap
Insert and remove minima from a complete binary heap represented by linked nodes.
Trace input
Bounded JSONEdit the lesson input, then build a deterministic source-linked trace.
Source scenarios
Complexity
BestO(1)
AverageO(log n)
WorstO(log n)
SpaceO(n)
Array state
Follow the active comparisons and writes.
Current operation
Trace stateReady to explore
Source behavior & common mistakes
Trace the source's subtree-size-guided leaf placement, value-only swim and sink swaps, and last-layer removal while keeping parent and child pointers visible.
- The heap is a linked tree; createNodeInLastLayer chooses a position from child pointers and subtree sizes.
- The source exchanges values between fixed Node objects; parent, left, and right pointers do not move during swim or sink.
- Strict greater-than comparisons leave equal values in place, and sink selects the left child on a tie.
- removeLastNodeInLastLayer decrements each node size on the path before detaching the final leaf.
- min and delMin throw NoSuchElementException when the root is null.
Algorithm code
Javasorting/MinPQLinked.javaStep linked
The highlighted line follows the current step.
Ready to begin
Press Play to see what happens.
Detailed execution
Snapshot 1 / 1