Question: Implement the two versions of the solveTower algorithm given in Segment 7.31 7.31) Before we write some pseudocode to describe the algorithm, we need to

Implement the two versions of the solveTower algorithm given in Segment 7.31

7.31) Before we write some pseudocode to describe the algorithm, we need to identify a base case. If only one disk is on pole 1, we can move it directly to pole 3 without using recursion. With this as the base case, the algorithm is as follows: Algorithm to move numberOfDisks disks from startPole to endPole using tempPole as a spare according to the rules of the Towers of Hanoi problem if (numberOfDisks == 1) Move disk from startPole to endPole else { Move all but the bottom disk from startPole to tempPole Move disk from startPole to endPole Move all disks from tempPole to endPole } At this point, we can develop the algorithm further by writing Algorithm solveTowers(numberOfDisks, startPole, tempPole, endPole) if (numberOfDisks == 1) Move disk from startPole to endPole else { solveTowers(numberOfDisks - 1, startPole, endPole, tempPole) Move disk from startPole to endPole solveTowers(numberOfDisks - 1, tempPole, startPole, endPole) } If we choose zero disks as the base case instead of one disk, we can simplify the algorithm a bit, as follows: Algorithm solveTowers(numberOfDisks, startPole, tempPole, endPole) // Version 2 if (numberOfDisks > 0) { solveTowers(numberOfDisks - 1, startPole, endPole, tempPole) Move disk from startPole to endPole solveTowers(numberOfDisks - 1, tempPole, startPole, endPole) } Although somewhat easier to write, the second version of the algorithm executes many more recursive calls. Both versions, however, make the same moves.

. Represent the towers by either

single characters or strings. Each method should display directions that indicate the moves that must be made.

Insert counters into each method to count the number of times it is called. These counters can be data fields of

the class that contains these methods. Compare the number of recursive calls made by each method for various

numbers of disks.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

To implement both versions of the solveTowers algorithm and compare their recursive calls follow these steps Step 1 Understanding the Problem Youre ta... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!