Download presentation
Presentation is loading. Please wait.
1
Towers of Hanoi http://en.wikipedia.org/wiki/Image:Hanoiklein.jpg
2
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
3
Recursive Solution Move n-1 smallest discs right. Move largest disc left. cyclic wrap-around
4
The Recursive Process
5
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
6
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
7
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
8
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
9
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
10
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
11
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
12
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
13
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
14
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”
15
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
16
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
17
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”
18
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
19
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
20
Recursive Graphics
21
21
22
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.