By Vivek - October 6, 2026

Min Heap Heapify - A Worked Example With Every Swap

Bottom-up heapify turns an unordered array into a binary heap by repairing parent-child relationships from the last parent towards the root. For a min heap, a parent must be no larger than either child.

Open the Heap Visualizer, choose Min heap, paste [9, 4, 7, 1, 3, 6, 2], and click Build heap. This guide follows the exact swaps used by the tool.

Find the last parent first

With zero-based array indexes, a node at index i has children at 2i + 1 and 2i + 2. With seven values, the last parent is at floor(7 / 2) - 1 = 2. Indexes 3 through 6 are leaves and need no repair of their own.

Start at index 2 and work backwards through indexes 1 and 0. When a parent is too large, swap it with its smaller child. Then continue downwards from its new position.

Trace all four swaps

StepParent and childrenActionArray after the action
StartInput arrayNo swaps yet[9, 4, 7, 1, 3, 6, 2]
1Index 2: parent 7, children 6 and 2Swap 7 with 2[9, 4, 2, 1, 3, 6, 7]
2Index 1: parent 4, children 1 and 3Swap 4 with 1[9, 1, 2, 4, 3, 6, 7]
3Index 0: parent 9, children 1 and 2Swap 9 with 1[1, 9, 2, 4, 3, 6, 7]
4Index 1: parent 9, children 4 and 3Continue down; swap 9 with 3[1, 3, 2, 4, 9, 6, 7]

The final heap has root 1, children 3 and 2, and leaves 4, 9, 6 and 7. Check each parent against its children: 1 is no larger than 3 or 2; 3 is no larger than 4 or 9; 2 is no larger than 6 or 7.

The result is not a sorted array. The value 3 appears before 2, which is valid because siblings have no required order. For a fuller account of heap properties and bottom-up construction, see OpenDSA’s heaps and priority queues chapter.

Extract the minimum from this heap

Click Extract root on the built example. The tool removes 1 and moves the last value, 7, to the root:

[7, 3, 2, 4, 9, 6]

The smaller child of 7 is 2, so swap those values:

[2, 3, 7, 4, 9, 6]

At index 2, the remaining child is 6. Swap 7 with 6 to finish:

[2, 3, 6, 4, 9, 7]

The new minimum is 2. For a different exercise, rebuild the original example, enter 0 in Number, and click Insert. Trace 0 moving upwards until it becomes the root.

Share the exact diagram

Use Copy example link to preserve the current array, min/max mode and node highlights. Opening that link restores the diagram. It does not replay the operation history or include text you have pasted but not built. Anyone with the link can see its example values.

Use Download SVG for an image that stays sharp in slides or lesson notes. The export includes node indexes and a description of the level-order values. Blue marks the active node; amber marks nodes involved in the latest swap.

The visualizer accepts up to 31 whole numbers between -999 and 999. Try the same input in Max heap mode and predict which child each parent swaps with. For the difference between heap order and search-tree order, compare it with the Binary Search Tree Visualizer. For sorting steps, use the Sorting Algorithm Visualizer.