Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.

Similar presentations


Presentation on theme: "Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn."— Presentation transcript:

1 Chapter 14 Recursion

2 Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn about recursive algorithms

3 Chapter Objectives Learn about recursive methods Become aware of direct and indirect recursion Explore how to use recursive methods to implement recursive algorithms

4 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

5 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

6 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

7 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

8 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

9 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

10 Designing Recursive Methods Understand problem requirements Determine limiting conditions Identify base cases

11 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

12 Recursive Factorial Method public static int fact(int num) { if(num = = 0) return 1; else return num * fact(num – 1); }

13 Recursive Factorial Trace

14 Recursive Implementation: Largest Value in Array

15 Execution of largest(list, 0, 3)

16 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); }

17 Execution of rFibonacci(2,3,5)

18 Towers of Hanoi Problem with Three Disks

19 Towers of Hanoi: Three Disk Solution

20

21 Towers of Hanoi: Recursive Algorithm

22 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

23 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); }

24 Execution of decToBin(13,2)

25 Sierpinski Gaskets of Various Orders

26 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

27 Recursive Algorithm to Draw Sierpinski Gasket

28 Programming Example: Sierpinski Gasket Input

29

30 Chapter Summary Recursive Definitions Recursive Algorithms Recursive Methods Base cases General cases

31 Chapter Summary Tracing recursive methods Designing recursive methods Varieties of recursive methods Recursion vs. Iteration Various recursive functions explored


Download ppt "Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn."

Similar presentations


Ads by Google