Lect15A_lists_recursion

# Lect15A_lists_recursion - ITI 1121 Introduction to...

This preview shows pages 1–8. Sign up to view the full content.

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

View Full Document

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

View Full Document

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

View Full Document

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ITI 1121. Introduction to Computing II * Marcel Turcotte School of Information Technology and Engineering Version of March 20, 2010 Abstract • Recursive list processing (part I) * These lecture notes are meant to be looked at on a computer screen. Do not print them unless it is necessary. Recursion: reminders 1. The solution to a given problem can be obtained by combining the solution of sub-problem(s); 2. The sub-problems are of the same type (i.e. the same strategy applies); 3. The sub-problems are always smaller (converges); 4. There is a size of problem that can be solved trivially (no recursive calls). What problems have you solved using recursion? Calculating the factorial? Locating a value in array? Here recursion is used to traverse (singly) linked lists. Problem (intuition) Consider calculating the sum of the values of an array t . The whole problem consists of calculating the sum of the values, t [ k ] , for the interval k = 0 to length- 1 . Let’s call s the sum of all the values for the interval k = 1 to length- 1 . Furthermore, let’s assume that s has been pre-calculated. What is the solution to our problem? t [0] + s How to calculate s , the sum of all the values for the sub-interval k = 1 ... length- 1 . Similarly, except that the interval is smaller by one position. What size of problem can be solved trivially? An interval of size 1. Problem (continued) private static int sum( int t, int k ) { int s, result, length = t.length - k; if ( length == 1 ) { // Base case result = t[ k ]; } else { // General case int k1 = k+1; s = sum( t, k1 ); result = t[ k ] + s; } return result; } What would be the initial call? public static int sum( int t ) { return sum( t, 0 ); } public class Sum { private static int sum( int t, int k ) { if ( k == ( t.length - 1 ) ) { return t[ k ]; } return t[ k ] + sum( t, k+1 ); } public static int sum( int t ) { if ( t.length == 0 ) { throw new IllegalArgumentException(); } return sum( t, 0 ); } public static void main( String args ) { int t = { 1, 2, 3, 4, 5 }; System.out.println( sum( t ) ); } } Remarks It’s important that the size of the problems gets smaller with each successive call, otherwise this would create an infinite recursion (similar to infinite loops). To stop the recursion, there must be a size of problem such that the result can be computed directly (no recursive call). Let’s call the special cases where the result can be obtained directly the base cases , there is at least one but there can be more than one base case....
View Full Document

## This note was uploaded on 01/06/2011 for the course ITI 1121 taught by Professor Samaan during the Spring '10 term at University of Ottawa.

### Page1 / 31

Lect15A_lists_recursion - ITI 1121 Introduction to...

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

View Full Document
Ask a homework question - tutors are online