# Towers of Hanoi

## Presentation on theme: "Towers of Hanoi"— Presentation transcript:

Towers of Hanoi http://en.wikipedia.org/wiki/Image:Hanoiklein.jpg

Towers of Hanoi Move all the discs from the leftmost peg to the rightmost one. –Only one disc may be moved at a time. –A disc can be placed either on empty peg or on top of a larger disc. startfinish Demo

Recursive Solution Move n-1 smallest discs right. Move largest disc left. cyclic wrap-around

The Recursive Process

if (n == 0) return; move(n - 1, 1 - d); if (d == 1) System.out.println("Move disk " + n + “ right"); else System.out.println("Move disk " + n + “ left"); move(n - 1, 1 - d); move(3, 1) n = 3 d = 1

if (n == 0) return; move(n - 1, 1 - d); if (d == 1) System.out.println("Move disk " + n + “ right"); else System.out.println("Move disk " + n + “ left"); move(n - 1, 1 - d); move(3, 1) n = 3 d = 1

if (n == 0) return; move(n - 1, 1 - dir); if (dir == 1) System.out.println("Move disk " + n + " to right"); else System.out.println("Move disk " + n + " to left"); move(n - 1, 1 - dir); move(3, 1) n=3 dir = 1 if (n == 0) return; move(n - 1, 1 - d); if (d == 1) System.out.println("Move disk " + n + “ right"); else System.out.println("Move disk " + n + “ left"); move(n - 1, 1 - d); move(2, 0) n = 2 d = 0

if (n == 0) return; move(n - 1, 1 - dir); if (dir == 1) System.out.println("Move disk " + n + " to right"); else System.out.println("Move disk " + n + " to left"); move(n - 1, 1 - dir); move(3, 1) n=3 dir = 1 if (n == 0) return; move(n - 1, 1 - d); if (d == 1) System.out.println("Move disk " + n + “ right"); else System.out.println("Move disk " + n + “ left"); move(n - 1, 1 - d); move(2, 0) n = 2 d = 0

if (n == 0) return; move(n - 1, 1 - dir); if (dir == 1) System.out.println("Move disk " + n + " to right"); else System.out.println("Move disk " + n + " to left"); move(n - 1, 1 - dir); move(3, 1) n=3 dir = 1 if (n == 0) return; move(n - 1, 1 - dir); if (dir == 1) System.out.println("Move disk " + n + " to right"); else System.out.println("Move disk " + n + " to left"); move(n - 1, 1 - dir); move(2, 0) n = 2 dir = 0 if (n == 0) return; move(n - 1, 1 - d); if (d == 1) System.out.println("Move disk " + n + “ right"); else System.out.println("Move disk " + n + “ left"); move(n - 1, 1 - d); move(1, 1) n = 1 d = 1

if (n == 0) return; move(n - 1, 1 - dir); if (dir == 1) System.out.println("Move disk " + n + " to right"); else System.out.println("Move disk " + n + " to left"); move(n - 1, 1 - dir); move(3, 1) n=3 dir = 1 if (n == 0) return; move(n - 1, 1 - dir); if (dir == 1) System.out.println("Move disk " + n + " to right"); else System.out.println("Move disk " + n + " to left"); move(n - 1, 1 - dir); move(2, 0) n = 2 dir = 0 if (n == 0) return; move(n - 1, 1 - d); if (d == 1) System.out.println("Move disk " + n + “ right"); else System.out.println("Move disk " + n + “ left"); move(n - 1, 1 - d); move(1, 1) n = 1 d = 1

if (n == 0) return; move(n - 1, 1 - dir); if (dir == 1) System.out.println("Move disk " + n + " to right"); else System.out.println("Move disk " + n + " to left"); move(n - 1, 1 - dir); move(3, 1) n=3 dir = 1 if (n == 0) return; move(n - 1, 1 - dir); if (dir == 1) System.out.println("Move disk " + n + " to right"); else System.out.println("Move disk " + n + " to left"); move(n - 1, 1 - dir); move(2, 0) n = 2 dir = 0 if (n == 0) return; move(n - 1, 1 - dir); if (dir == 1) System.out.println("Move disk " + n + " to right"); else System.out.println("Move disk " + n + " to left"); move(n - 1, 1 - dir); move(1, 1) n = 1 dir = 1 if (n == 0) return; move(n - 1, 1 - d); if (d == 1) System.out.println("Move disk " + n + “ right"); else System.out.println("Move disk " + n + “ left"); move(n - 1, 1 - d); move(0, 0) n = 0 d = 0

if (n == 0) return; move(n - 1, 1 - dir); if (dir == 1) System.out.println("Move disk " + n + " to right"); else System.out.println("Move disk " + n + " to left"); move(n - 1, 1 - dir); move(3, 1) n=3 dir = 1 if (n == 0) return; move(n - 1, 1 - dir); if (dir == 1) System.out.println("Move disk " + n + " to right"); else System.out.println("Move disk " + n + " to left"); move(n - 1, 1 - dir); move(2, 0) n = 2 dir = 0 if (n == 0) return; move(n - 1, 1 - d); if (d == 1) System.out.println("Move disk " + n + “ right"); else System.out.println("Move disk " + n + “ left"); move(n - 1, 1 - d); move(1, 1) n = 1 d = 1

if (n == 0) return; move(n - 1, 1 - dir); if (dir == 1) System.out.println("Move disk " + n + " to right"); else System.out.println("Move disk " + n + " to left"); move(n - 1, 1 - dir); move(3, 1) n=3 dir = 1 if (n == 0) return; move(n - 1, 1 - dir); if (dir == 1) System.out.println("Move disk " + n + " to right"); else System.out.println("Move disk " + n + " to left"); move(n - 1, 1 - dir); move(2, 0) n = 2 dir = 0 if (n == 0) return; move(n - 1, 1 - d); if (d == 1) System.out.println("Move disk " + n + “ right"); else System.out.println("Move disk " + n + “ left"); move(n - 1, 1 - d); move(1, 1) n = 1 d = 1

if (n == 0) return; move(n - 1, 1 - dir); if (dir == 1) System.out.println("Move disk " + n + " to right"); else System.out.println("Move disk " + n + " to left"); move(n - 1, 1 - dir); move(3, 1) n=3 dir = 1 if (n == 0) return; move(n - 1, 1 - dir); if (dir == 1) System.out.println("Move disk " + n + " to right"); else System.out.println("Move disk " + n + " to left"); move(n - 1, 1 - dir); move(2, 0) n = 2 dir = 0 if (n == 0) return; move(n - 1, 1 - d); if (d == 1) System.out.println("Move disk " + n + “ right"); else System.out.println("Move disk " + n + “ left"); move(n - 1, 1 - d); move(1, 1) n = 1 d = 1 Print “Move disk 1 right”

if (n == 0) return; move(n - 1, 1 - dir); if (dir == 1) System.out.println("Move disk " + n + " to right"); else System.out.println("Move disk " + n + " to left"); move(n - 1, 1 - dir); move(3, 1) n=3 dir = 1 if (n == 0) return; move(n - 1, 1 - d); if (d == 1) System.out.println("Move disk " + n + “ right"); else System.out.println("Move disk " + n + “ left"); move(n - 1, 1 - d); move(2, 0) n = 2 d = 0

if (n == 0) return; move(n - 1, 1 - dir); if (dir == 1) System.out.println("Move disk " + n + " to right"); else System.out.println("Move disk " + n + " to left"); move(n - 1, 1 - dir); move(3, 1) n=3 dir = 1 if (n == 0) return; move(n - 1, 1 - d); if (d == 1) System.out.println("Move disk " + n + “ right"); else System.out.println("Move disk " + n + “ left"); move(n - 1, 1 - d); move(2, 0) n = 2 d = 0

if (n == 0) return; move(n - 1, 1 - dir); if (dir == 1) System.out.println("Move disk " + n + " to right"); else System.out.println("Move disk " + n + " to left"); move(n - 1, 1 - dir); move(3, 1) n=3 dir = 1 if (n == 0) return; move(n - 1, 1 - d); if (dir == 1) System.out.println("Move disk " + n + “ right"); else System.out.println("Move disk " + n + “ left"); move(n - 1, 1 - dir); move(2, 0) n = 2 d = 0 Print “Move disk 2 left”

if (n == 0) return; move(n - 1, 1 - dir); if (dir == 1) System.out.println("Move disk " + n + " to right"); else System.out.println("Move disk " + n + " to left"); move(n - 1, 1 - dir); move(3, 1) n=3 dir = 1 if (n == 0) return; move(n - 1, 1 - dir); if (dir == 1) System.out.println("Move disk " + n + " to right"); else System.out.println("Move disk " + n + " to left"); move(n - 1, 1 - dir); move(2, 0) n = 2 dir = 0 if (n == 0) return; move(n - 1, 1 - d); if (d == 1) System.out.println("Move disk " + n + “ right"); else System.out.println("Move disk " + n + “ left"); move(n - 1, 1 - d); move(1, 1) n = 1 d = 1

3, 0 2, 1 1, 0 2, 1 1, 0 1 left2 right 1 left 3 left 2 right 1 left n, left 1 14 2 7 3 4 6 5 9 10 12 11 17 18 20 19 23 24 26 25 13 816 21 27 22 28 15 Recursive Tree

Recursive Graphics

21

22 Htree H-tree of order n –Draw an H. –Recursively draw 4 H-trees of order n-1, one connected to each tip. and half the size order 1 order 2 order 3 tip size