chapter 14 recursion. chapter objectives learn about recursive definitions explore the base case and...
TRANSCRIPT
Chapter 14
Recursion
Chapter Objectives
• Learn about recursive definitions
• Explore the base case and the general case of a recursive definition
• Learn about recursive algorithms
Chapter Objectives
• Learn about recursive methods
• Become aware of direct and indirect recursion
• Explore how to use recursive methods to implement recursive algorithms
Recursive Definitions
• Recursion– Process of solving a problem by reducing it to
smaller versions of itself
• Recursive definition– Definition in which a problem is expressed in
terms of a smaller version of itself– Has one or more base cases
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
Recursive Definitions
• General solution– Breaks problem into smaller versions of itself
• General case– Case in recursive definition in which a smaller
version of itself is called– Must eventually be reduced to a base case
Tracing a Recursive Method
Recursive method– Has unlimited copies of itself– Every recursive call has its own
• code• set of parameters• set of local variables
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
Recursive Definitions
• 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
Designing Recursive Methods
• Understand problem requirements
• Determine limiting conditions
• Identify base cases
Designing Recursive Methods
• Provide direct solution to each base case
• Identify general case(s)
• Provide solutions to general cases in terms of smaller versions of general cases
Recursive Factorial Method
public static int fact(int num){ if(num = = 0) return 1; else return num * fact(num – 1);}
Recursive Factorial Trace
Recursive Implementation: Largest Value in Array
Execution of largest(list, 0, 3)
Recursive Fibonacci
public static int rFibNum(int a, int b, int n){ if(n = = 1) return a; else if(n = = 2) return b; else return rFibNum(a, b, n -1) + rFibNum(a, b, n - 2);}
Execution of rFibonacci(2,3,5)
Towers of Hanoi Problem with Three Disks
Towers of Hanoi: Three Disk Solution
Towers of Hanoi: Three Disk Solution
Towers of Hanoi: Recursive Algorithm
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 easier– Recursive solution is often slower
Programming Example: Decimal to Binary
public static void decToBin(int num, int base){ if(num > 0) { decToBin(num/base, base); System.out.print(num % base); }}
Execution of decToBin(13,2)
Sierpinski Gaskets of Various Orders
Programming Example:Sierpinski Gasket
• Input: non-negative integer indicating level of Sierpinski gasket
• Output: triangle shape displaying a Sierpinski gasket of the given order
• Solution includes– Recursive method drawSierpinski– Method to find midpoint of two points
Recursive Algorithm to Draw Sierpinski Gasket
Programming Example: Sierpinski Gasket Input
Programming Example: Sierpinski Gasket Input
Chapter Summary
• Recursive Definitions
• Recursive Algorithms
• Recursive Methods
• Base cases
• General cases
Chapter Summary
• Tracing recursive methods
• Designing recursive methods
• Varieties of recursive methods
• Recursion vs. Iteration
• Various recursive functions explored