Lecture notes · Algorithms · Lecture 7

Binary Trees, Part 2: AVL

MIT 6.006 Introduction to Algorithms, lecture 7, 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 explains how binary trees store items in order, why a tree's height controls how fast it works, and why we need a tree that stays short.

Key ideas

Words to know

height
How far down the longest path goes from a node to the bottom, counting the lines (edges) you cross.
traversal order
The order you get when you visit every node in a tree: left side first, then the node, then the right side.
binary search tree
A tree where every key on the left of a node is smaller and every key on the right is bigger, so you can search it like a sorted list.
size
The number of nodes in a subtree, including the node at its top.

Example

Think of a sorted phone book. You open it in the middle, then go left or right, and each step cuts the choices in half. A short, balanced tree works the same way, so you find what you want in only a few steps.

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

Part 2 · 10:00–20:00

This part shows how a binary tree can find the item at any position, and how each node can store a size so that inserts keep the positions correct.

Key ideas

Words to know

subtree
A smaller tree that sits inside a bigger tree. It is a node together with everything below it.
traversal order
The fixed order in which you visit all the nodes of a tree, one after another. It gives every item a position in the sequence.
recurse
To call the same process again on a smaller piece of the problem, such as moving down into the left or right subtree.
constant time
A task that takes about the same short amount of time no matter how big the tree is.

Example

Think of a row of coats on a hanger rail where each hook also has a tag saying how many coats hang on the left side of it. To find coat number 7, you check the tags and move left or right, the way a binary search does, instead of counting from the first coat. This is only an analogy to help the idea click.

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

Part 3 · 20:00–30:00

This part explains how to keep useful information up to date in each subtree of a binary tree, and why only certain kinds of information can be kept up to date cheaply.

Key ideas

Words to know

leaf
A node in a tree that has no children below it.
ancestor
A node above another node on the path up to the root of the tree.
subtree property
A stored value for a node that depends only on that node and the nodes below it, so it can be updated by walking up the tree.
balanced binary tree
A binary tree whose height is about log n, so it stays short and fast to search, even as it grows.

Example

Think of a company org chart where each manager keeps a count of everyone in their team. When one new person joins at the bottom, only the managers above that person need to add one to their count, not everyone in the company. That is how a subtree property works: a change only affects the nodes above it.

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

Part 4 · 30:00–40:00

This part explains how rotations can rebalance a tree without changing its order, and how the AVL height-balance rule keeps the tree's height small.

Key ideas

Words to know

rotation
A small change to how nodes are linked, where one node moves up and its neighbor moves down. The order of the data stays the same.
traversal order
The order you get when you visit every node in the tree in a fixed walk. The lesson says this order must never change.
skew
The difference between the height of a node's right subtree and the height of its left subtree. In an AVL tree it must be -1, 0, or +1.
height
The longest downward distance from a node to a leaf, meaning a node with no children at the bottom of the tree.

Example

Think of a set of books that must be read in alphabetical order. You can stack them in one tall pile or spread them into a few short piles, as long as reading them left to right still gives the same order. A rotation changes the piles to a shape that is easier to search, and the reading order stays the same.

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

Part 5 · 40:00–50:00

This part shows why AVL trees stay balanced in height, and how storing each node's height lets the tree fix itself with rotations after an insert.

Key ideas

Words to know

subtree augmentation
Storing extra information in each node, such as its height, that depends only on that node's own subtree.
skew
The difference between the heights of a node's left and right subtrees. In an AVL tree, the skew must stay at minus 1, 0, or plus 1.
rotation
A small change to the pointers that moves one node up and another node down, keeping the tree in order while changing its shape.
ancestor
A node above another node on the path up to the root of the tree.

Example

Think of a company org chart where a new person joins a deep team. Only the managers on the chain of bosses above that person need to check whether their teams are still balanced. Managers in other branches do not need to look.

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

Part 6 · 50:00–54:03

This part explains how an AVL tree fixes itself when it leans too far, so that it stays balanced and fast to search.

Key ideas

Words to know

height balanced
Each node's two sides differ in height by at most 1, so no side is much taller than the other.
rotation
A small rearrangement of a few nodes that moves one node up and another down, while keeping the order of the values the same.
skew
How much a node leans. It tells you whether the left side or the right side is taller at that node.
augmentation
Extra information stored in each node, such as its height or the number of nodes below it, that must be kept correct when the tree changes.

Example

Think of a stack of books on a shelf that starts leaning. A small shift of a few books can straighten the stack, but sometimes a bad shift makes it lean harder, so you have to move books in two steps instead. You keep checking the stack from the bottom up until the whole thing stands straight.

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

← Lecture 6: Binary Trees, Part 1 · Lecture 8: Binary Heaps →

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