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
| Step | Parent and children | Action | Array after the action |
|---|---|---|---|
| Start | Input array | No swaps yet | [9, 4, 7, 1, 3, 6, 2] |
| 1 | Index 2: parent 7, children 6 and 2 | Swap 7 with 2 | [9, 4, 2, 1, 3, 6, 7] |
| 2 | Index 1: parent 4, children 1 and 3 | Swap 4 with 1 | [9, 1, 2, 4, 3, 6, 7] |
| 3 | Index 0: parent 9, children 1 and 2 | Swap 9 with 1 | [1, 9, 2, 4, 3, 6, 7] |
| 4 | Index 1: parent 9, children 4 and 3 | Continue 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.
Related Posts
- Compare JSON Arrays by ID Without Reorder Noise
- Find and Fix Common JSON Syntax Errors
- Generate JSON Schema from a Sample API Response
- Compare API JSON Responses While Ignoring Timestamps
- Filter a Nested API Response with JSONPath
- Deploy SvelteKit to Firebase Hosting (Static Guide)
- How to Install Node.js and NPM on Ubuntu
- AWS Lambda Function with Response Streaming using Node.js
- Bootstrap 5 in SvelteKit: Install and Import CSS
- How to fix Nginx 403 Forbidden Error