What if that’s the only operation you care about — always getting the min (or max) quickly?
What if that’s the only operation you care about — always getting the min (or max) quickly?
That’s the problem a heap solves.
Note: this heap data structure is unrelated to the heap memory in Java where objects are allocated – same word, different thing.
The heap property is simpler than BST:
No ordering between siblings, no left-vs-right rule. This makes heaps less “sorted” than a BST, but much faster for one specific job: always knowing the min/max in O(1)
Every parent is ≤ its children → the root is always the minimum
Every parent is ≥ its children → the root is always the maximum
The shape rule:
We will be implementing an array-based complete binary tree
Go to gradescope and answer the heap or not heap exercise
A complete binary tree maps perfectly onto an array:
10
/ \
15 20
/ \
30 40
Index: 0 1 2 3 4
Value: [10, 15, 20, 30, 40]
No pointers needed. This is why heaps are almost always implemented as arrays, not as linked nodes — it’s faster and uses less memory.
Index: 0 1 2 3 4
Value: [10, 15, 20, 30, 40]
For a node at index i:
Note: in Java, (0 - 1) / 2 is 0, so the root would be its own “parent” – that’s why we need a hasParent check
Index: 0 1 2 3 4
Value: [10, 15, 20, 30, 40]
15 is at index 1: Left child of 15 is at \(2 * 1 + 1 = 3\) (value 30)
Index: 0 1 2 3 4
Value: [10, 15, 20, 30, 40]
15 is at index 1: Right child of 15 is at \(2 * 1 + 2 = 4\) (value 40)
Index: 0 1 2 3 4
Value: [10, 15, 20, 30, 40]
15 is at index 1: Parent of 15 is at \((1 - 1) / 2 = 0\) (value 10)
Index: 0 1 2 3 4
Value: [10, 15, 20, 30, 40]
Core operations:
Insert and pop are \(O(\log n)\) because the tree is always complete — it never gets unbalanced like a BST can.
Add at the end, then “bubble up”:
Let’s insert 2 in our min heap:
Index: 0 1 2 3 4
Value: [10, 15, 20, 30, 40]
Index: 0 1 2 3 4 5
Value: [10, 15, 20, 30, 40, 2]
Let’s insert 2 in our min heap:
Let’s insert 2 in our min heap:
Index: 0 1 2 3 4 5
Value: [10, 15, 20, 30, 40, 2]
Index: 0 1 2 3 4 5
Value: [10, 15, 2, 30, 40, 20]
Index: 0 1 2 3 4 5
Value: [10, 15, 2, 30, 40, 20]
Index: 0 1 2 3 4 5
Value: [2, 15, 10, 30, 40, 20]
Index: 0 1 2 3 4 5
Value: [2, 15, 10, 30, 40, 20]
Take the root, then “bubble down”:
Index: 0 1 2 3 4 5
Value: [2, 15, 10, 30, 40, 20]
Index: 0 1 2 3 4
Value: [20, 15, 10, 30, 40]
Index: 0 1 2 3 4
Value: [20, 15, 10, 30, 40]
Index: 0 1 2 3 4
Value: [20, 15, 10, 30, 40]
Index: 0 1 2 3 4
Value: [10, 15, 20, 30, 40]
What if we had swapped 20 with 15 (the larger child) instead?
Index: 0 1 2 3 4
Value: [15, 20, 10, 30, 40]
Index: 0 1 2 3 4
Value: [10, 15, 20, 30, 40]
Index: 0 1 2 3 4
Value: [10, 15, 20, 30, 40]
┌────────────────────┬──────────────────────────────┬────────────────────┐
│ │ BST │ Heap │
├────────────────────┼──────────────────────────────┼────────────────────┤
│ Find min/max │ O(log n) average, O(n) worst │ O(1) always │
├────────────────────┼──────────────────────────────┼────────────────────┤
│ Insert │ O(log n) average, O(n) worst │ O(log n) always │
├────────────────────┼──────────────────────────────┼────────────────────┤
│ Remove min/max │ O(log n) average, O(n) worst │ O(log n) always │
├────────────────────┼──────────────────────────────┼────────────────────┤
│ Search for a value │ O(log n) average, O(n) worst │ O(n) — not its job │
├────────────────────┼──────────────────────────────┼────────────────────┤
│ Implementation │ Linked nodes │ Array │
├────────────────────┼──────────────────────────────┼────────────────────┤
│ Shape guarantee │ None │ Always complete │
└────────────────────┴──────────────────────────────┴────────────────────┘
Here are the helper methods we need to implement in MinHeap (besides all methods in the Heap interface):
Methods to get indices of left and right child, and parent:
Here are the helper methods we need to implement in MinHeap (besides all methods in the Heap interface):
Methods to check if there is a parent, a left or right child:
Here are the helper methods we need to implement in MinHeap (besides all methods in the Heap interface):
Let’s implement insert(T value)
data.length (can throw error, or expand array)Implement toString() to check your insertion. Submit your Heap.java and MinHeap.java (with insert, peek, isEmpty, length methods) to gradescope
Implement toString() to check your insertion. Submit your Heap.java and MinHeap.java to gradescope
public static void main(String[] args) {
MinHeap<Integer> heap = new MinHeap<>(10);
heap.insert(40);
heap.insert(15);
heap.insert(30);
heap.insert(5);
heap.insert(10);
heap.insert(20);
heap.insert(0);
heap.insert(2);
System.out.println(heap);
}Expected output: [ 0 2 5 10 15 30 20 40 ]
Let’s implement T pop()
Submit your Heap.java and MinHeap.java to gradescope
Submit your Heap.java and MinHeap.java to gradescope
public static void main(String[] args) {
MinHeap<Integer> heap = new MinHeap<>(10);
heap.insert(40);
heap.insert(15);
heap.insert(30);
heap.insert(5);
heap.insert(10);
heap.insert(20);
heap.insert(0);
heap.insert(2);
System.out.print("After inserts: ");
System.out.println(heap);
System.out.println("\nRemoving in order:");
while (!heap.isEmpty()) {
System.out.println(heap.pop());
}
}private void bubbleDown(int i) {
while (hasLeftChild(i)) {
int smallerChild = leftChild(i);
// find the smaller of the two children
if (hasRightChild(i) && data[rightChild(i)].compareTo(data[leftChild(i)]) < 0) {
smallerChild = rightChild(i);
}
if (data[i].compareTo(data[smallerChild]) <= 0) return;
swap(i, smallerChild);
i = smallerChild;
}
}You have 10 minutes to complete the quiz.
Your tree in (b) should be based on your tree in (a) – meaning, start with your tree in (a) then delete the specified node and then add the other specified node