Lecture notes · Algorithms · Lecture 6

Binary Trees, Part 1

MIT 6.006 Introduction to Algorithms, lecture 6, 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 binary trees, a data structure built from nodes with parent and left and right pointers, and explains why they can reach any item in only a few steps.

Key ideas

Words to know

node
One box in the structure. It holds an item and pointers to nearby boxes.
root
The top node of the tree. It is the only node with no parent.
depth
How many pointers you must follow from the root to reach a certain node.
subtree
A smaller tree inside a bigger one. It is one node plus all the nodes below it.

Example

Think of a family tree that starts with one grandparent at the top. Each person has a parent link going up and can have children going down, so you can trace a path from the grandparent to any great-grandchild in just a few steps. A single long line of descendants would take many more steps to reach the last person.

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

Part 2 · 10:00–20:00

This part explains how to measure how deep or tall a node is in a tree, and how a tree can be read in a set order.

Key ideas

Words to know

ancestor
Any node above a given node on the path up to the root. A node has only one parent, so its ancestors form a single chain going up.
leaf
A node that has no children. It sits at the bottom end of the tree.
subtree
A smaller tree inside a bigger one. It is one node plus everything below it, and we ignore the rest of the tree when we look at it.
depth
How far a node sits below the root, counted as the number of edges from the node up to the root.

Example

Think of the folders on a computer. A folder inside another folder inside the main folder is deeper, and its depth is how many folder steps you climb to reach the top. The height of a folder is how many levels of folders you must go down before you reach a folder with nothing inside it.

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

Part 3 · 20:00–30:00

This part shows how to find the first node in a subtree and the next node after any given node, by moving left, right, or up through the tree.

Key ideas

Words to know

traversal order
The fixed sequence you get when you visit every node in a tree by a set rule. It is like a numbered list of the nodes.
subtree
A smaller tree inside a bigger one. It is a node plus all the nodes below it, like a branch with its twigs.
successor
The node that comes right after a given node in the traversal order. It is the next item in the list.
ancestor
Any node above a given node on the path up to the top of the tree. A node's parent, its parent's parent, and so on are all its ancestors.

Example

Think of a filing system where folders sit in a tree, and you list them in a fixed order. To find the folder that comes right after a folder, you check whether it has sub-folders inside it. If it does, the next folder is the first one inside it. If it does not, you climb back up until you come out of a branch that you entered from the left, and the folder you reach is the next one.

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

Part 4 · 30:00–40:00

This part explains how to find the next node in a tree's order and how to insert a new node into that order by changing the tree's links.

Key ideas

Words to know

traversal order
The order you visit every node when you go through the tree in a set pattern, like reading a book from start to finish.
successor
The node that comes right after a given node in the traversal order.
subtree
A smaller tree that sits inside a bigger tree, starting from one node and including everything below it.
height
How many steps you have to go down from the top of the tree to reach the lowest node.

Example

Think of a set of sticky notes where each note says who is next in line, and you read the notes by following the pointers. Adding a new person only means rewriting one or two notes, so the change is quick. Rewriting a whole printed list each time someone joins would take much longer.

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

Part 5 · 40:00–50:50

This part explains how to delete a node from a binary tree without breaking the tree apart, and how to search a tree for a key.

Key ideas

Words to know

traversal order
The order you visit every node when you walk through the whole tree in a set pattern. The lesson uses it to decide what order the items should be in.
predecessor
The item that comes right before a given item in the traversal order. The lesson uses it as a stand-in when deleting a node that has children.
successor
The item that comes right after a given item in the traversal order. It is the mirror of predecessor.
binary search tree property
The rule that keys on the left of a node are smaller than that node's key, and keys on the right are bigger. It is what makes searching a tree work like binary search.

Example

Think of people standing in a line in order of their height. If the person in the middle leaves, you cannot just leave a gap, because the line would be broken, so the person just before them can step into their spot and the line stays in order. Deleting a node in the tree works the same way: its neighbor in the order moves into its place.

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

← Lecture 4: Hashing · Lecture 7: Binary Trees, Part 2: AVL →

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