TAOCP Vol 1: Fundamental Algorithms · Week 55

TAOCP 2.3.1 Traversing Binary Trees

Updated

About 9 hours37 exercises, rated 00 to HM41

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.

Flashcards 15 cards for Anki and AnkiMobile · all decks

Anki deck.tsv

Step through at your own pace; the arrow keys work too.

TAOCP 2.3.1 Traversing a binary tree One walk touches each node three times; preorder, inorder and postorder differ only in which touch visits.

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 steps
  1. Read §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.

  2. Work the exercises (8 picked)

    Start with Ex 2.3-21, Ex 2, Ex 4; log what you solve below.

  3. 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 211

    List a small tree's nodes in all three orders.

    Discuss

  • Exercise 2.3-21M22

    From the 2.3 opener: count leaves from the numbers of nodes of each degree.

    Discuss

  • Exercise 420▶ Recommended

    Look at a mirror-image traversal order and relate it to the standard ones.

    Discuss

  • Exercise 722

    Rebuild a binary tree from its preorder and inorder. The interview favorite, from the source.

    Discuss

  • Exercise 1324

    Write a stack-based postorder traversal. The tricky one.

    Discuss

  • Exercise 1722▶ Recommended

    Find the preorder successor in a threaded tree.

    Discuss

  • Exercise 2133▶ Recommended

    Traverse in inorder with no stack by temporarily rewiring links. The stretch.

    Discuss

  • Exercise 3022▶ Recommended

    Thread an unthreaded tree.

    Discuss

Your log

Sign in to log the exercises you work: number, rating, how far you got.

Your notes

Only you

Sign 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 posts

Questions 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 ```spoiler block.

    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 ```spoiler block.

    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 ```spoiler block.

    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 ```spoiler block.

    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 ```spoiler block.

    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 ```spoiler block.

    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 ```spoiler block.

    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 ```spoiler block.

    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.

Spot a mistake? Report it

Back to week 55