Week 11 - Recursion
Week 11 - Recursion
Recursion
John Lewis
William Loftus
Recursive Thinking
Recursive Programming
Using Recursion
A List is a: number
or a: number comma List
• That is, a List is defined to be a single number, or a
number followed by a comma followed by a List
• The concept of a List is used to define itself
5!
120
5 * 4!
24
4 * 3!
6
3 * 2!
2
2 * 1!
1
5 * 1 = 5
5 * n = 5 + (5 * (n-1))
Recursive Thinking
Recursive Programming
Using Recursion
if (num == 1)
result = 1;
else
result = num + sum (n-1);
return result;
}
Recursive Thinking
Recursive Programming
Using Recursion
System.out.println(labyrinth);
if (labyrinth.traverse(0, 0))
System.out.println("The maze was successfully traversed!");
else
System.out.println("There is no possible path.");
System.out.println(labyrinth);
}
}
continued
//-----------------------------------------------------------------
// Attempts to recursively traverse the maze. Inserts special
// characters indicating locations that have been tried and that
// eventually become part of the solution.
//-----------------------------------------------------------------
public boolean traverse(int row, int column)
{
boolean done = false;
if (valid(row, column))
{
grid[row][column] = TRIED; // this cell has been tried
continued
Copyright © 2018 Pearson Education, Ltd.
continued
return done;
}
//-----------------------------------------------------------------
// Determines if a specific location is valid.
//-----------------------------------------------------------------
private boolean valid(int row, int column)
{
boolean result = false;
return result;
}
continued
//-----------------------------------------------------------------
// Returns the maze as a string.
//-----------------------------------------------------------------
public String toString()
{
String result = "\n";
return result;
}
}
Move 2 Move 3
Towers of Hanoi
Move 4 Move 5
//-----------------------------------------------------------------
// Sets up the puzzle with the specified number of disks.
//-----------------------------------------------------------------
public TowersOfHanoi (int disks)
{
totalDisks = disks;
}
//-----------------------------------------------------------------
// Performs the initial call to moveTower to solve the puzzle.
// Moves the disks from tower 1 to tower 3 using tower 2.
//-----------------------------------------------------------------
public void solve ()
{
moveTower (totalDisks, 1, 3, 2);
}
continued
//-----------------------------------------------------------------
// Moves the specified number of disks from one tower to another
// by moving a subtower of n-1 disks out of the way, moving one
// disk, then moving the subtower back. Base case of 1 disk.
//-----------------------------------------------------------------
private void moveTower (int numDisks, int start, int end, int temp)
{
if (numDisks == 1)
moveOneDisk (start, end);
else
{
moveTower (numDisks-1, start, temp, end);
moveOneDisk (start, end);
moveTower (numDisks-1, temp, end, start);
}
}
//-----------------------------------------------------------------
// Prints instructions to move one disk from the specified start
// tower to the specified end tower.
//-----------------------------------------------------------------
private void moveOneDisk (int start, int end)
{
System.out.println ("Move one disk from " + start + " to " +
end);
}
}
Copyright © 2012 Pearson Education, Inc.
//********************************************************************
// SolveTowers.java Author: Lewis/Loftus
//
// Demonstrates recursion.
//********************************************************************
towers.solve();
}
}