TAOCP Vol 1: Fundamental Algorithms · Week 55
TAOCP 2.3.1 Traversing Binary Trees
Updated
Summary
TAOCP 2.3.1 Traversing Binary Trees: Binary trees, the three traversal orders (preorder, inorder, postorder), traversal with an explicit stack instead of recursion, and threaded trees where spare null links point to inorder successors. Budget about 9 hours; do exercises 2.3-21, 2 and 4.
See it
All visualsStep through at your own pace; the arrow keys work too.
Inorder so far
DBAEG
Stack (the path from the root)
ACEG
Back at G after its left subtree (empty): inorder visits G now, between its two subtrees.
This week's steps
3 stepsRead §2.3.1 Traversing Binary Trees
Read the opening pages of 2.3 (Trees) first: the definitions, the drawing conventions, Dewey notation, forests and Lists.
Start with Ex 2.3-21, Ex 2, Ex 4; log what you solve below.
Write down one thing you'd explain to someone
A few sentences in your own words in Your notes: the idea, why it works, an example. 30 words ticks this.
Sign in to track this: tick each step off and it counts toward your week.
Exercises
This section has 37 exercises, rated 00 to HM41, and Knuth marks nine of them with the triangle for recommended. The 2.3 opener (Trees) has its own 22 exercises, rated 00 to HM32; one pick from it is listed as 2.3-21. In 2.3.1 itself, the traversal and threading exercises are the classics.
Worth your time
Exercise 2.3-21M22
From the 2.3 opener: count leaves from the numbers of nodes of each degree.
Exercise 420▶ Recommended
Look at a mirror-image traversal order and relate it to the standard ones.
Exercise 722
Rebuild a binary tree from its preorder and inorder. The interview favorite, from the source.
Exercise 2133▶ Recommended
Traverse in inorder with no stack by temporarily rewiring links. The stretch.
Your log
Sign in to log the exercises you work: number, rating, how far you got.
Your notes
Only youSign in to keep private notes on this guide, in Markdown. They stay yours: never on your profile, and you can export them all.
Discussion
0 postsQuestions about the reading go here; each exercise has its own thread below. Hints over answers.
No posts yet. Ask a question or share a tip.
Sign in to postAnyone can read. Posting needs an account with a confirmed email.
Exercise threads
One thread per exercise. Open one to ask for a nudge or check your thinking.
Exercise 211No posts yet
Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler:
||like this||or a```spoilerblock.Sign in to postAnyone can read. Posting needs an account with a confirmed email.
Exercise 2.3-21M22No posts yet
Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler:
||like this||or a```spoilerblock.Sign in to postAnyone can read. Posting needs an account with a confirmed email.
Exercise 420No posts yet
Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler:
||like this||or a```spoilerblock.Sign in to postAnyone can read. Posting needs an account with a confirmed email.
Exercise 722No posts yet
Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler:
||like this||or a```spoilerblock.Sign in to postAnyone can read. Posting needs an account with a confirmed email.
Exercise 1324No posts yet
Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler:
||like this||or a```spoilerblock.Sign in to postAnyone can read. Posting needs an account with a confirmed email.
Exercise 1722No posts yet
Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler:
||like this||or a```spoilerblock.Sign in to postAnyone can read. Posting needs an account with a confirmed email.
Exercise 2133No posts yet
Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler:
||like this||or a```spoilerblock.Sign in to postAnyone can read. Posting needs an account with a confirmed email.
Exercise 3022No posts yet
Hints over answers. Say what you tried; put an answer or a full solution inside a spoiler:
||like this||or a```spoilerblock.Sign in to postAnyone can read. Posting needs an account with a confirmed email.
Reference
Why it matters
Tree traversal is everywhere: ASTs in your interpreters, DOM trees, file systems. Converting recursion to an explicit stack is a practical skill when recursion depth is a risk.
Before you start
- 2.2.3 Linked Allocation
- Recursion
How to approach it
Read the opening pages of 2.3 (Trees) first: the definitions, the drawing conventions, Dewey notation, forests and Lists. The curriculum has no separate week for them and 2.3.1 assumes them. Then implement inorder traversal with an explicit stack and confirm it matches the recursive version on random trees. Build a threaded tree and walk it without any stack. Compare to the AST visitors from jlox.
Common traps
- Confusing the order in which you push left and right children.
- Threads and real links look identical without the tag bit. Keep the tag.