Version: 0.6.22
Problem Set#
- Objectives:
Practice the concepts introduced in the Trees module
This problem set is optional practice. Work on it wherever you like, alone or in groups, and with any resources you find useful. Unless a problem says otherwise, answer with pen and paper or in pseudocode; implementation tasks can be done in any programming language.
Problems are roughly ordered by difficulty, from ★ (warm-up) to ★★★★ (challenging). The last two problems introduce topics that are not covered in the lectures: Each one starts with a short background that gives you everything you need.
Every problem comes with a solution, hidden in a dropdown below it. Give the problem a real try before you open it: Most of the learning happens while you are stuck.
Core Problems#
Exercise 1 (Counting on Recursion ★)
Recall from Lecture 5.1 that a tree
\(t = (r, c)\) is made of a root \(r\) and a sequence
\(c\) of disjoint children trees. Assume every node gives
access to node.children, the (possibly empty) sequence of its
children, as in the “sequence of children” representation. Consider
the tree shown on Fig. 2.
Fig. 2 A general tree with nine nodes#
Write recursive procedures for the following three operations:
size(node)returns the number of nodes in the subtree rooted atnode;height(node)returns the height of the subtree rooted atnode, where a single node has height 0;leaves(node)returns the number of leaves in the subtree rooted atnode.
Run
height(R)on the tree above. List the calls in the order they return, together with the value each of them returns.Look at the order you obtained in the previous question. Which traversal is it? Explain why all three operations end up visiting the nodes in this order.
How many calls to
sizedoessize(R)trigger in total, for a tree with \(n\) nodes? What is the time complexity ofsize? Justify your answer.
Solution to Exercise 1 (Counting on Recursion ★)
All three operations combine the answers of the children:
size(node): total = 1 for each child in node.children: total = total + size(child) return total height(node): tallest = -1 for each child in node.children: tallest = max(tallest, height(child)) return 1 + tallest leaves(node): if node.children is empty: return 1 total = 0 for each child in node.children: total = total + leaves(child) return totalStarting
heightwithtallest = -1mirrors the convention \(h(\varnothing) = -1\) from the AVL lecture, so a leaf returns 0 without a special case.The calls return in the following order:
Call
W
X
S
T
Z
Y
U
V
R
Returns
0
0
1
0
0
1
2
0
3
This is a post-order traversal. Each operation needs the answers of all the children before it can compute the answer of the node itself, so a node always returns after all its descendants.
size(R)triggers exactly \(n\) calls: The root is called once, and every other node is called exactly once, by its parent. Each call does a constant amount of work plus one loop iteration per child. Summed over all calls, the loops run once per edge, that is \(n - 1\) times. The total is therefore \(O(n)\).
Common mistakes
Counting nodes instead of edges, so that a leaf gets height 1 and
height(R)returns 4.Returning 0 from
leaveson a leaf, which makes every answer 0.Claiming \(O(n^2)\) or \(O(n \log n)\) “because there is a loop inside a recursion”. The loops are not nested over the same nodes: Each edge is followed exactly once.
Exercise 2 (One Tree, Two Encodings ★)
Lecture 5.1 shows a second way to store a general tree: Every node keeps only two references, one to its left-most child and one to its next sibling (see Fig. 1.25). If we read the first reference as a “left” child and the second as a “right” child, any general tree becomes a binary tree, where every node has at most two children.
Consider for instance the tree shown on Fig. 3. Node A has three children, so it is not a binary tree. In its encoding, shown on Fig. 4, A keeps a link to its first child B only. B links to its next sibling C, which in turn links to its own first child E and to its next sibling D.
Fig. 3 A general tree, where Node A has three children# |
Fig. 4 Its encoding as a binary tree. Solid arrows lead to the left-most child, dashed arrows to the next sibling.# |
The tree from Fig. 2 is not a binary tree either: Its root has four children.
Draw the binary tree that encodes the tree from Fig. 2. What is its height? Compare it with the height of the original tree.
List the nodes of both trees in pre-order. Then list the nodes of the general tree in post-order, and the nodes of the binary tree in in-order (left subtree, then the node itself, then the right subtree). What do you notice?
Explain why the post-order of a general tree always matches the in-order of its encoding.
Hint: Given a node \(v\), which nodes of the general tree end up in the left subtree of \(v\) in the encoding? Which end up in its right subtree?
A classmate reuses the
sizeprocedure for binary trees, where an empty tree has size 0:size(node): if node is null: return 0 return 1 + size(node.left) + size(node.right)They call it on Node S of the encoding. Does the result match the size of the subtree rooted at S in the general tree? Explain why
size(R)still returns the correct answer, then fix the procedure so that it returns the size of the general subtree for any node.
Solution to Exercise 2 (One Tree, Two Encodings ★)
Solid arrows lead to the left-most child (drawn as a left child), dashed arrows lead to the next sibling (drawn as a right child):
Fig. 5 The tree from Problem 1, encoded as a binary tree#
The binary tree has height 5 (along R, S, T, U, Y, Z), whereas the general tree has height 3. The encoding is never shorter: The depth of a node in the encoding is its depth in the general tree plus the number of sibling links on the way, so wide trees become tall binary trees.
Both pre-orders are identical: R, S, W, X, T, U, Y, Z, V.
The post-order of the general tree is W, X, S, T, Z, Y, U, V, R, and the in-order of the binary tree gives exactly the same sequence.
In the encoding, the left subtree of a node \(v\) holds exactly the descendants of \(v\) in the general tree (its first child, followed by that child’s siblings and all their descendants). Its right subtree holds the siblings that come after \(v\), together with their descendants.
The in-order visits the left subtree, then \(v\), then the right subtree. That is: All the descendants of \(v\), then \(v\) itself, then the next siblings. This is precisely the post-order rule: A node comes after all its descendants, and before its next siblings.
For the pre-order, the same reasoning gives \(v\), then its descendants, then its next siblings, which is the pre-order rule for general trees.
No.
size(S)returns 8 (S, W, X, T, U, Y, Z, V), whereas the subtree of S in the general tree has only 3 nodes (S, W, X). The right subtree of S holds its siblings, not its descendants.size(R)is correct by luck: The root has no sibling, so its right subtree is empty and only its descendants are counted.The fix is to count the node and its left subtree only:
generalSize(node): return 1 + size(node.left)
Here
sizeis the classmate’s procedure: Applied to the left-most child, it counts that child, its siblings, and all their descendants, which are exactly the descendants ofnode.
Common mistakes
Drawing every child as a left child of its parent (e.g., T and U as children of R). Only the first child hangs to the left: The others are chained to the right of their previous sibling.
Placing a single child in the middle, so that X looks like a left child of W. A right child must be visibly on the right.
Defining in-order for the general tree instead of the encoding. In-order only makes sense when a node has a “left” and a “right”.
Exercise 3 (Rebuilding from Traversals ★★)
A traversal turns a tree into a sequence. Can we go the other way, and rebuild a binary tree from its traversals? In this problem, all nodes carry different labels. Recall that the in-order visits the left subtree, then the node itself, then the right subtree (see Exercise 2).
A binary tree with nine nodes gives the following two sequences:
Pre-order: M, K, Q, A, J, P, T, R, L
In-order: Q, K, J, A, M, P, R, T, L
Rebuild the tree. Explain how you find the root, and how you decide which nodes belong to its left and right subtrees.
Turn your method into a recursive procedure
rebuild(preorder, inorder)that returns the root of the tree. What is its time complexity in the worst case? Which tree shape triggers this worst case?Does the pre-order alone determine the tree? How many different binary trees have the pre-order A, B, C? Draw them all.
Do the pre-order and the post-order together determine the tree? If not, find two different binary trees that share both sequences. Can you think of a family of binary trees for which these two sequences are enough?
Solution to Exercise 3 (Rebuilding from Traversals ★★)
The tree is the following:
Fig. 6 The tree rebuilt from its pre-order and in-order#
The pre-order always starts with the root, so the root is M. In the in-order, the root sits between its two subtrees: Everything on the left of M (Q, K, J, A) is the left subtree, and everything on its right (P, R, T, L) is the right subtree. The left subtree has four nodes, so the next four nodes of the pre-order (K, Q, A, J) are its pre-order, and the remaining ones (P, T, R, L) are the pre-order of the right subtree. We repeat on each side:
Pre-order
In-order
Root
Left
Right
M K Q A J P T R L
Q K J A M P R T L
M
Q K J A
P R T L
K Q A J
Q K J A
K
Q
J A
A J
J A
A
J
(empty)
P T R L
P R T L
P
(empty)
R T L
T R L
R T L
T
R
L
A possible procedure, using 0-based positions and slices that include their first index but exclude their last:
rebuild(preorder, inorder): if preorder is empty: return null root = preorder[0] i = position of root in inorder left = rebuild(preorder[1 .. i+1], inorder[0 .. i]) right = rebuild(preorder[i+1 .. n], inorder[i+1 .. n]) return new Node(root, left, right)Here \(i\) is also the size of the left subtree. Each call searches for the root in the in-order, which takes up to \(O(n)\) for a call on \(n\) nodes. In the worst case, the tree is a single long branch, every call peels off a single node, and the total work is \(n + (n-1) + \ldots + 1\), that is \(O(n^2)\).
Going further: Storing the position of every label of the in-order in a hash table, and passing indices instead of copying slices, brings it down to \(O(n)\).
No. Exactly five binary trees have the pre-order A, B, C:
Fig. 7 The five binary trees whose pre-order is A, B, C#
The pre-order tells us which node comes first, but not on which side each child hangs, nor whether two nodes are siblings or parent and child. In fact, every possible shape with three nodes appears exactly once (compare with the lab exercise on the possible shapes of a BST with three keys).
No. Consider a root A with a single child B. Whether B is the left or the right child, the pre-order is A, B and the post-order is B, A. Both traversals put a lonely child after (or before) its parent, but neither says on which side it hangs.
These two sequences are enough when every node has either zero or two children (so-called full binary trees). The second node of the pre-order is then always the root of the left subtree. Finding it in the post-order tells us how large the left subtree is, because the left subtree ends exactly there. We then recurse as in Question 1.
Common mistakes
Splitting the pre-order around the root’s position in the in-order. Only the in-order is split around the root. In the pre-order, we skip the root and use the size of the left subtree to know where it ends.
Assuming the tree is a BST and sorting the labels. The in-order of this tree is not alphabetical, so it is not a BST.
In Question 3, listing a couple of trees instead of all five, or drawing a single child in the middle, so that left and right look the same.
Remark: The problem assumes distinct labels for a reason. With duplicates, such as pre-order A, A and in-order A, A, we cannot tell whether the second A is the left or the right child of the first one.
Exercise 4 (The Lazy Validator ★★)
Recall from Lecture 5.2 the invariant of a binary search tree: Every node carries an item that is greater than or equal to all the items in its left subtree, and strictly smaller than all the items in its right subtree. A classmate wants to check whether a binary tree satisfies this invariant, and writes the following procedure:
isBST(node):
if node is null:
return true
if node.left is not null and node.left.item > node.item:
return false
if node.right is not null and node.right.item <= node.item:
return false
return isBST(node.left) and isBST(node.right)
Their argument: “It checks every node against its children, and it does so for every node in the tree, so it must be correct.”
Fig. 8 A binary tree with eight nodes#
Run
isBSTon the tree shown on Fig. 8. What does it return? Is this tree a BST? Search for 30 in this tree, as the lecture’scontainsoperation would do. What happens?What is the smallest tree (with the fewest nodes) that
isBSTaccepts, although it is not a BST? Explain why no smaller tree can fool it.Fix the procedure.
Hint: As you walk down the tree, every node you pass narrows the range of items allowed below it. Write a procedure
isBST(node, low, high)that also checks that every item in the subtree lies within this range. How do you call it on the root? What is its time complexity?Another classmate suggests a different approach: Run an in-order traversal, and check that the resulting sequence never decreases. Does that work with the invariant above? If not, give a counterexample, and say under which condition the approach does work.
Hint: Think about duplicates.
Solution to Exercise 4 (The Lazy Validator ★★)
isBSTreturns true: Every node is consistent with its children (in particular, 30 > 19 is a valid right child). Yet the tree is not a BST: 30 sits in the left subtree of 27, although 30 > 27.Searching for 30 goes right at 27 (30 > 27), then left at 39, then left at 35, where there is no left child. The search reports that 30 is missing, although the tree contains it. A broken invariant does not crash anything: The operations silently return wrong answers.
The smallest such tree has three nodes, for instance a root 2, its left child 1, and the right child 3 of Node 1 (left on the figure below). Node 3 is a valid right child of 1, but it sits in the left subtree of 2.
Fig. 9 Left: The smallest tree that fools
isBST. Right: A tree whose in-order never decreases, but which is not a BST.#With one or two nodes, every pair of nodes is a parent and its child, which
isBSTchecks directly. With three nodes, the only pairs it does not check are two siblings, or a node and its grandchild. Siblings are safe: \(l \leq r < s\) gives \(l < s\). A grandchild hanging on the same side as the child (both left, or both right) is safe too, since the order carries over: \(x \leq y \leq z\) gives \(x \leq z\). Only a “zig-zag” (left then right, or right then left) can escape the check.Each node passes down a range of allowed items, \((low, high]\): Its left subtree must hold items at most
node.item, and its right subtree items strictly greater thannode.item.isBST(node, low, high): if node is null: return true if node.item <= low or node.item > high: return false return isBST(node.left, low, node.item) and isBST(node.right, node.item, high)We call it as
isBST(root, -∞, +∞). On the tree from Question 1, 30 receives the range \((19, 27]\) and the check fails. Every node is visited once and does a constant amount of work, so the procedure runs in \(O(n)\).Alternative: Each call returns the minimum and the maximum of its subtree, and each node compares itself with the maximum of its left subtree and the minimum of its right subtree. It is also \(O(n)\), but harder to get right.
Not with duplicates. Take a root 5 with a right child 5 (right on the figure above). Its in-order is 5, 5, which never decreases, but the tree breaks the invariant: Items in the right subtree must be strictly greater. Moving the child to the left gives a valid BST with the same in-order, so the in-order alone cannot tell the two apart.
The approach works when all items are distinct: Then a binary tree is a BST exactly when its in-order is strictly increasing. It also runs in \(O(n)\).
Common mistakes
Answering Question 1 with “it is a BST, since every node is larger than its left child and smaller than its right child”. That is the classmate’s mistake: The invariant is about whole subtrees.
In Question 2, proposing a tree with a straight branch (e.g., 3, 2, 1 all as left children). Transitivity makes such trees safe.
Passing the ranges the wrong way round, for instance giving the left subtree \((node.item, high]\).
Using strict bounds on both sides, so that the fixed procedure rejects valid trees with duplicates on the left.
Exercise 5 (Inner Trouble ★★)
Lecture 5.4 distinguishes two ways in which an AVL tree can become unbalanced: The outer cases, fixed by a single rotation, and the inner cases, which need two. This problem focuses on the inner cases, and on what happens when we delete.
We use the conventions of the lecture: The empty tree has height \(-1\), the balance factor of a node is \(b = h(L) - h(R)\), and a left rotation lifts up the right child. Insertions proceed as in a plain BST before rebalancing, and a deleted node that has two children is replaced by its predecessor.
Starting from an empty AVL tree, insert the keys 50, 20, 70, 10, 30, and 25, in this order. Draw the tree after every insertion, and write down the balance factor of every node. When the tree becomes unbalanced, say which node is unbalanced, which of the four cases applies (outer or inner, left or right), and which rotations restore the balance. Draw the tree after each rotation.
Insert 60 into the resulting tree, and restore the balance. Compare the height of the subtree you rebalanced before the insertion with its height after the rebalancing. Why does this mean that, after an insertion, no node higher up ever needs rebalancing?
Now delete the keys 10, 25, and 20, in this order. Which deletion breaks the balance? What is then the balance factor of the heavy child? Which of the four cases from the lecture applies? Which rotation(s) restore the balance? Check that your final tree is a valid AVL tree.
Explain why the situation from the previous question, where the heavy child has a balance factor of 0, never occurs right after an insertion.
Solution to Exercise 5 (Inner Trouble ★★)
The first five insertions keep every balance factor in \([-1, 1]\):
Insert
Tree (balance factors in brackets)
50
50 [0]
20
50 [+1] with left child 20 [0]
70
50 [0] with children 20 [0] and 70 [0]
10
50 [+1], 20 [+1] with left child 10
30
50 [+1], 20 [0] with children 10 and 30
Inserting 25 places it as the left child of 30. Node 50 then has \(b = +2\) (left-heavy), and its left child 20 has \(b = -1\) (right-heavy): This is the inner left case. We rotate 20 to the left, which lifts 30 and moves its left child 25 under 20, then rotate 50 to the right:
Fig. 10 Left: After inserting 25, 50 is unbalanced. Middle: After rotating 20 to the left (an intermediate state). Right: After rotating 50 to the right. Balance factors are shown next to each node.#
The final tree has root 30, with children 20 (10, 25) and 50 (right child 70).
60 goes to the left of 70. Node 50 then has \(b = -2\) (right-heavy), and its right child 70 has \(b = +1\) (left-heavy): This is the inner right case. We rotate 70 to the right, then 50 to the left. The root 30 has \(b = -1\), which is still valid, so the rebalancing stays local:
Fig. 11 Left: After inserting 60, 50 is unbalanced. Right: After rotating 70 to the right and 50 to the left.#
Before the insertion, the subtree rooted at 50 (50, 70) had height 1. After the rebalancing, the subtree that takes its place (60, with children 50 and 70) has height 1 as well. Seen from the parent 30, nothing has changed, so neither 30 nor any other ancestor needs rebalancing. This holds for every insertion, single or double rotation alike: After an insertion, at most one node is rebalanced.
Deleting 10 and 25 keeps the tree balanced: 20 becomes a leaf and the root 30 gets \(b = -1\). Deleting 20 breaks the balance: The root 30 has an empty left subtree (height \(-1\)) and a right subtree of height 1, so \(b = -2\).
Its heavy child 60 has \(b = 0\): Both its subtrees (50 and 70) have the same height. None of the four cases from the lecture applies as such, since they assume a left- or right-heavy child. A single left rotation of 30 restores the balance, as in the outer case:
Fig. 12 Left: After deleting 20, 30 is unbalanced, and its right child 60 is balanced. Right: After rotating 30 to the left.#
The resulting tree has root 60 (\(b = +1\)), whose left child 30 (\(b = -1\)) has 50 as right child, and whose right child is 70. All balance factors lie in \([-1, 1]\), and the BST ordering holds (30 < 50 < 60 < 70).
Going further: Here, a double rotation would happen to yield a valid tree as well (50 at the root), but only because the subtrees are tiny. In general, it does not: With the notation of the lecture, take A with \(b = -2\), its right child B with two subtrees of height \(h+1\), and the left child C of B with a left subtree of height \(h\) and a right subtree of height \(h-1\). After a double rotation, B keeps its right subtree (height \(h+1\)) and receives the right subtree of C (height \(h-1\)) as its left subtree, so \(b(B) = -2\). When the heavy child is balanced, always use a single rotation.
A node becomes unbalanced after an insertion because the height of one of its children grew by one. That child grew because one of its subtrees grew, namely the one where the new node went. If the child ended up with \(b = 0\), its two subtrees would have the same height after the insertion, so the subtree that grew used to be the shorter one. Then the height of the child would not have changed, which contradicts our starting point.
The only exception would be the new leaf itself (\(b = 0\)). Its parent, however, had an empty side before the insertion, so its other subtree is at most a single leaf, and the parent’s balance factor stays within \([-1, 1]\).
Going further: After a deletion, the rebalanced subtree may end up one level shorter than before (whenever the heavy child is not balanced). Unlike insertion, a deletion may therefore require rebalancing several nodes on the way up to the root.
Common mistakes
Computing balance factors as \(h(R) - h(L)\), which flips all the cases.
Rotating the root instead of the lowest unbalanced node. In Question 2, only 50 needs fixing.
Losing a subtree during a rotation: In Question 1, 25 (the left child of 30) must move to become the right child of 20.
Treating Question 3 as an inner case, because 60 “is not right-heavy”.
Exercise 6 (Heapify, Step by Step ★★)
We use the minimum binary heap from Lecture 5.3, stored in an array whose first index is 0: The children of the node at index \(i\) sit at indices \(2i+1\) and \(2i+2\).
Heapify the array (14, 9, 17, 3, 12, 5, 8, 1, 6, 10). For every non-leaf node you process, list the swaps you make and give the array afterwards. How many swaps do you make in total? Draw the resulting heap as a tree.
Now build a heap by inserting the same items, in the same order, into an empty heap. Give the array after every insertion, and count the swaps. Do you obtain the same heap? Which method made fewer swaps?
Consider the keys 1 to 7. In which order should you insert them into an empty heap to make as many swaps as possible? How many swaps is that? How many swaps does heapify make on an array that holds the keys in that same order?
Explain the difference. Hint: In a complete binary tree, about half the nodes are leaves. How far can a leaf move during an insertion? During heapify?
A game server receives \(n\) scores, one at a time, and must report the \(k\) highest ones at the end. Memory is short: You may only use a minimum heap that never holds more than \(k\) scores.
Design a procedure that solves this problem, and run it on the scores (42, 17, 63, 8, 55, 71, 23, 60, 90, 12) with \(k = 3\). Give the heap array after every score. What is the time complexity of your procedure, in terms of \(n\) and \(k\)? Why does a minimum heap fit better here than a maximum heap?
Solution to Exercise 6 (Heapify, Step by Step ★★)
The array has 10 items, so the last non-leaf node is the parent of index 9, at index \((9 - 1) / 2 = 4\). We process indices 4 down to 0:
Index
Swaps
Array afterwards
4 (12)
12 ↔ 10
14, 9, 17, 3, 10, 5, 8, 1, 6, 12
3 (3)
3 ↔ 1
14, 9, 17, 1, 10, 5, 8, 3, 6, 12
2 (17)
17 ↔ 5
14, 9, 5, 1, 10, 17, 8, 3, 6, 12
1 (9)
9 ↔ 1, 9 ↔ 3
14, 1, 5, 3, 10, 17, 8, 9, 6, 12
0 (14)
14 ↔ 1, 14 ↔ 3, 14 ↔ 6
1, 3, 5, 6, 10, 17, 8, 9, 14, 12
This makes 8 swaps in total. The resulting heap is shown on the left of the figure below.
Fig. 13 Left: The heap built by heapify. Right: The heap built by repeated insertions. They differ only in the two highlighted nodes.#
Inserting the items one by one gives:
Insert
Swaps
Array afterwards
14
0
14
9
1
9, 14
17
0
9, 14, 17
3
2
3, 9, 17, 14
12
0
3, 9, 17, 14, 12
5
1
3, 9, 5, 14, 12, 17
8
0
3, 9, 5, 14, 12, 17, 8
1
3
1, 3, 5, 9, 12, 17, 8, 14
6
1
1, 3, 5, 6, 12, 17, 8, 14, 9
10
1
1, 3, 5, 6, 10, 17, 8, 14, 9, 12
This makes 9 swaps, against 8 for heapify. The heaps are not the same: 9 and 14 (indices 7 and 8) are swapped. Both are valid, since the heap ordering only constrains parents and children, not siblings. A set of items usually has many valid heaps.
Each insertion makes at most as many swaps as the depth of the new node. Inserting in descending order (7, 6, 5, 4, 3, 2, 1) makes every new key the smallest so far, so it climbs all the way to the root: \(0 + 1 + 1 + 2 + 2 + 2 + 2 = 10\) swaps, the maximum possible.
Heapify on (7, 6, 5, 4, 3, 2, 1) makes only 4 swaps: 5 ↔ 1 (index 2), 6 ↔ 3 (index 1), then 7 ↔ 1 and 7 ↔ 2 (index 0). The result is (1, 3, 2, 4, 6, 7, 5).
In an insertion, a node moves up, at most its depth. The leaves are the deepest nodes and make up about half of the tree, so in the worst case the total is about \(\frac{n}{2} \log_2 n\), that is \(O(n \log n)\). In heapify, a node moves down, at most its height. Leaves do not move at all, their parents move at most one level, and only the root may move \(\log_2 n\) levels. Most nodes are near the bottom, so most nodes move little: This is the \(O(n)\) bound from the lecture.
Keep in the heap the \(k\) highest scores seen so far. Its root is the smallest of them, that is, the one to evict when a better score arrives:
topK(scores, k): heap = empty minimum heap for each score in scores: if size(heap) < k: enqueue(heap, score) else if score > minimum(heap): dequeue(heap) enqueue(heap, score) return heapWith \(k = 3\), using the lecture’s
dequeueandenqueue:Score
Action
Heap afterwards
42
enqueue
42
17
enqueue
17, 42
63
enqueue
17, 42, 63
8
ignore (8 ≤ 17)
17, 42, 63
55
evict 17
42, 63, 55
71
evict 42
55, 63, 71
23
ignore (23 ≤ 55)
55, 63, 71
60
evict 55
60, 71, 63
90
evict 60
63, 71, 90
12
ignore (12 ≤ 63)
63, 71, 90
The three highest scores are 63, 71, and 90. If you replace the root directly and move it down, you get (42, 55, 63) and (55, 71, 63) after scores 55 and 71, which is also correct.
Each score costs at most one
dequeueand oneenqueueon a heap of size \(k\), so the procedure runs in \(O(n \log k)\) time and \(O(k)\) memory.A maximum heap gives quick access to the best score kept so far, but to decide whether a new score gets in, we need the worst one. In a maximum heap of size \(k\), finding (and removing) it would take \(O(k)\).
Going further: With enough memory, we could heapify all \(n\) scores into a maximum heap and dequeue \(k\) times, in \(O(n + k \log n)\). That requires storing every score, and having them all before we start.
Common mistakes
Starting heapify at the root, or at the last node instead of the last non-leaf node.
Stopping a node’s descent after one swap. In Question 1, 14 moves down three levels.
Swapping with the first child instead of the smallest one.
In Question 4, using a maximum heap and evicting the root, which throws away the best score instead of the worst.
Exercise 7 (Counting Disk Reads ★★)
Lecture 5.5 introduces B-trees to reduce the number of disk accesses. We use its conventions with \(k = 4\): Only the leaves carry entries, at most four each. An internal node has at most four children, separated by up to three keys, and every key is the smallest key found in the subtree on its right. Every node except the root holds at least \(\lfloor k/2 \rfloor = 2\) entries or children. Consider the B-tree shown on Fig. 14.
Fig. 14 A B-tree with \(k = 4\), holding ten entries#
To leave no room for doubt, we fix the following rules:
An overflowing node (five entries or children) keeps the first three, and moves the last two into a new node, placed just on its right. Its parent gets a new key: The smallest key under the new node.
To repair an underflowing node, first look at its left sibling: If it holds more than two entries (or children), borrow its last one. Otherwise, look at its right sibling, and borrow its first one under the same condition. Otherwise, merge with the left sibling if there is one, or else with the right sibling. In every case, update the keys of the parent.
We count disk accesses as in the disk-access model. Each node fills
exactly one block: Reading a node costs one READ_BLOCK, and
writing it costs one WRITE_BLOCK. Memory is small, so every
operation starts by reading the root, but a node read during an
operation stays in memory until the operation ends. Every node that
is created or changed is written exactly once, at the end of the
operation. Discarding a node costs nothing, and so does keeping
track of where the root is.
Insert the key 28. Draw the tree after every split. Which nodes do you read, and which do you write? How many disk accesses does the insertion cost in total?
Starting from the tree you obtained, delete the keys 30, 10, and 50, in this order. Draw the tree after each deletion, say how you repair every underflow (borrowing or merging, and with which sibling), and count the reads and writes.
After the last deletion, the root is left with a single child. Does this break the B-tree rule? What would you do about it, and why does it matter for later operations?
Consider a B-tree of height \(h\), where, as for any tree, a single node has height 0. In the worst case, how many disk accesses does an insertion cost? Which insertion triggers this worst case? Show that a deletion also costs \(O(h)\) disk accesses.
A database stores \(10^9\) entries in a B-tree, on a hard drive whose blocks hold 4096 bytes. A key and the address of a block take 8 bytes each, so we choose \(k = 4096 / 16 = 256\). What are the smallest and the largest possible heights of this B-tree? How many blocks does a search read at most?
Compare with a perfectly balanced BST holding the same entries, where every node sits in its own block. Assuming a disk access takes 10 ms, how long does a search take in each case?
Hint: The tree is tallest when its nodes are as empty as the rule allows. At least how many entries does a B-tree of height \(h\) hold?
Solution to Exercise 7 (Counting Disk Reads ★★)
28 lands in Leaf 2, which overflows with five entries (20, 25, 28, 30, 35). Leaf 2 keeps (20, 25, 28), a new Leaf 5 takes (30, 35), and the root gets the key 30. The root then has five children (keys 20, 30, 40, 60) and overflows in turn: It keeps its first three children (Leaf 1, Leaf 2, Leaf 5, separated by 20 and 30), and a new Node B takes Leaf 3 and Leaf 4 (separated by 60). The smallest key under Node B, 40, goes up into a new root:
Fig. 15 The B-tree after inserting 28. The nodes in red are written.#
We read 2 nodes (the root and Leaf 2) and write 5 (Leaf 2, Leaf 5, the old root, Node B, and the new root), that is, 7 disk accesses. The tree grows at the top, so all leaves stay at the same depth.
The three deletions go as follows:
Delete
Repair
Reads
Writes
Total
30
Leaf 5 is left with (35). Its left sibling Leaf 2 holds three entries, so Leaf 5 borrows 28, and the key 30 in Node A becomes 28.
4 (root, A, Leaf 5, Leaf 2)
3 (Leaf 2, Leaf 5, A)
7
10
Leaf 1 is left with (5). It has no left sibling, and its right sibling Leaf 2 has no entry to spare, so they merge into Leaf 6 (5, 20, 25). Node A keeps two children.
4 (root, A, Leaf 1, Leaf 2)
2 (Leaf 6, A)
6
50
Leaf 3 is left with (40), and merges with its right sibling Leaf 4 into Leaf 7 (40, 60, 70). Node B is then left with a single child. Its left sibling Node A has no child to spare, so A and B merge. The key 40, the smallest key under Leaf 7, comes down from the root.
5 (root, B, Leaf 3, Leaf 4, A)
2 (Leaf 7, merged node)
7
Fig. 16 After deleting 30: Leaf 5 borrows from Leaf 2#
Fig. 17 After deleting 10: Leaf 1 merges with Leaf 2#
Fig. 18 After deleting 50: The leaves merge, then the internal nodes merge, and the root disappears#
A root with a single child does not break the rule, since the root is the only node allowed to have fewer than two children. It is useless, though: Every later operation would read it, paying one disk access to learn nothing. We discard it, and the merged node becomes the new root. The height drops back to 1: A B-tree shrinks at the top, just as it grows at the top.
If you keep the old root and write it, you count 3 writes instead of 2 for the last deletion.
An insertion reads one node per level, that is, \(h + 1\) reads. In the worst case, every node on the path splits, which writes two nodes per level, plus a new root: \(2(h + 1) + 1 = 2h + 3\) writes. The total is \(3h + 4\). It happens when the leaf and all its ancestors are full, as in Question 1 (\(h = 1\), 7 accesses).
A deletion also reads the path, plus at most two siblings per level (the left one, then the right one). At every level, the repair writes at most the node, its sibling, and their parent. Each level therefore costs a constant number of disk accesses, and there are \(h + 1\) levels: \(O(h)\) in total. (A careful count gives at most \(3h + 1\) reads and \(h + 2\) writes.)
The tree is shortest when its nodes are full: A tree of height \(h\) then holds \(256^h\) leaves of 256 entries, that is \(256^{h+1}\) entries. Since \(256^3 \approx 1.7 \cdot 10^7\) and \(256^4 \approx 4.3 \cdot 10^9\), we need \(h + 1 = 4\), so the smallest height is \(h = 3\).
The tree is tallest when its nodes are as empty as possible: The root has two children, and every other node has 128 children or entries. A tree of height \(h \geq 1\) then holds \(2 \cdot 128^{h-1}\) leaves of 128 entries, that is \(2 \cdot 128^h\) entries. Since \(2 \cdot 128^4 \approx 5.4 \cdot 10^8 \leq 10^9\) but \(2 \cdot 128^5 \approx 6.9 \cdot 10^{10} > 10^9\), the largest height is \(h = 4\).
A search reads one block per level, so at most 5 blocks, that is 50 ms. A perfectly balanced BST with \(10^9\) nodes has height \(\lfloor \log_2 10^9 \rfloor = 29\), so a search reads up to 30 blocks: 300 ms, six times slower. In practice, the database keeps the root (and often the next level) in memory, which brings the B-tree down to 3 or 4 reads.
Common mistakes
Writing only the leaf after a split, and forgetting the new node and the parent that gets a new key.
Not counting the reads of siblings during a deletion. A node can only borrow from a sibling once it has looked at it.
Forgetting to update the parent’s key after borrowing. If Node A still holds 30 after the first deletion, a search for 28 goes to Leaf 2 and misses it.
Stopping after the leaves merge when deleting 50, although Node B is then left with a single child.
Mixing up levels and height: A tree of height \(h\) has \(h + 1\) levels, so a search reads \(h + 1\) blocks.
Note: In the lecture, the split figure keeps three entries on the left, whereas the Ruby code keeps two. The problem fixes the figure’s rule, so that everyone gets the same trees.
Exercise 8 (Prefix Power ★★)
Lecture 5.6 stores a set of words in a prefix tree: The root carries the empty symbol \(\varepsilon\), every other node carries one symbol, and a leaf \(\bot\) marks the end of each word. The lecture explains how to check whether a word is in the tree, but leaves prefix search, insertion, and deletion open. This problem fills these gaps. Consider the set of eight words
Draw the prefix tree that holds \(S\). How many nodes does it have? Compare with the total number of symbols in the eight words. Explain why the leaves \(\bot\) are necessary in this tree.
In a compressed prefix tree, every chain of nodes that have a single child is merged into a single node, which carries all their symbols. Afterwards, every node except the leaves has at least two children. Draw the compressed version of your tree. How many nodes are left?
Explain how to check whether the compressed tree contains a word, and run your method on “te”, “tend”, and “inn”.
Design a procedure
prefixSearch(root, p)that returns all the words of a (non-compressed) prefix tree that start with the prefix \(p\). Run it on the prefixes “te”, “in”, “tex”, and on the empty prefix. What is its time complexity?What changes in a compressed tree? Think in particular of the prefix “inn”.
Add the word “i” to your compressed tree, then remove the word “in”. Draw the tree after each step. In general, when does an insertion force you to split a node? When does a deletion force you to merge nodes?
Solution to Exercise 8 (Prefix Power ★★)
The prefix tree has 22 nodes: The root, 13 nodes that carry a symbol, and 8 leaves \(\bot\), one per word.
Fig. 19 The prefix tree holding \(S\)#
The eight words total \(2 + 5 + 3 + 4 + 3 + 4 + 2 + 3 = 26\) symbols, but only 13 symbol nodes are needed: Words that share a prefix share the nodes of that prefix.
Without the leaves \(\bot\), we could not tell that “in” is a word: Its path is part of the path to “inner”. The same goes for “tea” (part of “tear”), “ten” (“tend”), and “to” (“toe”). By contrast, “te” and “inn” label paths in the tree, but they are not words, since no leaf \(\bot\) hangs below them.
The compressed tree has 15 nodes:
Fig. 20 The compressed prefix tree holding \(S\)#
The nodes “i” and “n” merge into “in”, and every symbol whose only child is a leaf merges with it (for instance “r⊥”).
To check a word, we compare the rest of the word with the whole label of a child, and only move to that child when the label is a prefix of the rest. The word is in the tree when we consume it entirely and reach a node that ends with \(\bot\) (or that has a child \(\bot\)).
“te”: we consume “t”, then “e”, and the word is used up. Node “e” has no child \(\bot\), so “te” is not in the tree.
“tend”: we consume “t”, “e”, “n”, and the rest “d” matches the label “d⊥”. The word is in the tree.
“inn”: we consume “in”, and the rest “n” only matches the beginning of “ner⊥”. The word is not in the tree.
We walk down along \(p\), then collect every word below the node we reach:
prefixSearch(root, p): node = root for each symbol c in p: if node has no child carrying c: return {} node = the child of node carrying c results = {} collect(node, p, results) return results collect(node, word, results): for each child of node: if child.symbol is ⊥: add word to results else: collect(child, word + child.symbol, results)“te” gives {tea, tear, ten, tend}.
“in” gives {in, inner}: “in” itself counts, since a word is a prefix of itself.
“tex” gives the empty set: Node “e” has no child “x”.
The empty prefix gives the whole set \(S\), since we collect from the root.
Walking down takes \(O(|p|)\) steps, assuming each node finds a child in \(O(1)\) (e.g., with a hash table). The collection visits every node below once, and there are at most as many such nodes as symbols in the results (plus one \(\bot\) per word). The total is \(O(|p| + m)\), where \(m\) is the total length of the words returned: The cost depends on the size of the answer, not on the size of the tree.
In a compressed tree, the prefix may end in the middle of a label. With “inn”, we consume “in”, and the rest “n” is only the beginning of “ner⊥”. We must then collect everything below “ner⊥”, which gives {inner}. At each step, there are three cases: The label is a prefix of the rest (consume it and go on), the rest is a prefix of the label (stop and collect below that child), or neither (return the empty set).
Adding “i” leaves the label “in” in the middle: “i” ends after its first symbol. We split the node “in” into “i”, whose children are a new leaf \(\bot\) and “n”, which keeps the former children of “in”. Removing “in” then deletes the leaf \(\bot\) below “n”. “n” is left with a single child, so it merges with it into “nner⊥”:
Fig. 21 Left: After adding “i”, the node “in” splits. Right: After removing “in”, the node “n” merges with its last child.#
An insertion splits a node when the new word and the label share only a part of the label: The shared part stays in the node, and the rest moves down to a new child. A deletion merges a node with its child when the node is left with a single child. In a non-compressed tree, a deletion removes the nodes that lead only to the deleted word, climbing up until it reaches a node that still has other children.
Common mistakes
Forgetting the leaf \(\bot\) below “in” (or “tea”, “ten”, “to”), which loses the shorter words.
Matching only the first symbol of a label in the compressed tree, so that “inn” is found although it is not a word.
In the prefix search, returning only the leaves directly below the prefix node instead of the whole subtree, or forgetting the prefix itself when it is a word.
Claiming the prefix search runs in \(O(|p|)\). Listing \(m\) symbols takes at least \(m\) steps.
Exercise 9 (One Pass Is Enough ★★★)
Many questions about a binary tree combine answers computed on its subtrees. Computed carelessly, the same answers get recomputed over and over. This problem shows how to compute everything in a single traversal.
As in Lecture 5.4, the empty tree has height \(-1\), and we say a binary tree is balanced when the balance factor of every node lies in \([-1, 1]\). We ignore the items: The tree need not be a BST. The diameter of a tree is the number of edges on the longest path between two of its nodes. Such a path climbs up from one node and goes down to the other, and it need not pass through the root.
Fig. 22 A binary tree with ten nodes#
A classmate checks the balance as follows, where
heightreturns the height of a subtree, as in Exercise 1:isBalanced(node): if node is null: return true if abs(height(node.left) - height(node.right)) > 1: return false return isBalanced(node.left) and isBalanced(node.right)Which nodes of the tree shown on Fig. 22 are unbalanced? Which one makes
isBalancedreturn false?Now run
isBalancedon a perfect binary tree of height \(h\), whose leaves all sit at depth \(h\). How many times do the calls toheightvisit a node at depth \(d\)? Deduce the time complexity ofisBalancedon such a tree, in terms of its number of nodes \(n\).Write a procedure that checks the balance in a single traversal, visiting every node at most once. What is its time complexity? Run it on Fig. 22. Which node reveals the imbalance first? Which nodes are never visited?
Hint: A node needs the heights of its children to check its own balance, and to compute its own height. Let a single procedure return both, and agree on a special value meaning “unbalanced”.
What is the diameter of the tree on Fig. 22? Give a longest path. Does it pass through the root?
The classmate now writes:
diameter(node): if node is null: return 0 through = height(node.left) + height(node.right) + 2 return max(through, diameter(node.left), diameter(node.right))Explain why it is correct. What is its time complexity on a tree where every node has a single child? Write a procedure that computes the diameter in a single traversal.
Adapt your single-pass procedure to the general trees of Exercise 1, where a node may have any number of children. Run it on the tree shown on Fig. 2: What is its diameter, and which two nodes are the furthest apart?
Hint: A longest path has a single highest node. Through which of its children can it go?
Solution to Exercise 9 (One Pass Is Enough ★★★)
The heights and balance factors are:
Node
A
B
C
D
E
F
G
H
I
J
Height
4
3
0
2
2
1
0
0
1
0
Balance
+3
0
0
+1
-2
+1
0
0
-1
0
Two nodes are unbalanced: A and E.
isBalancedchecks the root first, so A makes it return false at once, and the procedure never looks at E.In a perfect tree, every node is balanced, so
isBalancedruns on every node.isBalanced(v)callsheighton both children of \(v\), which visits the whole subtree of \(v\), except \(v\) itself. A node at depth \(d\) is thus visited once for each of its \(d\) proper ancestors: \(d\) times. There are \(2^d\) nodes at depth \(d\), so the total is\[\sum_{d=0}^{h} d \cdot 2^d = (h - 1) \cdot 2^{h+1} + 2\]With \(n = 2^{h+1} - 1\), that is about \(n \log_2 n\):
isBalancedruns in \(O(n \log n)\) on a perfect tree. Most nodes are deep, and each of them is visited once per ancestor.A single post-order traversal computes the heights and checks the balance along the way:
checkHeight(node): if node is null: return -1 left = checkHeight(node.left) if left is UNBALANCED: return UNBALANCED right = checkHeight(node.right) if right is UNBALANCED: return UNBALANCED if abs(left - right) > 1: return UNBALANCED return 1 + max(left, right) isBalanced(root): return checkHeight(root) is not UNBALANCEDEach node is visited at most once and does a constant amount of work, so the procedure runs in \(O(n)\).
On the tree from the figure, the calls return in post-order: H (0), F (1), G (0), D (2), J (0), I (1), then E, whose children have heights \(-1\) and 1, returns UNBALANCED. B and A pass it up without further checks. The imbalance is revealed at E, the lowest unbalanced node, and C is never visited.
The diameter is 6, along H, F, D, B, E, I, J. Its highest node is B, not the root: The longest path through A (from H or J to C) has only 5 edges.
The procedure is correct because every path has a single highest node \(v\). Either the path goes down into both subtrees of \(v\), and then its longest version has \(h(L) + 1 + h(R) + 1\) edges, or it stays entirely within one subtree, where the recursive calls find it. With \(h(\varnothing) = -1\), the formula also gives 0 for a leaf and \(h + 1\) for a node with a single child.
On a tree where every node has a single child (a single branch of \(n\) nodes), each call computes the height of the whole branch below it: \((n - 1) + (n - 2) + \ldots + 1\), that is \(O(n^2)\). Returning the height together with the diameter avoids recomputing it:
heightAndDiameter(node): if node is null: return (-1, 0) (hl, dl) = heightAndDiameter(node.left) (hr, dr) = heightAndDiameter(node.right) height = 1 + max(hl, hr) diameter = max(hl + hr + 2, dl, dr) return (height, diameter)It visits every node once and runs in \(O(n)\).
A path whose highest node is \(v\) goes down through at most two of its children. To make it as long as possible, we pick the two tallest ones:
heightAndDiameter(node): first = -1 # largest height among the children second = -1 # second largest diameter = 0 for each child in node.children: (h, d) = heightAndDiameter(child) diameter = max(diameter, d) if h > first: second = first first = h else if h > second: second = h diameter = max(diameter, first + second + 2) return (1 + first, diameter)Starting both heights at \(-1\) handles leaves and nodes with a single child, as before. The procedure still visits every node once, in \(O(n)\).
On the tree from Problem 1, the children of R have heights 1 (S), 0 (T), 2 (U), and 0 (V). The two tallest give \(2 + 1 + 2 = 5\), and no other node does better (S gives 2, U and Y give 1). The diameter is 5, between Z and W (or X), along Z, Y, U, R, S, W.
Common mistakes
Counting nodes instead of edges, which gives a diameter of 7 instead of 6.
Assuming the longest path passes through the root, and answering \(h(L) + h(R) + 2 = 5\).
Using \(-2\) as the “unbalanced” value without checking for it. A child returning \(-2\) next to an empty sibling (\(-1\)) then passes the test \(|{-2} - ({-1})| \leq 1\), and the imbalance gets lost.
In Question 4, combining the first two children instead of the two tallest ones.
Going Further#
These problems go beyond the lectures: Huffman coding and scapegoat trees are not part of the syllabus. Both build directly on this module, though. Huffman codes are binary prefix trees, and scapegoat trees are another way to keep a search tree balanced. Each problem starts with a short background that introduces everything you need.
Exercise 10 (Squeezing BANANA BANDANA ★★★)
Background. A computer stores text as a sequence of bits, so every symbol needs a code: A sequence of bits that stands for it. The simplest choice gives every symbol a code of the same length. With five symbols, three bits each are enough, for example A = 000, B = 001, and so on. But some symbols are much more frequent than others. If we give them shorter codes, and give longer codes to the rare ones, the whole text takes fewer bits.
Codes of different lengths raise a problem, though: Where does one code end and the next begin? A code is prefix-free when no code is the beginning (the prefix) of another one. With a prefix-free code, we can read the bits from left to right, and as soon as the bits read so far form a code, we know which symbol it stands for.
A prefix-free code is a binary tree. Every symbol sits in a leaf, and its code describes the path from the root to that leaf: 0 means “go to the left child” and 1 means “go to the right child”. This is the prefix tree of Lecture 5.6, over the alphabet \(\{0, 1\}\). Because no code is the prefix of another, every code ends in a leaf, and we need no \(\bot\) marker. To decode, we start at the root and follow the bits. Whenever we reach a leaf, we output its symbol and jump back to the root.
Huffman’s algorithm builds the best such tree for a given text. It uses a minimum priority queue, as in Lecture 5.3, whose items are trees and whose priorities are weights:
Count how often each symbol occurs. For each symbol, enqueue a tree made of a single leaf, whose weight is that count.
While the queue holds more than one tree, dequeue the two lightest trees, join them as the left and right children of a new root, and enqueue the result, whose weight is the sum of their weights.
The last tree left in the queue is the code tree.
The algorithm leaves ties open: When several trees have the same weight, any choice is allowed.
Problem. We want to store the text “BANANA BANDANA”, where the space “ “ counts as a symbol, written ␣. Consider the following three codes:
Symbol |
A |
N |
B |
D |
␣ |
|---|---|---|---|---|---|
Code 1 |
0 |
1 |
01 |
10 |
11 |
Code 2 |
00 |
01 |
10 |
110 |
111 |
Code 3 |
000 |
001 |
010 |
011 |
100 |
Which of these codes are prefix-free? For Code 1, find a sequence of bits that can be decoded in two different ways. Draw the trees of the other two codes, and use the tree of Code 2 to decode 110000100.
How many bits does the text take with Code 2? With Code 3?
Run Huffman’s algorithm on the text. Give the content of the priority queue after every step, draw the resulting tree, and give the code of every symbol. How many bits does the text take now? Does a different way of breaking the ties change this number?
Show that in a best code tree (one that minimises the total number of bits), every internal node has exactly two children.
Then show that there is always a best tree where the two least frequent symbols are siblings, at the deepest level. This is the key step in proving that Huffman’s algorithm is optimal.
Hint: What happens to the total number of bits if you swap a frequent symbol with a rarer one that sits deeper in the tree?
A text uses \(m\) different symbols. What is the time complexity of Huffman’s algorithm, once the symbols are counted, if the priority queue is a binary heap? Find a text with four different symbols where the Huffman code saves nothing compared with a code where every symbol takes two bits.
Solution to Exercise 10 (Squeezing BANANA BANDANA ★★★)
Code 1 is not prefix-free: A = 0 is the prefix of B = 01, and N = 1 is the prefix of D = 10 and ␣ = 11. The bits 01 decode either as B, or as A followed by N. (Other answers work too, e.g., 10 is D or NA.)
Codes 2 and 3 are prefix-free. For Code 3 it is immediate: All codes have the same length and are different, so none can be the beginning of another.
Fig. 23 Left: The tree of Code 2. Right: The tree of Code 3, where two nodes have a single child.#
With Code 2, 110000100 splits as 110 · 00 · 01 · 00, that is, D, A, N, A: “DANA”.
The text has 14 symbols: A occurs 6 times, N 4 times, B twice, and D and ␣ once each. Code 2 takes \(6 \cdot 2 + 4 \cdot 2 + 2 \cdot 2 + 1 \cdot 3 + 1 \cdot 3 = 30\) bits, and Code 3 takes \(14 \cdot 3 = 42\) bits.
Writing every tree with its weight, the queue evolves as follows (D␣ is the tree joining D and ␣, and so on):
Step
Dequeued
Queue afterwards
Start
D:1, ␣:1, B:2, N:4, A:6
1
D:1, ␣:1
B:2, D␣:2, N:4, A:6
2
B:2, D␣:2
N:4, BD␣:4, A:6
3
N:4, BD␣:4
A:6, NBD␣:8
4
A:6, NBD␣:8
ANBD␣:14
Fig. 24 The Huffman tree of “BANANA BANDANA”. Internal nodes show their weight.#
The codes are A = 0, N = 10, B = 110, D = 1110, and ␣ = 1111. The text takes \(6 \cdot 1 + 4 \cdot 2 + 2 \cdot 3 + 1 \cdot 4 + 1 \cdot 4 = 28\) bits, a third less than Code 3, and 2 bits less than Code 2.
Here, the ties do not change the code lengths: At each step, exactly two trees have the smallest weights, so the only choice is which one goes left, which swaps 0s and 1s. In general, ties can change the code lengths, but never the total. For the weights 1, 1, 2, 2, joining the two leaves of weight 2 gives four codes of length 2, whereas joining the tree of weight 2 with a leaf gives lengths 1, 2, 3, 3. Both take 12 bits.
Every internal node has two children. Assume an internal node \(v\) has a single child. Remove \(v\), and hang its child in its place. The code stays prefix-free (symbols are still in the leaves), and every symbol below \(v\) loses one bit. Since every symbol occurs at least once, the total decreases, so the tree was not a best one. Code 3 shows this: Replacing ␣ = 100 by ␣ = 1 saves 2 bits.
The two rarest symbols can be deepest siblings. Take a best tree, and a leaf \(a\) at the largest depth. Its parent has two children, and the other one, \(b\), is also a leaf: Otherwise, there would be a deeper leaf. Let \(x\) and \(y\) be the two rarest symbols, with frequencies \(f(x) \leq f(a)\) and depths \(d(x) \leq d(a)\). Swapping \(x\) and \(a\) changes the total by
\[f(x)\,d(a) + f(a)\,d(x) - f(x)\,d(x) - f(a)\,d(a) = -\big(f(a) - f(x)\big)\big(d(a) - d(x)\big) \leq 0\]So the swap cannot make the tree worse, and the tree stays a best one. Swapping \(y\) and \(b\) the same way puts \(x\) and \(y\) side by side at the deepest level. (If \(x\) or \(y\) is already \(a\) or \(b\), skip the corresponding swap.)
To finish the proof: The rest is an induction on the number of symbols. Merging \(x\) and \(y\) into a single symbol of frequency \(f(x) + f(y)\) gives a smaller problem, and any tree \(T\) for the original symbols where they are siblings costs exactly \(f(x) + f(y)\) more than the merged tree \(T'\). This is what Huffman’s first step does.
The queue starts with \(m\) trees, and every step replaces two trees by one, so there are \(m - 1\) steps. Each one makes two dequeues and one enqueue, in \(O(\log m)\) each. In total: \(O(m \log m)\). (Filling the queue with \(m\) insertions also takes \(O(m \log m)\), or \(O(m)\) with heapify.)
With four symbols that occur equally often, e.g., “ABCD”, Huffman joins A with B, then C with D, then the two trees: Every symbol gets 2 bits, exactly as with a fixed-length code. In general, a fixed-length code is already optimal when the frequencies are close to each other: With 3, 3, 2, 2, Huffman also gives every symbol 2 bits.
Common mistakes
Forgetting the space, or counting it twice, and working with 13 or 15 symbols.
Claiming that Code 3 is not prefix-free because “000 starts with 0”. Prefix-free is about whole codes, not about single bits.
Dequeuing the trees by number of symbols instead of by weight.
Claiming that the Huffman tree is unique. It is not, but its total cost is.
In Question 4, swapping in the wrong direction (moving the rare symbol up), and concluding that the total increases.
Exercise 11 (Scapegoat Trees ★★★★)
Background. AVL trees stay balanced by storing extra information in every node, and by fixing the tree with rotations after every insertion and deletion. Scapegoat trees take a lazier approach: Nodes store nothing but their item and their two children, and the tree is a plain BST most of the time. Only when a new node lands too deep do we pick one subtree, the scapegoat, and rebuild it from scratch.
We fix a parameter \(\alpha\) with \(1/2 < \alpha < 1\). In this problem, \(\alpha = 2/3\). The size of a node is the number of nodes in its subtree, as in Exercise 1, and the root has depth 0. A node \(w\) is weight-balanced when each of its children has a size of at most \(\alpha \cdot \mathrm{size}(w)\): Neither side holds much more than its share of the nodes.
A scapegoat tree with \(n\) nodes handles an insertion as follows:
Insert the new node \(x\) as in a plain BST (see Lecture 5.2).
If the depth of \(x\) is at most \(\log_{1/\alpha} n\), where \(n\) now counts \(x\), stop here.
Otherwise, walk up from \(x\) towards the root, and stop at the first ancestor \(w\) that is not weight-balanced. This node is the scapegoat.
Rebuild the subtree rooted at \(w\) into a perfectly balanced tree: List its \(m\) items in order (with an in-order traversal), make the item at position \(\lceil m/2 \rceil\) the new root, and build its left and right subtrees the same way, from the items on each side.
Problem.
Starting from an empty scapegoat tree, insert the keys 10, 20, 30, 40, 50, 60, and 70, in this order. Draw the tree after every insertion. Whenever the new node lands too deep, give the size of every ancestor you check on the way up, say which node is the scapegoat, and draw the tree after the rebuild.
Compare the final height with that of a plain BST built from the same insertions.
Show that, whenever the new node \(x\) lands too deep, the walk up always finds a scapegoat.
Hint: Assume that every ancestor of \(x\) is weight-balanced. Going down one step from the root, how much can the size shrink at most? What does this give for the size of \(x\)?
One can show that the rebuild always brings \(x\) back within the bound. What is then the largest possible height of a scapegoat tree with \(n\) nodes, built by insertions only? Compare it with the bound \(1.44 \log_2 n\) of AVL trees.
How long does it take to rebuild a subtree with \(m\) nodes? Find a sequence of insertions where a single insertion triggers a rebuild of \(\Theta(n)\) nodes.
Since the nodes do not store their size, the walk up must compute it. Show that finding the scapegoat takes \(O(\mathrm{size}(w))\) time, so it costs no more than the rebuild itself.
Hint: When you step up from a child \(u\) to its parent, you already know the size of \(u\). Which subtree do you still need to count?
Rebuilding is expensive, but it should not happen often. Right after a rebuild, the scapegoat \(w\) has \(m\) nodes in its subtree and is perfectly balanced. Show that at least \(\Omega(m)\) insertions into the subtree of \(w\) must happen before \(w\) can be a scapegoat again.
Deduce that \(n\) insertions into an empty scapegoat tree take \(O(n \log n)\) time in total, so that each insertion costs \(O(\log n)\) on average.
Hint: Pay for each rebuild with the insertions that made it necessary. Under how many scapegoats can a single inserted node sit?
Solution to Exercise 11 (Scapegoat Trees ★★★★)
With \(\alpha = 2/3\), the bound is \(\log_{3/2} n\), which gives 0, 1.71, 2.71, 3.42, 3.97, 4.42, and 4.80 for \(n = 1, \ldots, 7\). Every key is larger than the previous ones, so it always goes to the right.
Inserting 10, 20, 30, and 40 gives a single branch to the right, where 40 sits at depth \(3 \leq 3.42\). No rebuild.
50 lands at depth \(4 > 3.97\). Walking up, we check 40 (size 2, child 50 of size 1, limit 1.33: balanced), then 30 (size 3, child 40 of size 2, limit 2: balanced, just), then 20 (size 4, child 30 of size 3, limit 2.67: not balanced). The scapegoat is 20. Its items are 20, 30, 40, 50, so the new root is the 2nd one, 30. Its left subtree is 20, and its right subtree is 40 with the right child 50. Node 50 now sits at depth 3.
60 lands at depth \(4 \leq 4.42\). No rebuild.
70 lands at depth \(5 > 4.80\). Walking up, we check 60 (size 2, limit 1.33: balanced), 50 (size 3, child of size 2, limit 2: balanced), and 40 (size 4, child of size 3, limit 2.67: not balanced). The scapegoat is 40. Rebuilding 40, 50, 60, 70 makes 50 the root, with 40 on its left, and 60 on its right, which has 70 as right child.
Fig. 25 (a) After inserting 50: 20 is the scapegoat. (b) After the rebuild. (c) After inserting 70: 40 is the scapegoat. (d) The final tree.#
The final tree has height 4, against 6 for the plain BST, which is a single branch. Note that the root 10 is not weight-balanced (its right child has size 6), yet nothing forces us to fix it: A scapegoat tree only enforces the depth bound, not weight balance everywhere.
Let \(u_0, u_1, \ldots, u_d = x\) be the path from the root to \(x\), where \(d > \log_{1/\alpha} n\). Assume that every ancestor \(u_0, \ldots, u_{d-1}\) is weight-balanced. Then \(\mathrm{size}(u_{i+1}) \leq \alpha \cdot \mathrm{size}(u_i)\) at every step, and
\[1 = \mathrm{size}(x) \leq \alpha^d \cdot \mathrm{size}(u_0) = \alpha^d \, n\]that is, \((1/\alpha)^d \leq n\), or \(d \leq \log_{1/\alpha} n\). This contradicts the assumption, so some ancestor is not weight-balanced, and the walk up finds it.
If every insertion leaves the new node within the bound, no node ever sits deeper than \(\log_{1/\alpha}\) of the number of nodes at the time, and that number only grows. With \(\alpha = 2/3\), the height is at most
\[\log_{3/2} n = \frac{\log_2 n}{\log_2 (3/2)} \approx 1.71 \log_2 n\]This is a looser bound than the \(1.44 \log_2 n\) of AVL trees, but still \(O(\log n)\), without storing anything in the nodes. Choosing \(\alpha\) closer to \(1/2\) tightens the bound, at the price of more frequent rebuilds.
The in-order traversal lists the \(m\) items into an array in \(O(m)\). Building the balanced tree creates one node per item, and each call does a constant amount of work besides its recursive calls (picking the middle of a slice of the array, not copying it), so it also takes \(O(m)\). A rebuild takes \(\Theta(m)\) time.
Inserting the keys 1, 2, 3, … in increasing order triggers ever larger rebuilds: Every key goes to the right-most branch, which keeps growing until a node high up becomes the scapegoat. For instance, the 24th insertion rebuilds a subtree of 23 nodes, the 413th rebuilds 363 nodes, and the 880th rebuilds 648 nodes. You do not need these exact numbers, which come from running the procedure on a computer: It is enough to argue that a subtree holding a constant fraction of the tree gets rebuilt.
To find the scapegoat, we know that \(\mathrm{size}(x) = 1\). Stepping up from a child \(u\) to its parent \(p\), we only need to count the other child \(s\) of \(p\): \(\mathrm{size}(p) = \mathrm{size}(u) + \mathrm{size}(s) + 1\), which costs \(O(\mathrm{size}(s))\) with the
sizeprocedure from Problem 1. All these siblings lie in the subtree of the scapegoat \(w\), and they are disjoint, so the walk costs \(O(\mathrm{size}(w))\) in total, plus one step per level.Right after the rebuild, each child of \(w\) has at most \(m/2\) nodes. Assume \(k\) insertions then land in the subtree of \(w\) before it becomes a scapegoat again (with insertions only, nothing ever leaves it). Its size is then \(m + k\), and its heavy child holds at most \(m/2 + k\) nodes. For \(w\) to be a scapegoat, we need
\[\frac{m}{2} + k > \frac{2}{3}(m + k) \quad\Longleftrightarrow\quad k > \frac{m}{2}\]So more than \(m/2\) insertions must land below \(w\). This also gives \(m + k < 3k\): The rebuild of \(w\), which costs \(O(m + k)\), costs \(O(k)\). The same holds for a node that has never been rebuilt: It was created as a leaf, so we can apply the argument with \(m = 1\).
We can thus pay for each rebuild by charging a constant amount to each of the \(k\) insertions that landed below the scapegoat since its last rebuild. A given insertion of \(x\) is charged at most once by each of its ancestors at the time it was inserted: Once an ancestor is rebuilt, it starts counting from zero again, and the nodes created by a rebuild above \(x\) only count insertions made after \(x\). Since the height is \(O(\log n)\), each insertion is charged \(O(\log n)\).
In total, \(n\) insertions cost \(O(n \log n)\) for the walks down (each one is \(O(\log n)\) deep), plus \(O(n \log n)\) for all the rebuilds, so \(O(\log n)\) per insertion on average. A single insertion can still cost \(\Theta(n)\), as Question 3 shows: Unlike AVL trees, scapegoat trees only guarantee a good average cost.
Common mistakes
Checking the depth against \(\log_{3/2}\) of the size before the insertion. With \(n = 3\) instead of 4, the trace already rebuilds when inserting 40.
Stopping at the first node whose children have different sizes, instead of the first one that breaks the \(\alpha\) limit. In Question 1, this wrongly picks 40 as the first scapegoat.
Rebuilding the whole tree instead of the scapegoat’s subtree, or picking the upper middle (\(\lfloor m/2 \rfloor + 1\)) when \(m\) is even.
In Question 4, charging every insertion to every rebuild, which only gives \(O(n)\) per insertion.
