All chapters[•]Data StructuresChapter 05
Data Structures · Chapter 5

Trees and Binary Search TreesData Structures · Chapter 5

Folders, family trees and web pages are all trees. This chapter shows how to walk every item of a tree exactly once, how one extra rule turns a binary tree into a search structure whose search, insert and delete follow a single path, and why that path can still be as long as the whole tree when the keys arrive in the wrong order. That last problem is the reason for chapter 6.

Instructors: Boxi Yu · Yintong Huo · Python and Java versions (switch at the top) · Every program here has been run, and every output shown is the real output. · All chapters

Insert keys, watch the tree grow

Type or click keys to insert into an empty binary search tree; the search path is highlighted, the new leaf appears, and a live readout shows the height and the average depth. A button feeds the keys 10, 20, 30, … in sorted order and the tree turns into a chain.

Why this matters

about 90–120 minutes (4 videos of 7–9 minutes plus the exercises).

Trees are how hierarchical data is stored

File systems, expression parsers, web pages and the call tree of a recursive function are all trees, and almost every algorithm on them has the same shape: do something with the root and let recursion handle the subtrees.

One rule makes a tree searchable

If every key in the left subtree is smaller and every key in the right subtree larger, each comparison discards a whole subtree, so search, insert and delete cost time proportional to the height h.

Height is not guaranteed

The same keys give a tree of height about log₂ n or of height n − 1, depending on the insertion order; knowing why is the first step towards the balanced trees of chapter 6.

5 parts

Four videos with their notes, then the summary and practice. Read them in order.

01

Tree terms and traversals

A tree has a root, parents and children, leaves, depth and height. A binary tree is a root with two smaller binary trees, so every walk over it is "do something with the root and recurse on both subtrees". The three depth-first walks (preorder, inorder, postorder) differ only in when the root is visited, level order uses a queue instead of a stack, and the order of the walk changes the result.

rootleafdepthheightpreorderinorderpostorderlevel orderexpression tree
Open →
02

BST search and insert

A sorted array searches in log n but inserts in n; a linked list inserts in constant time but searches in n. A binary search tree (BST) adds one rule to a binary tree, "every key in the left subtree is smaller and every key in the right subtree is larger", and then search, insert and failed search all follow the same single path from the root. Their cost is proportional to the height h of the tree, O(h).

BST propertysearch pathinsert as a leafcost O(h)inorder is sorted
Open →
03

BST delete

Insertion always adds a leaf, but deleting a key from the middle of the tree must keep the ordering rule. There are three cases by the number of children: a leaf is cut off, a node with one child is replaced by that child, and a node with two children takes the key of its in-order successor, the smallest key of its right subtree, whose node is then removed. All three cost O(h).

leafone childtwo childrenin-order successorO(h)
Open →
04

Why BSTs degenerate

Every BST operation costs O(h), but h depends on the insertion order, not just on n. Keys inserted in increasing order build a chain of height n − 1, a linked list in disguise; keys in random order keep the tree short (average depth about 2 ln n). Real data is often sorted or nearly sorted, so we need trees that keep their height small by themselves.

sorted insertionchainheight n − 1random insertionaverage depthbalance
Open →
05

Summary and practice

A one-page map of the chapter, five self-check questions, ten exercises with solutions, and a way to test claims an AI makes about complexity.

self-checkexercises
Open →

How to use these notes

Watch · Predict · Step through · Practise

STEP 1

Watch

Each part starts with its video, 8–10 minutes, in Python or Java.

STEP 2

Predict

Answer the amber Predict box before you open the answer.

STEP 3

Step through

The interactive demos are being added; each one is described where it will appear.

STEP 4

Practise

Finish with the self-check and the exercises, with templates in your language.