Recursion and Iteration
EECS 280
Programming and Introductory Data Structures
Review
General Recursion
circle5
A recursive problem is one that is defined in terms of itself.
circle5
A recursive problem has two important features:
1.
There be one or more trivial base cases.
2.
There be a way to solve “bigger” instances by first solving “smaller”
instances, then performing a simple computation.
circle5
Recursive problems usually follow the structure of an inductive
proof.
circle5
Once the problem has been recursively stated, it is fairly simple to
write a recursive program to solve it
circle5
Unfortunately, these types of recursive programs require one stack
frame for every recursive call; this can be substantial.
Review
Tail Recursion
circle5
To solve the general recursive stack problem, we considered tail
recursive programs which require only “constant space”.
circle5
It doesn't matter how “large” the problem to be solved is, we only
need a fixed number of stack frames to solve it.
circle5
For example, in our tailrecursive factorial solution, we only need
the first
fact_helper
stack frame, no matter the size of N.
circle5
This is also true when solving the problems with
iteration
.
circle5
In fact, the two versions of
factorial()
we've seen are very,
very similar – that similarity is no accident!
Recursion and Iteration
Are they related?
circle5
What we will show today:
1.
For
any
tailrecursive function, you can write an iterative
equivalent.
In other words, tailrecursion is
just
iteration.
2.
This equivalence is
not
true for general recursive programs.
circle5
Note:
There are some recursive programs that cannot be
solved in constant space, including the tree traversal problem
from Project 2.
circle5
We want to show that tailrecursion is “no more powerful”
than iteration (i.e. for any computation that we could express
tailrecursively, we could also express it iteratively).
circle5
We do this by giving a method for converting an arbitrary
tailrecursive function to an iterative one.
circle5
This technique is called a “proof by construction”.
(TailRecursion == Iteration)
True
while (<booleancondition>)
<bodystatement>
head2right
On entry, the condition is evaluated.
If the condition is false,
the loop terminates.
Otherwise, the body statement is
evaluated, and the loop is reentered.
do
<bodystatement>
while (<booleancondition>);
head2right
On entry, the body statement is evaluated followed by the
condition.
If the condition is false, the loop terminates.
Otherwise, it is reentered at the body statement.
Reviewing Loops
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
for ( <initstatement>;
<booleancondition>;
<postbody> )
<bodystatement>
head2right
When a forloop is entered for the first time, the initstatement
is executed.
Then, the condition is evaluated.
If it’s false, the
loop terminates.
Otherwise, the body is evaluated, then the
postbody is evaluated, and finally the loop is resumed at the
point of evaluating the boolean condition.
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '08
 NOBLE
 Recursion, Tail recursion, Control flow

Click to edit the document details