Lecture notes · Algorithms · Lecture 8

Binary Heaps

MIT 6.006 Introduction to Algorithms, lecture 8, explained in plain English

Want this lecture to stick? In LessonBound you get checked practice questions for every part, can ask about anything you don't get, and can listen with your screen off. Free, no ads.

Learn this lecture free

Watch or read the original lecture: MIT OpenCourseWare.

Part 1 · 0:00–10:00

This part introduces the priority queue, a way to store items and always pull out the most important one, and shows why sorting with it is useful.

Key ideas

Words to know

priority queue
A collection of items where each item has a priority, and you can always take out the item with the highest priority next.
amortized
Averaged over many operations. A single step may be slow, such as resizing an array, but the average cost per step stays low.
in-place
Done using the same memory you already have, without making a big new copy of the data.
augmentation
Extra information added to each node of a tree so that a certain question, like finding the max, can be answered faster.

Example

Think of a hospital emergency room. Patients arrive at different times, but the doctor always treats the most urgent patient next, not the one who arrived first. A priority queue works the same way: items come in whenever they like, and you always take out the one with the highest priority.

3 checked practice questions for this part are in the app.

Part 2 · 10:00–20:00

This part explains how a binary heap stores items in a plain array while keeping the shape of a balanced tree, which lets it sort quickly without extra memory.

Key ideas

Words to know

complete binary tree
A tree where every level is full, except possibly the last. The last level is filled from the left with no gaps.
in place
Using only the memory that already holds the items, plus a few extra pointers or counters. No large second copy is made.
depth
How many steps a node is below the top of the tree. The top node is at depth 0.
bijection
A perfect matching where each item in one group pairs with exactly one item in the other group, with no leftovers on either side.

Example

Think of filling a theater row by row, from the front row to the back, and filling each row from left to right. Once you know how many people are coming, there is only one way to seat them. The seating chart can be written as a simple list, so you never need to draw the rows.

3 checked practice questions for this part are in the app.

Part 3 · 20:00–30:00

This part shows how a binary heap can be stored as a plain array, using index math instead of pointers, and what the max-heap rule guarantees about where the biggest item sits.

Key ideas

Words to know

implicit data structure
A way of organizing data where the structure comes from the position of each item in an array, so no extra links or pointers need to be stored.
complete binary tree
A tree where every level is full, except possibly the last, which is filled from left to right. Each node has at most two children.
max-heap property
The rule that every node must be greater than or equal to its children. It is a local rule about a node and its two children.
integer division
Dividing two whole numbers and throwing away the remainder, so 7 divided by 2 gives 3.

Example

Think of a row of numbered mailboxes, with box 0 at the top and boxes 1 and 2 below it, then 3 to 6 below those, and so on. If you know you are at box 1, you can work out that its two helpers are boxes 3 and 4 without any labels or signs pointing to them. The numbers themselves tell you the family links.

3 checked practice questions for this part are in the app.

Part 4 · 30:00–40:00

This part explains how a binary heap adds and removes its biggest item by working only at the end of an array and then moving one item up or down the tree.

Key ideas

Words to know

leaf
A node at the bottom of a tree that has no children below it.
complete binary tree
A tree where every level is full, except possibly the last, and the last level fills up from left to right with no gaps.
max-heap property
The rule that every parent is greater than or equal to its children, so the biggest item is always at the top.
recursive
A way of writing a process that repeats itself on a smaller piece each time, like moving up or down one level at a time.

Example

Think of a sports team ranked from best to worst, where each player must rank above anyone they coach. A new player joins at the bottom of the list, and if they are better than the player above, they swap places and keep moving up until the ranking is correct again. The same idea works in a heap, with one item moving up or down at a time.

3 checked practice questions for this part are in the app.

Part 5 · 40:00–50:42

This part shows how a max-heap fixes itself after its top item is removed, how heapsort sorts an array in place using that same method, and why building a heap from scratch can be done in linear time.

Key ideas

Words to know

max-heap property
The rule that every parent must be greater than or equal to each of its children.
in-place
Done inside the array you already have, without needing extra space for a new array.
prefix
The first part of an array, from the start up to some position.
heapsort
A sorting method that builds a max-heap, then repeatedly removes the largest item and places it at the end of the array.

Example

Imagine a company org chart where every boss must earn more than each person who reports to them. If a new hire is too senior for a junior spot, they swap down with the stronger of their two direct reports, and keep swapping until they settle into a spot that fits.

3 checked practice questions for this part are in the app.

← Lecture 7: Binary Trees, Part 2: AVL · Lecture 9: Breadth-First Search →

Notes written by AI (Claude) from the lecture's official transcript, so check anything important against the lecture. MIT OpenCourseWare · CC BY-NC-SA 4.0 · not affiliated with or endorsed by MIT. These notes are shared under the same license.

LessonBound · Free lecture notes · Privacy · Terms · New York