Comp 249 Programming Methodology: Chapter 11
Comp 249 Programming Methodology: Chapter 11
Programming Methodology
Chapter 11 – Recursion
Prof. Aiman Hanna
Department of Computer Science & Software Engineering
Concordia University, Montreal, Canada
These slides have been extracted, modified and updated from original slides of Absolute Java 3rd Edition by
Savitch; which has originally been prepared by Rose Williams of Binghamton University. Absolute Java is
published by Pearson Education / Addison-Wesley.
See Recursion2.java
Copyright © 2008 Pearson Addison-
Wesley. All rights reserved 11-10
Stacks for Recursion
To keep track of recursion (and other things), most
computer systems use a stack
A stack is a very specialized kind of memory structure
analogous to a container that holds stack of paper
Getting an older sheet out from the stack would require that
all the ones on top be first removed (more accurately
removed and thrown away)
Copyright © 2008 Pearson Addison-
Wesley. All rights reserved 11-11
Stacks for Recursion
Since the last sheet put on the stack is the first sheet
that can be taken off the stack, a stack is called a last-
in/first-out memory structure (LIFO)
Following the previous analogy, to keep track of
recursion, whenever a method is called, a new sheet of
paper is taken
The method definition is copied onto this sheet, and the
arguments are plugged in for the method parameters
The computer starts to execute the method body
When it encounters a recursive call, it stops the computation
in order to make the recursive call
It writes information about the current method on the sheet of
paper, and places it on the stack
Copyright © 2008 Pearson Addison-
Wesley. All rights reserved 11-12
Stacks for Recursion
A new sheet of paper is then used for the recursive
call
The computer writes a second copy of the method,
plugs in the arguments, and starts to execute its body
Then the computer goes to the top sheet of paper on the stack
This sheet contains the partially completed computation
that is waiting for the recursive computation that just
ended
Now it is possible to proceed with that suspended
computation
Copyright © 2008 Pearson Addison-
Wesley. All rights reserved 11-14
Stacks for Recursion
After the suspended computation ends, the computer
discards its corresponding sheet of paper (the one on
top)
If there are too many, then the stack will attempt to grow
beyond its limit, resulting in an error condition known as a
stack overflow
See Recursion3.java
Copyright © 2008 Pearson Addison-
Wesley. All rights reserved 11-17
Recursion Versus Iteration
Recursion is not absolutely necessary
Any task that can be done using recursion can also be done in
a nonrecursive manner
A nonrecursive version of a method is called an iterative version
2. Each stopping case returns the correct value for that case
3. For the cases that involve recursion: if all recursive calls return
the correct value, then the final value returned by the method is
the correct value
If the entire array (or array segment) has been searched in this
way without finding the value, then it is not in the array, so
return -1 (indicating that there is no index for that value)
See Recursion6.java
See Recursion7.java
Copyright © 2008 Pearson Addison-
Wesley. All rights reserved 11-34