Data Structures · Assignment · Problem 4

Problem 4: Tower of Hanoi — a recursive solution

A (source) → C (destination), B (auxiliary); solve it with the recursive hanoi() and print every move

🎯 The three rods

disk 1 (smallest, on top) disk 2 disk 3 disk 4 disk 5 (largest, at the bottom — when n = 5) the disk that just moved move only the top disk of a rod · never place a larger disk on a smaller one

💬 Current step

💻 Lecture C++ code (whole program)

Yellow = current line. Recursion in three steps: ① n−1 disks from→aux ② disk n from→to ③ n−1 disks aux→to. The green box below is the cout terminal.

🥞 Recursion call stack hanoi(n, from, aux, to)

Speed
Shortcuts: Space play/pause · ←→ step · R reset · E result