05+-+Recursion+and+Iteration

05+-+Recursion+and+Iteration - 5/10/2009 1 Recursion and...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 5/10/2009 1 Recursion and Iteration Ve 280 Programming and Introductory Data Structures Recursion and Iteration Review General Recursion y A recursive problem is one that is defined in terms of itself. y 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. y Recursive problems usually follow the structure of an inductive proof. y Once the problem has been recursively stated, it is fairly simple to write a recursive program to solve it y Unfortunately, these types of recursive programs require one stack frame for every recursive call; this can be substantial. Review Tail Recursion y To solve the general recursive stack problem, we considered tail recursive programs which require only constant space. y It doesn't matter how large the problem to be solved is, we only need a fixed number of stack frames to solve it. y For example, in our tail-recursive factorial solution, we only need the first fact_helper stack frame, no matter the size of N. y This is also true when solving the problems with iteration . y 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? y 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. y Note: There are some recursive programs that cannot be solved in constant space, including the tree traversal problem from Project 2. y 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). y We do this by giving a method for converting an arbitrary (Tail-Recursion == Iteration) True We do this by giving a method for converting an arbitrary tail-recursive function to an iterative one. y This technique is called a proof by construction. 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. Reviewing Loops do <body-statement> while (<boolean-condition>); On entry, the body statement is evaluated followed by the condition. If the condition is false, the loop terminates. Otherwise, it is re-entered at the body statement. 5/10/2009 2 for ( <init-statement>; <boolean-condition>; <post-body> ) <body-statement> Reviewing Loops When a for-loop is entered for the first time, the init-statement is executed. Then, the condition is evaluated. If its 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 evaluating the boolean condition....
View Full Document

This note was uploaded on 10/26/2009 for the course EECS 280 taught by Professor Jeff during the Summer '09 term at University of Michigan-Dearborn.

Page1 / 7

05+-+Recursion+and+Iteration - 5/10/2009 1 Recursion and...

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

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