7-IntroToRecursion
7-IntroToRecursion
Lecture 7: Introduction to
Recursion
reading:
Programming Abstractions in C++, Chapter 5.4-5.6
Today's Topics
• Logistics:
• Handout in class: http://web.stanford.edu/class/cs106b//lectures/7-
IntroToRecursion/code/handout.pdf
• Writing a simple program all by yourself
• Serafini Due Wednesday, July 12th, noon
• One submission of two files (wordLadder, Ngrams)
• Recursion!
Today's Topics
• There was a question last quarter on Piazza:
• This is a great opportunity to write a quick program to test this yourself! Let's
see how we might do that!
A Little Demo
The Towers of Hanoi Puzzle
i o n !
c u r s
b y re
l v e d
e s o
a n b
his c
T
A Little Demo
By the end of today, we will be able to write this program, and
you may talk about the algorithm in section
Towers of Hanoi
Here is the way the game is played:
Towers of Hanoi
Here is the way the game is played:
Towers of Hanoi
Here is the way the game is played:
Towers of Hanoi
Here is the way the game is played:
Towers of Hanoi
Here is the way the game is played:
Towers of Hanoi
Here is the way the game is played:
Illegal move!
Towers of Hanoi
Here is the way the game is played:
Towers of Hanoi
Here is the way the game is played:
Towers of Hanoi
Here is the way the game is played:
etc.
What is Recursion?
What is Recursion?
Recursion:
1. Great style
2. Powerful tool
3. Master of control flow
Pedagogy
the original function call was to this one, so it returns 125, which is 53
Faster Method!
int power(int x, int exp) {
if(exp == 0) {
// base case
return 1;
} else {
if (exp % 2 == 1) {
// if exp is odd
return x * power(x, exp - 1);
} else {
// else, if exp is even
int y = power(x, exp / 2);
a r i n g
return y * y; s qu
n b y
}
t i a t i o !
} n e n ? ? ? - y a y
x p o i g O n ) -
} E B l o
( g
O
Mystery Recursion: Trace this function
int mystery(int n) {
What is the result
if (n < 10) {
of mystery(648)?
return n;
} else {
A. 8
int a = n/10;
B. 9
int b = n % 10;
C. 54
return mystery(a + b);
D. 72
}
E. 648
}
Mystery Recursion: Trace this function
isPalindrome("madam") → true
isPalindrome("racecar") → true
isPalindrome("step on no pets") → true
isPalindrome("Java") → false
isPalindrome("byebye") →false
Three Musts of Recursion
hailstone(int n)
•If the tower has size one, we can just move that single
disk from the source to the destination.
•If the tower has more than one, we have to use the
auxiliary spindle.
Back to Towers of Hanoi
•We can break the entire process down into very simple
steps -- not necessarily easy to think of steps, but
simple ones!
Back to Towers of Hanoi
Back to Towers of Hanoi
Back to Towers of Hanoi
Back to Towers of Hanoi
Back to Towers of Hanoi
s e
h e h
t t a c
a
e at ! e
ep s e
R ep ta g
st s
Back to Towers of Hanoi
Recap
•Recursion
•Break a problem into smaller subproblems of the same form, and call the same
function again on that smaller form.
•Super powerful programming tool
•Not always the perfect choice, but often a good one
•Some beautiful problems are solved recursively
• References:
• http://www.cs.utah.edu/~germain/PPS/Topics/recursion.html
• Why is iteration generally better than recursion? http://stackoverflow.com/a/
3093/561677
• Advanced Reading:
• Tail recursion: http://stackoverflow.com/questions/33923/what-is-tail-recursion
• Interesting story on the history of recursion in programming languages: http://
goo.gl/P6Einb
Extra Slides
Converting Decimal to Binary