{{ summaryTitle }}
{{ summaryValue }}

{{ summaryLine }}

Heap{{ resultsReady ? computation.values.heap_kind_label : '—' }} Frame{{ resultsReady ? `${traceFrameCursor + 1}/${traceRows.length}` : '—' }} Property{{ resultsReady && computation.values.invariant_ok ? 'Verified' : '—' }}

{{ primaryCopyAnnouncement }}

Heap tree unavailable.

Heap simulation inputs
The selected priority rule applies to build, insert, peek, and extraction.
Floyd's bottom-up algorithm turns this array into a complete binary heap.
Examples: insert 2, peek, and extract.
{{ resultsReady ? `${traceFrameCursor + 1} of ${traceRows.length}` : '—' }}
{{ selectedTraceFrame ? selectedTraceFrame.action : 'Enter valid heap inputs to inspect the trace.' }}
Off keeps the trace concise; on exposes every comparison before a swap or stop.
{{ include_comparisons ? 'Comparison frames included' : 'Concise trace' }}
{{ row.label }}
{{ row.display }}
[{{ index }}]{{ value }}
The final heap is empty.
Heap method:
Zero-based indices preserve complete-tree shape while local sift operations restore priority.
children(i) = 2i + 1, 2i + 2 · parent(i) = floor((i − 1) / 2)
  1. Build bottom-up from the last non-leaf with sift-down.
  2. Insert at the next leaf, then sift toward the root.
  3. Extract the root, move the last leaf to index 0, then sift down.
{{ methodCopyAnnouncement }}
{{ chartExportStatus }}

The chart renderer is unavailable. The trace ledger still contains every comparison and swap count.

FrameOperationActionHeap arrayRootComparisonsSwapsPropertyCopy
{{ row.frame }}{{ row.operation }}{{ row.action }}{{ row.heap }}{{ row.root }}{{ row.comparisons }}{{ row.swaps }}{{ row.invariant }}
{{ ledgerExportStatus }}

Introduction:

A priority queue needs fast access to the value with highest priority, not a complete sort of every value. A binary heap provides that access by combining a complete binary tree shape with a local parent-child rule. In a min heap, every parent is no greater than its children; in a max heap, every parent is no less than its children.

The complete shape means levels fill from left to right with no gaps, so the tree fits naturally in an array. The root occupies index 0. Parent and child positions follow directly from the index, and no pointers are needed to preserve the shape.

What a binary heap guarantees and does not guarantee
Heap factPractical meaning
The root has priority over every descendant.Peek can read the minimum or maximum immediately.
Each parent has priority over its own children.Local swaps can repair the structure after an update.
Siblings and separate branches have no ordering requirement.The array is a valid heap, not a sorted list.
The shape stays complete.Insertion uses the next array position and extraction fills the root from the last leaf.

Building, inserting, and extracting all depend on a sift path. Sift-down moves a misplaced parent toward the leaves; sift-up moves a newly appended value toward the root. Each comparison asks only whether the local heap rule is satisfied. Once the rule holds, that path can stop.

Duplicate values are valid, and several array layouts may represent valid heaps for the same multiset. A final root confirms the current priority value, but it does not show insertion order or prove that the remaining array is globally sorted.

How to Use This Tool:

Choose the priority direction, provide an initial array, and queue only the operations whose structural effect you want to inspect.

  1. Select a min heap for the smallest root or a max heap for the largest root.
  2. Enter 1 to 15 comma- or space-separated integers from −99 to 99 as Initial values. They are heapified with Floyd's bottom-up method.
  3. Add up to 12 operations, one per line: insert 2, peek, or extract. Insertion cannot take the heap beyond 15 nodes.
  4. Scrub the Trace frame to follow the array and tree together. Turn on Include comparison frames when you need to see each comparison before a swap or stop; it does not change the final heap.
  5. Confirm the final root and Heap property, then use the trace ledger to see which operation caused each comparison and swap.

Interpreting Results:

Verified means every parent-child pair in the final array satisfies the selected min-heap or max-heap rule. It does not mean the array is sorted. Read the root as the next priority value and treat the remaining positions as a complete tree whose branches are only locally ordered.

Comparison and swap totals cover the bottom-up build plus the queued operations. Enabling comparison frames makes the trace longer but leaves those totals and the final heap unchanged. Equal-priority child choices keep the left child, which makes the trace repeatable when duplicates occur.

Peeking at an empty heap returns an empty result without changing the structure. Extracting from an empty heap is also recorded without failure of the heap invariant; there is simply no root to remove.

Technical Details:

Zero-based array indices encode the complete-tree shape. These position formulas identify the only relatives that a sift operation needs to inspect.

Formula Core:

lefti=2i+1,righti=2i+2 parenti=i-12for i > 0

For example, array index 3 has parent floor((3 − 1) / 2) = 1. Index 1 has children 3 and 4 when those positions exist.

Rule Core:

Binary heap operation rules
OperationStructural changeRepair rule
BuildRead values as a complete tree and start at the last non-leaf.Sift each parent down, moving toward index 0.
InsertAppend at the next leaf position.Sift up while the child has priority over its parent.
PeekRead index 0 without mutation.No repair is needed.
Extract rootRemove index 0 and move the last leaf into that position.Sift down through the priority child until the rule holds.

For a min heap, a candidate has priority when it is numerically smaller; for a max heap, when it is larger. Strict comparison means equal values do not swap. When both children exist, they are compared first, and an equal pair keeps the left child.

Worked Examples:

Insert and extract in a min heap

Starting values 9, 4, 7, 1 build into the valid min-heap array [1, 4, 7, 9]. Inserting 2 appends it at index 4 and swaps it with parent 4, giving [1, 2, 7, 9, 4]. Extracting root 1 moves the last leaf to index 0 and sifts it below 2, leaving [2, 4, 7, 9].