Java Programming: Chapter 9: Recursion Second Edition From Problem Analysis to Program Design (Chapter 14), Second Edition
Chapter Objectives Learn about recursive definitions. Explore the base case and the general case of a recursive definition. Learn about recursive algorithms. Learn about recursive methods. Learn about how to use recursive methods to implement recursive algorithms. Java Programming: From Problem Analysis to Program Design, Second Edition
Introduction // Program prints an array. public class Print { // print array iteratively public static void printIterative( int[] array ) { for (int i=0; i< array.length; ++i ) System.out.print(array[i] + " "); } // end method printArray Java Programming: From Problem Analysis to Program Design, Second Edition
// recursively print array private static void printArrayHelper( int[] array, int startIndex ) { if ( startIndex == array.length ) return; // print elements in normal order System.out.print( array[ startIndex ] + " " ); printArrayHelper( array, startIndex + 1 ); } // end method printArrayHelper Java Programming: From Problem Analysis to Program Design, Second Edition
// recursively print array in reverse order private static void printArrayReverseHelper( int[] array, int startIndex ) { if ( startIndex == array.length ) return; // print elements in reverse order printArrayReverseHelper( array, startIndex + 1 ); System.out.print( array[ startIndex ] + " " ); } // end method printArrayHelper Java Programming: From Problem Analysis to Program Design, Second Edition
public static void main( String args[] ) { int array[] = { 8, 22, 88, 34, 84, 21, 94 }; System.out.print( "Array is: " ); printArrayHelper( array, 0 ); // print array System.out.print( "Array in reverse order is: " ); printArrayReverseHelper( array , 0 );// print array in reverse order } Java Programming: From Problem Analysis to Program Design, Second Edition
Recursive Definitions Recursion: Process of solving a problem by reducing it to smaller versions of itself. Factorial of an integer: 0! = 1 n! = n * (n-1)! If n > 0 Java Programming: From Problem Analysis to Program Design, Second Edition
Recursive Definitions Definition in which a problem is expressed in terms of a smaller version of itself. Has one or more base cases. Java Programming: From Problem Analysis to Program Design, Second Edition
Recursive Definitions Recursive algorithm: Algorithm that finds the solution to a given problem by reducing the problem to smaller versions of itself. Has one or more base cases. Implemented using recursive methods. Recursive method: Method that calls itself. Base case: Case in recursive definition in which the solution is obtained directly. Stops the recursion. Java Programming: From Problem Analysis to Program Design, Second Edition
Recursive Factorial Method public static int fact (int num) { if (num == 0) return 1; else return num * fact(num-1); } Test to stop or continue. End case: recursion stops. Recursive case: recursion continues. Java Programming: From Problem Analysis to Program Design, Second Edition
Recursive Factorial Method Java Programming: From Problem Analysis to Program Design, Second Edition
Tracing a Recursive Method Has unlimited copies of itself. Every recursive call has its own: Code Set of parameters Set of local variables Java Programming: From Problem Analysis to Program Design, Second Edition
Tracing a Recursive Method After completing a recursive call: Control goes back to the calling environment. Recursive call must execute completely before control goes back to previous call. Execution in previous call begins from point immediately following recursive call. Java Programming: From Problem Analysis to Program Design, Second Edition
Designing Recursive Methods Understand problem requirements. Determine limiting conditions.( ex:the number of elements in a list) Identify base cases. Java Programming: From Problem Analysis to Program Design, Second Edition
Designing Recursive Methods Provide direct solution to each base case. Identify general cases. Provide solutions to general cases in terms of smaller versions of general cases. Java Programming: From Problem Analysis to Program Design, Second Edition
Largest Value in Array public static int largest(int[] list, int lowerIndex, int upperIndex) { int max; if (lowerIndex == upperIndex) return list[lowerIndex]; else max = largest(list, lowerIndex + 1, upperIndex); if (list[lowerIndex] >= max) return max; } Java Programming: From Problem Analysis to Program Design, Second Edition
Largest Value in Array Java Programming: From Problem Analysis to Program Design, Second Edition
Recursion or Iteration? Two ways to solve particular problem: Iteration Recursion Iterative control structures use looping to repeat a set of statements. Tradeoffs between two options: Sometimes recursive solution is more natural and easier to understand. Recursive solution is often slower. Java Programming: From Problem Analysis to Program Design, Second Edition
When Not to Use Recursion When recursive algorithms are designed carelessly, it can lead to very inefficient and unacceptable solutions. For example, consider the following: public int fibonacci( int N ) { if (N == 0 || N == 1) { return 1; } else { return fibonacci(N-1) + fibonacci(N-2); } Java Programming: From Problem Analysis to Program Design, Second Edition
Excessive Repetition Recursive Fibonacci ends up repeating the same computation numerous times. Java Programming: From Problem Analysis to Program Design, Second Edition
Nonrecursive Fibonacci public int fibonacci( int N ) { int fibN, fibN1, fibN2, cnt; if (N == 0 || N == 1 ) { return 1; } else { fibN1 = fibN2 = 1; cnt = 2; while ( cnt <= N ) { fibN = fibN1 + fibN2; //get the next fib no. fibN1 = fibN2; fibN2 = fibN; cnt ++; } return fibN; Java Programming: From Problem Analysis to Program Design, Second Edition