What a heap is
25 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)A heap is a specialized complete binary tree that maintains extreme values at the root efficiently.
Min heap and max heap invariants
A heap is a complete binary tree where every parent is smaller than its children (min-heap) or larger (max-heap), so the smallest or largest element is always at the top.
A heap is a tree where each parent is ordered against its children. In a min-heap the smallest value sits at the root; in a max-heap the largest does.
import java.util.*;
public class HeapRoots { public static void main(String[] args) { PriorityQueue<Integer> minHeap = new PriorityQueue<>(); PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder()); for (int x : new int[] {7, 2, 9, 4}) { minHeap.offer(x); maxHeap.offer(x); } System.out.println("min-heap root: " + minHeap.peek()); System.out.println("max-heap root: " + maxHeap.peek()); }}min-heap root: 2 max-heap root: 9
In a min-heap every parent is smaller than or equal to both children; in a max-heap it is larger than or equal. Reading the root costs O(1), while finding the minimum of an unsorted array costs O(n) and keeping a sorted array costs O(n) per insert.
A heap does not order siblings: a left child can be larger than, smaller than or equal to its right sibling. Only the path from the root down to a leaf is ordered.
Complete binary tree stored inside an array
A complete binary tree fills every level except possibly the last, and the last level is packed from the left with no gaps. That shape lets us store the tree in a plain array, level by level.
// 2 level 0: index 0// / \// 5 3 level 1: indexes 1, 2// / \ /// 9 6 4 level 2: indexes 3, 4, 5int[] heap = {2, 5, 3, 9, 6, 4}; // stored level by level, left to rightThe root is at index 0, the next level at indexes 1 and 2, the third level at indexes 3 to 6. There are no node objects or pointers, so memory use is small.
Each tree level is one contiguous slice of the array, which also makes reading it cache friendly.
Parent and child index calculations
We navigate between parents and children using simple zero-based arithmetic formulas. For any node at index i, its left child resides at index 2 * i + 1. Its right child resides at index 2 * i + 2.
Example 1 · Computing Parent and Child Indexes
public class Main { static int leftChild(int i) { return 2 * i + 1; } static int rightChild(int i) { return 2 * i + 2; } static int parent(int i) { return (i - 1) / 2; } static boolean isLeaf(int i, int n) { return leftChild(i) >= n; }
public static void main(String[] args) { int n = 7; for (int i = 0; i < n; i++) { int left = leftChild(i); int right = rightChild(i); int p = (i == 0) ? -1 : parent(i); boolean leaf = isLeaf(i, n); System.out.println("Node " + i + ": parent=" + p + ", left=" + left + ", right=" + right + ", leaf=" + leaf); } }}Node 0: parent=-1, left=1, right=2, leaf=false Node 1: parent=0, left=3, right=4, leaf=false Node 2: parent=0, left=5, right=6, leaf=false Node 3: parent=1, left=7, right=8, leaf=true Node 4: parent=1, left=9, right=10, leaf=true Node 5: parent=2, left=11, right=12, leaf=true Node 6: parent=2, left=13, right=14, leaf=true
What this code does
- Root at index 0 has children at indexes 1 and 2.
- Nodes 3, 4, 5, and 6 have child indexes exceeding array length 7, so they are leaves.
- Parent computation uses integer division to round down correctly.
Example 2 · Checking Min-Heap Invariant on an Array
public class Main { static boolean isMinHeap(int[] arr) { int n = arr.length; for (int i = 0; i <= (n - 2) / 2; i++) { int left = 2 * i + 1; int right = 2 * i + 2; if (left < n && arr[i] > arr[left]) return false; if (right < n && arr[i] > arr[right]) return false; } return true; }
public static void main(String[] args) { int[] validHeap = {10, 15, 20, 17, 25, 30, 40}; int[] invalidHeap = {10, 25, 20, 12, 30, 35, 40};
System.out.println("validHeap is min-heap: " + isMinHeap(validHeap)); System.out.println("invalidHeap is min-heap: " + isMinHeap(invalidHeap)); }}validHeap is min-heap: true invalidHeap is min-heap: false
What this code does
- The loop checks all internal nodes up to (n - 2) / 2.
- In validHeap, every parent is less than or equal to both children.
- In invalidHeap, parent 25 at index 1 is greater than child 12 at index 3.
Example 3 · Checking Max-Heap Invariant on an Array
public class Main { static boolean isMaxHeap(int[] arr) { int n = arr.length; for (int i = 0; i <= (n - 2) / 2; i++) { int left = 2 * i + 1; int right = 2 * i + 2; if (left < n && arr[i] < arr[left]) return false; if (right < n && arr[i] < arr[right]) return false; } return true; }
public static void main(String[] args) { int[] heap = {90, 80, 70, 50, 60, 40, 30}; System.out.println("heap is max-heap: " + isMaxHeap(heap)); }}heap is max-heap: true
What this code does
- In a max-heap, every parent must be greater than or equal to both its children.
- Root 90 is greater than 80 and 70.
- Parent 80 is greater than 50 and 60, while parent 70 is greater than 40 and 30.
To move upward to the parent from index i, compute (i - 1) / 2 using integer division. For example, index 4 has its parent at (4 - 1) / 2, which equals index 1. Index 1 has its parent at (1 - 1) / 2, which equals index 0. We detect leaf nodes by checking child bounds. When child index 2 * i + 1 equals or exceeds n, the node is a leaf. All nodes from index n / 2 to n - 1 are guaranteed to be leaves.
Predict the output
Predict the index
A heap is stored in an array starting at index 0. Pick what each snippet prints.
Round 1 of 3
Next step
Check your understanding
10 quick questions