



































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




































Lecture 4
1
2
3
1 2 3 1 2 3
Solution walkthrough:
1
2
3
Step 1
Step 2
Step 3
Step 4
1
2
3
1
2
3
1
2
3
Solution walkthrough: Step 5
Step 6
Step 7
Step 8
1
2
3
1
2
3
1 2 3 1 2 3 1
2
3
Solution walkthrough: Step 13
Step 14
Step 15
Step 16
The general problem is how to move a tower of size
(^) n (^) from peg
(^) o (^) (origin) to peg
d (destination). The general solution is to use a recursive algorithm.
disk fromThe base case applies to a tower of size 1. In this case, we simply move the
(^) o
to
d .
of sizeThe recursive case applies to tower sizes > 1. In this case, we move the tower
(^) n (^) - 1 from peg
o (^) to peg
(^) s (^) (spare) using peg
(^) d (^) as the spare. Then we
move the
n th disk from peg
(^) o
to peg
(^) d
. Finally, we move the tower of size
n -
(^) 1
(the one we temporarily parked on peg
(^) s ) to peg
(^) d,
(^) using peg
(^) o
as the spare.
areNotice that, like the fibonacci example and unlike the factorial example, there
two
(^) recursive function calls for this case.
o
s
d
(^1)
2 3
Less Rigorous Description of the General Solution
Let's consider the^ Recursive Call Tree
(^) n (^) = 4 case:
#Count from
to (spare)
o
s
d
} (^1)
(^23)
To move a tower of size
(^) n (^) from peg
(^) o (^) to peg
(^) d , using peg
(^) s (^) as a spare:
(^) n (^) = 1, move the disk from
(^) o (^) to (^) d .
(^) n (^) > 1,
2.1 Move the tower of size
(^) n (^) - 1 from
(^) o (^) to (^) s , using
(^) d (^) as the spare.
2.2 Move 1 disk from
(^) o (^) to (^) d .
2.3 Move the tower of size
(^) n (^) - 1 from
(^) s (^) to (^) d , using
(^) o (^) as the spare.
Let's consider the^ Recursive Call Tree
(^) n (^) = 4 case:
#Count from
to (spare)
o
s
d
} (^1)
(^23)
To move a tower of size
(^) n (^) from peg
(^) o (^) to peg
(^) d , using peg
(^) s (^) as a spare:
(^) n (^) = 1, move the disk from
(^) o (^) to (^) d .
(^) n (^) > 1,
2.1 Move the tower of size
(^) n (^) - 1 from
(^) o (^) to (^) s , using
(^) d (^) as the spare.
2.2 Move 1 disk from
(^) o (^) to (^) d .
2.3 Move the tower of size
(^) n (^) - 1 from
(^) s (^) to (^) d , using
(^) o (^) as the spare.
Let's consider the^ Recursive Call Tree
(^) n (^) = 4 case:
#Count from
to (spare)
There is no combine phase to the recursion. The base cases, in the order they are reached, give the moves of the solution.
4 3 2 1
o
s
d
} (^1)
(^23)
To move a tower of size
(^) n (^) from peg
(^) o (^) to peg
(^) d , using peg
(^) s (^) as a spare:
(^) n (^) = 1, move the disk from
(^) o (^) to (^) d .
(^) n (^) > 1,
2.1 Move the tower of size
(^) n (^) - 1 from
(^) o (^) to (^) s , using
(^) d (^) as the spare.
2.2 Move 1 disk from
(^) o (^) to (^) d .
2.3 Move the tower of size
(^) n (^) - 1 from
(^) s (^) to (^) d , using
(^) o (^) as the spare.
(^) n (^) disks from some peg
(^) o (^) to some
peg
(^) d (^) using spare peg
s .
o (^) to peg
(^) d .
You don’t need to keep track of where the disks are. Just print to the console that you are
moving a disk from
(^) o (^) to
(^) d
. There is only one possible disk this could refer to.
o
s
d
(^1)
2 3
solution? Where is this information stored? How is the program keeping track of where the disks are at various stages of Answer the following question: Run your class with at least two different tower sizes, both Create, compile, and run a class that:
(^)
3.
To move a tower of size
(^) n (^) from peg
(^) o (^) to peg
(^) d , using peg
(^) s (^) as a spare:
(^) n (^) = 1, move the disk from
(^) o (^) to
(^) d .
(^) n (^) > 1,
2.1 Move the tower of size
(^) n (^) - 1 from
(^) o (^) to (^) s , using
(^) d (^) as the spare.
2.2 Move 1 disk from
(^) o (^) to
(^) d .
2.3 Move the tower of size
(^) n
(^) - 1 from
(^) s (^) to
(^) d , using
(^) o (^) as the spare.
2.
2.
2.
To move a tower of size
(^) n (^) from peg
(^) o (^) to peg
(^) d , using peg
(^) s (^) as a spare:
(^) n (^) = 1, move the disk from
(^) o (^) to
(^) d .
(^) n (^) > 1,
2.1 Move the tower of size
(^) n (^) - 1 from
(^) o (^) to (^) s , using
(^) d (^) as the spare.
2.2 Move 1 disk from
(^) o (^) to
(^) d .
2.3 Move the tower of size
(^) n
(^) - 1 from
(^) s (^) to
(^) d , using
(^) o (^) as the spare.