Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 14 Recursion
Chapter Objectives <ul><li>Learn about recursive definitions </li></ul><ul><li>Explore the base case and the general case ...
Chapter Objectives (continued) <ul><li>Learn about recursive methods </li></ul><ul><li>Become aware of direct and indirect...
Recursive Definitions <ul><li>Recursion </li></ul><ul><ul><li>Process of solving a problem by reducing it to smaller versi...
Recursive Definitions (continued) Java Programming: From Problem Analysis to Program Design, 3e
Recursive Definitions (continued) <ul><li>Recursive algorithm  </li></ul><ul><ul><li>Algorithm that finds the solution to ...
Recursive Definitions (continued) <ul><li>General solution </li></ul><ul><ul><li>Breaks problem into smaller versions of i...
Tracing a Recursive Method <ul><li>Recursive method </li></ul><ul><ul><li>Logically, you can think of a recursive method h...
Tracing a Recursive Method (continued) <ul><li>After completing a recursive call </li></ul><ul><ul><li>Control goes back t...
Recursive Definitions <ul><li>Directly recursive: a method that calls itself </li></ul><ul><li>Indirectly recursive: a met...
Designing Recursive Methods <ul><li>Understand problem requirements </li></ul><ul><li>Determine limiting conditions </li><...
Designing Recursive Methods (continued) <ul><li>Provide direct solution to each base case </li></ul><ul><li>Identify gener...
Recursive Factorial Method Java Programming: From Problem Analysis to Program Design, 3e public static int  fact( int  num...
Recursive Factorial Method (continued) Java Programming: From Problem Analysis to Program Design, 3e
Largest Value in Array Java Programming: From Problem Analysis to Program Design, 3e
Largest Value in Array (continued) Java Programming: From Problem Analysis to Program Design, 3e <ul><li>if  the size of t...
Largest Value in Array (continued) Java Programming: From Problem Analysis to Program Design, 3e public static int  larges...
Execution of  largest (list, 0, 3) Java Programming: From Problem Analysis to Program Design, 3e
Execution of  largest (list, 0, 3) Java Programming: From Problem Analysis to Program Design, 3e
Recursive Fibonacci Java Programming: From Problem Analysis to Program Design, 3e
Recursive Fibonacci (continued) Java Programming: From Problem Analysis to Program Design, 3e public static int  rFibNum( ...
Recursive Fibonacci (continued) Java Programming: From Problem Analysis to Program Design, 3e
Towers of Hanoi Problem with Three Disks Java Programming: From Problem Analysis to Program Design, 3e
Towers of Hanoi: Three Disk Solution Java Programming: From Problem Analysis to Program Design, 3e
Towers of Hanoi: Three Disk Solution (continued) Java Programming: From Problem Analysis to Program Design, 3e
Towers of Hanoi: Recursive Algorithm Java Programming: From Problem Analysis to Program Design, 3e public static void  mov...
Recursion or Iteration? <ul><li>Two ways to solve particular problem </li></ul><ul><ul><li>Iteration </li></ul></ul><ul><u...
Programming Example:  Decimal to Binary Java Programming: From Problem Analysis to Program Design, 3e public static void  ...
Execution of  decToBin(13, 2) Java Programming: From Problem Analysis to Program Design, 3e
Sierpinski Gaskets of Various Orders  Java Programming: From Problem Analysis to Program Design, 3e
Programming Example: Sierpinski Gasket <ul><li>Input: non-negative integer indicating level of Sierpinski gasket </li></ul...
Programming Example: Sierpinski Gasket (continued) Java Programming: From Problem Analysis to Program Design, 3e private v...
Programming Example: Sierpinski Gasket Input Java Programming: From Problem Analysis to Program Design, 3e
Chapter Summary <ul><li>Recursive definitions </li></ul><ul><li>Recursive algorithms </li></ul><ul><li>Recursive methods <...
Chapter Summary (continued) <ul><li>Tracing recursive methods </li></ul><ul><li>Designing recursive methods </li></ul><ul>...
Upcoming SlideShare
Loading in …5
×

Chap14

661 views

Published on

  • Be the first to comment

  • Be the first to like this

Chap14

  1. 1. Java Programming: From Problem Analysis to Program Design, 3e Chapter 14 Recursion
  2. 2. Chapter Objectives <ul><li>Learn about recursive definitions </li></ul><ul><li>Explore the base case and the general case of a recursive definition </li></ul><ul><li>Learn about recursive algorithms </li></ul>Java Programming: From Problem Analysis to Program Design, 3e
  3. 3. Chapter Objectives (continued) <ul><li>Learn about recursive methods </li></ul><ul><li>Become aware of direct and indirect recursion </li></ul><ul><li>Explore how to use recursive methods to implement recursive algorithms </li></ul>Java Programming: From Problem Analysis to Program Design, 3e
  4. 4. Recursive Definitions <ul><li>Recursion </li></ul><ul><ul><li>Process of solving a problem by reducing it to smaller versions of itself </li></ul></ul><ul><li>Recursive definition </li></ul><ul><ul><li>Definition in which a problem is expressed in terms of a smaller version of itself </li></ul></ul><ul><ul><li>Has one or more base cases </li></ul></ul>Java Programming: From Problem Analysis to Program Design, 3e
  5. 5. Recursive Definitions (continued) Java Programming: From Problem Analysis to Program Design, 3e
  6. 6. Recursive Definitions (continued) <ul><li>Recursive algorithm </li></ul><ul><ul><li>Algorithm that finds the solution to a given problem by reducing the problem to smaller versions of itself </li></ul></ul><ul><ul><li>Has one or more base cases </li></ul></ul><ul><ul><li>Implemented using recursive methods </li></ul></ul><ul><li>Recursive method </li></ul><ul><ul><li>Method that calls itself </li></ul></ul><ul><li>Base case </li></ul><ul><ul><li>Case in recursive definition in which the solution is obtained directly </li></ul></ul><ul><ul><li>Stops the recursion </li></ul></ul>Java Programming: From Problem Analysis to Program Design, 3e
  7. 7. Recursive Definitions (continued) <ul><li>General solution </li></ul><ul><ul><li>Breaks problem into smaller versions of itself </li></ul></ul><ul><li>General case </li></ul><ul><ul><li>Case in recursive definition in which a smaller version of itself is called </li></ul></ul><ul><ul><li>Must eventually be reduced to a base case </li></ul></ul>Java Programming: From Problem Analysis to Program Design, 3e
  8. 8. Tracing a Recursive Method <ul><li>Recursive method </li></ul><ul><ul><li>Logically, you can think of a recursive method having unlimited copies of itself </li></ul></ul><ul><ul><li>Every recursive call has its own </li></ul></ul><ul><ul><ul><li>Code </li></ul></ul></ul><ul><ul><ul><li>Set of parameters </li></ul></ul></ul><ul><ul><ul><li>Set of local variables </li></ul></ul></ul>Java Programming: From Problem Analysis to Program Design, 3e
  9. 9. Tracing a Recursive Method (continued) <ul><li>After completing a recursive call </li></ul><ul><ul><li>Control goes back to the calling environment </li></ul></ul><ul><ul><li>Recursive call must execute completely before control goes back to previous call </li></ul></ul><ul><ul><li>Execution in previous call begins from point immediately following recursive call </li></ul></ul>Java Programming: From Problem Analysis to Program Design, 3e
  10. 10. Recursive Definitions <ul><li>Directly recursive: a method that calls itself </li></ul><ul><li>Indirectly recursive: a method that calls another method and eventually results in the original method call </li></ul><ul><li>Tail recursive method: recursive method in which the last statement executed is the recursive call </li></ul><ul><li>Infinite recursion: the case where every recursive call results in another recursive call </li></ul>Java Programming: From Problem Analysis to Program Design, 3e
  11. 11. Designing Recursive Methods <ul><li>Understand problem requirements </li></ul><ul><li>Determine limiting conditions </li></ul><ul><li>Identify base cases </li></ul>Java Programming: From Problem Analysis to Program Design, 3e
  12. 12. Designing Recursive Methods (continued) <ul><li>Provide direct solution to each base case </li></ul><ul><li>Identify general case(s) </li></ul><ul><li>Provide solutions to general cases in terms of smaller versions of general cases </li></ul>Java Programming: From Problem Analysis to Program Design, 3e
  13. 13. Recursive Factorial Method Java Programming: From Problem Analysis to Program Design, 3e public static int fact( int num) { if (num = = 0) return 1; else return num * fact(num – 1); }
  14. 14. Recursive Factorial Method (continued) Java Programming: From Problem Analysis to Program Design, 3e
  15. 15. Largest Value in Array Java Programming: From Problem Analysis to Program Design, 3e
  16. 16. Largest Value in Array (continued) Java Programming: From Problem Analysis to Program Design, 3e <ul><li>if the size of the list is 1 </li></ul><ul><ul><li>- the largest element in the list is the only element in the list </li></ul></ul><ul><li>else </li></ul><ul><ul><li>- to find the largest element in list[a]...list[b] </li></ul></ul><ul><ul><li>a. find the largest element in list[a + 1]...list[b] </li></ul></ul><ul><li>and call it max </li></ul><ul><ul><li>b. compare list[a] and max </li></ul></ul><ul><li>if ( list[a] >= max ) </li></ul><ul><li>the largest element in list[a]...list[b] is </li></ul><ul><li>list[a] </li></ul><ul><li>else </li></ul><ul><li>the largest element in list[a]...list[b] is max </li></ul>
  17. 17. Largest Value in Array (continued) Java Programming: From Problem Analysis to Program Design, 3e 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 list[lowerIndex]; else return max; } }
  18. 18. Execution of largest (list, 0, 3) Java Programming: From Problem Analysis to Program Design, 3e
  19. 19. Execution of largest (list, 0, 3) Java Programming: From Problem Analysis to Program Design, 3e
  20. 20. Recursive Fibonacci Java Programming: From Problem Analysis to Program Design, 3e
  21. 21. Recursive Fibonacci (continued) Java Programming: From Problem Analysis to Program Design, 3e 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); }
  22. 22. Recursive Fibonacci (continued) Java Programming: From Problem Analysis to Program Design, 3e
  23. 23. Towers of Hanoi Problem with Three Disks Java Programming: From Problem Analysis to Program Design, 3e
  24. 24. Towers of Hanoi: Three Disk Solution Java Programming: From Problem Analysis to Program Design, 3e
  25. 25. Towers of Hanoi: Three Disk Solution (continued) Java Programming: From Problem Analysis to Program Design, 3e
  26. 26. Towers of Hanoi: Recursive Algorithm Java Programming: From Problem Analysis to Program Design, 3e public static void moveDisks( int count, int needle1, int needle3, int needle2) { if (count > 0) { moveDisks(count - 1, needle1, needle2, needle3); System.out.println( &quot; Move disk &quot; + count + &quot; from needle &quot; + needle1 + &quot; to needle &quot; + needle3 + &quot; . &quot; ); moveDisks(count - 1, needle2, needle3, needle1); } }
  27. 27. Recursion or Iteration? <ul><li>Two ways to solve particular problem </li></ul><ul><ul><li>Iteration </li></ul></ul><ul><ul><li>Recursion </li></ul></ul><ul><li>Iterative control structures: use looping to repeat a set of statements </li></ul><ul><li>Tradeoffs between two options </li></ul><ul><ul><li>Sometimes recursive solution is easier </li></ul></ul><ul><ul><li>Recursive solution is often slower </li></ul></ul>Java Programming: From Problem Analysis to Program Design, 3e
  28. 28. Programming Example: Decimal to Binary Java Programming: From Problem Analysis to Program Design, 3e public static void decToBin( int num, int base) { if (num > 0) { decToBin(num / base, base); System.out.print(num % base); } }
  29. 29. Execution of decToBin(13, 2) Java Programming: From Problem Analysis to Program Design, 3e
  30. 30. Sierpinski Gaskets of Various Orders Java Programming: From Problem Analysis to Program Design, 3e
  31. 31. Programming Example: Sierpinski Gasket <ul><li>Input: non-negative integer indicating level of Sierpinski gasket </li></ul><ul><li>Output: triangle shape displaying a Sierpinski gasket of the given order </li></ul><ul><li>Solution includes: </li></ul><ul><ul><li>Recursive method drawSierpinski </li></ul></ul><ul><ul><li>Method to find midpoint of two points </li></ul></ul>Java Programming: From Problem Analysis to Program Design, 3e
  32. 32. Programming Example: Sierpinski Gasket (continued) Java Programming: From Problem Analysis to Program Design, 3e private void drawSierpinski(Graphics g, int lev, Point p1, Point p2, Point p3) { Point midP1P2; Point midP2P3; Point midP3P1; if (lev > 0) { g.drawLine(p1.x, p1.y, p2.x, p2.y); g.drawLine(p2.x, p2.y, p3.x, p3.y); g.drawLine(p3.x, p3.y, p1.x, p1.y); midP1P2 = midPoint(p1, p2); midP2P3 = midPoint(p2, p3); midP3P1 = midPoint(p3, p1); drawSierpinski(g, lev - 1, p1, midP1P2, midP3P1); drawSierpinski(g, lev - 1, p2, midP2P3, midP1P2); drawSierpinski(g, lev - 1, p3, midP3P1, midP2P3); } }
  33. 33. Programming Example: Sierpinski Gasket Input Java Programming: From Problem Analysis to Program Design, 3e
  34. 34. Chapter Summary <ul><li>Recursive definitions </li></ul><ul><li>Recursive algorithms </li></ul><ul><li>Recursive methods </li></ul><ul><li>Base cases </li></ul><ul><li>General cases </li></ul>Java Programming: From Problem Analysis to Program Design, 3e
  35. 35. Chapter Summary (continued) <ul><li>Tracing recursive methods </li></ul><ul><li>Designing recursive methods </li></ul><ul><li>Varieties of recursive methods </li></ul><ul><li>Recursion vs. Iteration </li></ul><ul><li>Various recursive functions explored </li></ul>Java Programming: From Problem Analysis to Program Design, 3e

×