Académique Documents
Professionnel Documents
Culture Documents
Learn about recursive definitions Explore the base case and the general case of a
recursive definition Discover what a recursive algorithm is Learn about recursive methods Explore how to use recursive methods to implement recursive algorithms Learn how recursion implements backtracking
2
Recursion
Process of solving a problem by reducing it to
Recursive definition
Definition in which a problem is expressed in terms
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
Data Structures Using Java
General solution
Breaks problem into smaller versions of itself
General case
Case in recursive definition in which a smaller
Directly recursive: a method that calls itself Indirectly recursive: a method that calls another
method and eventually results in the original method call Tail recursive method: recursive method in which the last statement executed is the recursive call Infinite recursion: the case where every recursive call results in another recursive call
kind of programming in which a procedure may call itself as a subroutine. Recursion is a problem-solving approach that can be used to generate simple solutions to certain kinds of problems that would be difficult to solve in other ways Recursion splits a problem into one or more simpler versions of itself
There are similarities between recursion and iteration In iteration, a loop repetition condition determines
In recursion, the condition usually tests for a base case You can always write an iterative solution to a problem
that is solvable by recursion Recursive code may be simpler than an iterative algorithm and thus easier to write, read, and debug But in some cases, a recursive approach is not so efficient because it consumes so much memoy.
Iteration Recursive
problem, that refers to itself Show everything in a folder and all it subfolders:
1. show everything in top folder 2. show everything in each subfolder in the same manner
if (base case) { return something easily computed } else { divide problem into pieces return something calculated from the solution to each pieces }
Recursive method: method that calls itself A recursive method has two cases:
Base case : case in recursive definition in which the
solution is obtained directly or a simple case of the problem that can be answered directly (does not use recursion)
which a smaller version of itself is called or a more complicated case of the problem, that isn't easy to answer directly, but can be expressed elegantly with recursion (makes a recursive call to help compute the overall answer)
Understand problem requirements Determine limiting conditions Identify base cases Provide direct solution to each base case Identify general case(s) Provide solutions to general cases in terms of smaller
versions of itself
19
Recursive method
Has unlimited copies of itself Every recursive call has its own code own set of parameters own set of local variables
20
control goes back to previous call Execution in previous call begins from point immediately following recursive call
21
ordering. 2. if all words in the definition are known to you, stop. 3. else, for each unknown word in the definition, look up that word
The base case is an empty string : its length is 0 The recursive case, each string has two parts: the first
character and the rest of the string. If we can find the length of the rest of the string, we can then add 1(for the first character) to get the length of the larger string. Example: the length of abcde is 1 + the length of bcde
if the string is empty(has no characters) the length is 0 else the length is 1 plus the length of the string that excludes the first character.
public static int length( String str ) { if ( str == null || str.equals("")) return 0; else return 1 + length(str.substring(1)); }
public static int factorial( int n ) { if ( n == 0 ) return 1; else return n * factorial( n-1 ); }
Recursive proof
Verify the base case is recognized and solved correctly Verify that each recursive case makes progress towards
the base case Verify that if all smaller problems are solved correctly, then the original problem is also solved correctly
public static int square( int N ) { if ( N == 1 ) return 1; else return square(N-1) + 2*N -1; }
public static int sum( int n ) { if ( n == 1 ) return 1; else return n * n + (sum( n-1 )); }
recursive definition for this method. Trace the recursive function for n = 4.
public static int Calculate(int x, int n) { if (n == 0) return 1; else return x * Calculate(x, n-1); }
Tracing recursive methods, For each call below, indicate what value is returned: mystery1(6, 13) mystery1(14, 10) mystery1(37, 10) mystery1(8, 2) mystery1(50, 7)
public static void mystery2(int n) { if (n <= 1) { System.out.print(n); } else { mystery2(n / 2); System.out.print(", " + n); } }
Tracing recursive methods, For each call below, indicate what value is returned: mystery2(1) mystery2(2) mystery2(3) mystery2(4) mystery2(16) mystery2(30) mystery2(100)
public static int mystery3(int n) { if (n < 0) { return -mystery3(-n); } else if (n < 10) { return n; } else { return mystery3(n/10 + n % 10); } }
Tracing recursive methods, For each call below, indicate what value is returned: mystery3(6) mystery3(17) mystery3(259) mystery3(977) mystery3(-479)
public static void mystery4(String s) { if (s.length() > 0) { System.out.print(s.charAt(0)); if (s.length() % 2 == 0) { mystery4(s.substring(0, s.length() - 1)); } else { mystery4(s.substring(1, s.length())); } System.out.print(s.charAt(s.length() - 1)); } }
Tracing recursive methods, For each call below, indicate what value is returned: mystery4(") mystery4("a) mystery4("ab) mystery4("bc) mystery4("abcd)
Past Year
Question 7 Sept 2011 Question 6 b) Apr 2010 Question 5- Oct 2010