05+-+Recursion+and+Iteration

05+-+Recursion+and+Iteration - EECS 280 Programming and...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
Recursion and Iteration EECS 280 Programming and Introductory Data Structures
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Review General Recursion A recursive problem is one that is defined in terms of itself. 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. Recursive problems usually follow the
Background image of page 2
Review Tail Recursion To solve the general recursive stack problem, we considered tail recursive programs which require only “constant space”. It doesn't matter how “large” the problem to be solved is, we only need a fixed number of stack frames to solve it. For example, in our tail-recursive factorial solution, we only need the first fact_helper stack frame, no matter the
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Recursion and Iteration Are they related? What we will show today: 1. For any tail-recursive function, you can write an iterative equivalent. In other words, tail- recursion is just iteration. 2. This equivalence is not true for general recursive programs. Note: There are some recursive programs that cannot be solved in constant space, including the tree traversal problem from Project 2.
Background image of page 4
We want to show that tail-recursion is “no more powerful” than iteration (i.e. for any computation that we could express tail- recursively, we could also express it iteratively). We do this by giving a method for converting an arbitrary tail-recursive function to an iterative one. This technique is called a “proof by construction”. (Tail-Recursion == Iteration) True
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
while (<boolean-condition>) <body-statement> Ø On entry, the condition is evaluated. If the condition is false, the loop terminates. Otherwise, the body statement is evaluated, and the loop is re-entered. do <body-statement> while (<boolean-condition>); Ø Reviewing Loops
Background image of page 6
for ( <init-statement>; <boolean-condition>; <post-body> ) <body-statement> Ø When a for-loop is entered for the first time, the init-statement is executed. Then, the condition is evaluated. If it’s false, the loop terminates. Otherwise, the body is evaluated, then the post-body is evaluated, and finally the loop is resumed at the point of Reviewing Loops
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
To help distinguish between the three types of loops: If you want to do something zero or more times, use “while”. If you want to do something one or more times, use “do-while”. If you want to do something a specific number of times, use “for”. It turns out that the “do” and “for” loops are just “syntactic sugar”. Reviewing Loops
Background image of page 8
Reviewing Loops Group Exercise for (<init-statement>; <boolean-condition>; <post-body>) <body-statement> do <body-statement> while (<boolean-condition>); while (<boolean-condition>)
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
There are five steps to the conversion of a tail-recursive function to an iterative one: 1. Copy the function’s type signature. 2.
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 38

05+-+Recursion+and+Iteration - EECS 280 Programming and...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online