recursive methods
play

Recursive Methods Noter ch.2 Recursive Methods Recursive problem - PowerPoint PPT Presentation

Recursive Methods Noter ch.2 Recursive Methods Recursive problem solution Problems that are naturally solved by recursion Examples: Recursive function: Fibonacci numbers Recursive graphics: Fractals Mutual recursion:


  1. Recursive Methods Noter ch.2

  2. Recursive Methods • Recursive problem solution – Problems that are naturally solved by recursion • Examples: – Recursive function: Fibonacci numbers – Recursive graphics: Fractals – Mutual recursion: Expression evaluation – Randomization and recursion: Random plants/trees • General aspects – Termination – Recursion versus iteration: simplicity vs efficiency

  3. Recursion in trees and flowers 3 smaller trees/branches = Tree trunk

  4. Natural Recursion: Derivative of Rational Function � � ����� � ��� �→�

  5. QUIZ Factorial function: n! = 1 * 2 * 3 * ... * (n-1) * n Recursive definition What is the proper recursive definition of the factorial function? (Assume n ≥ 1) 1. n! = n * (n-1)! (n-1)! For n > 1 2. n! = 1 For n = 1 (n-1)! For n > 1 3. n! = n-1 For n = 1 n*(n-1)! For n > 1 4. n! = 1 For n = 1 n*(n-1) For n > 1 5. n! = (n-1)! For n = 1 6. None of the above 7. I don’t know

  6. Factorial function n*(n-1)! For n > 1 n! = 1 For n ≤ 1 Java method for computing factorial function: public static long factorial (int n) { if (n<=1) return 1; else return n* factorial (n-1); } public static long factorial (int n) { long result; if (n<=1) { result = 1; } else { long recursiveCall = factorial (n-1); result = n*recursiveCall; } return result; }

  7. public static long factorial (int n) { 1.call long result; if (n<=1) { result = 1; } else { long recursiveCall = factorial (n-1); result = n*recursiveCall; } return result; } 1st call: n = 3

  8. public static long factorial (int n) { long result; 1.call if (n<=1) { result = 1; } else { long recursiveCall = factorial (n-1); result = n*recursiveCall; } return result; } 1st call: n = 3 result =

  9. public static long factorial (int n) { long result; if (n<=1) { result = 1; } else { 1.call long recursiveCall = factorial (n-1); result = n*recursiveCall; } return result; } 1st call: n = 3 result = recursiveCall =

  10. 2.call public static long factorial (int n) { long result; if (n<=1) { result = 1; } else { 1.call long recursiveCall = factorial (n-1); result = n*recursiveCall; } return result; } 1st call: n = 3 result = recursiveCall = 2nd call: n = 2

  11. public static long factorial (int n) { long result; 2.call if (n<=1) { result = 1; } else { 1.call long recursiveCall = factorial (n-1); result = n*recursiveCall; } return result; } 1st call: n = 3 result = recursiveCall = 2nd call: n = 2 result =

  12. public static long factorial (int n) { long result; if (n<=1) { result = 1; } else { 2.call 1.call long recursiveCall = factorial (n-1); result = n*recursiveCall; } return result; } 1st call: n = 3 result = recursiveCall = 2nd call: n = 2 result = recursiveCall =

  13. public static long factorial (int n) { 3.call long result; if (n<=1) { result = 1; } else { 2.call 1.call long recursiveCall = factorial (n-1); result = n*recursiveCall; } return result; } 1st call: n = 3 result = recursiveCall = 2nd call: n = 2 result = recursiveCall = 3rd call: n = 1

  14. public static long factorial (int n) { long result; 3.call if (n<=1) { result = 1; } else { 2.call 1.call long recursiveCall = factorial (n-1); result = n*recursiveCall; } return result; } 1st call: n = 3 result = recursiveCall = 2nd call: n = 2 result = recursiveCall = 3rd call: n = 1 result =

  15. public static long factorial (int n) { long result; if (n<=1) { result = 1; 3.call } else { 2.call 1.call long recursiveCall = factorial (n-1); result = n*recursiveCall; } return result; } 1st call: n = 3 result = recursiveCall = 2nd call: n = 2 result = recursiveCall = 3rd call: n = 1 result = 1

  16. public static long factorial (int n) { long result; if (n<=1) { result = 1; } else { 2.call 1.call long recursiveCall = factorial (n-1); result = n*recursiveCall; } return result; 3.call } 1st call: n = 3 result = recursiveCall = 2nd call: n = 2 result = recursiveCall = 1 3rd call: (finished) n = 1 result = 1

  17. public static long factorial (int n) { long result; if (n<=1) { result = 1; } else { 1.call long recursiveCall = factorial (n-1); result = n*recursiveCall; 2.call } return result; 3.call } 1st call: n = 3 result = recursiveCall = 2nd call: n = 2 result = 2 recursiveCall = 1 3rd call: (finished) n = 1 result = 1

  18. public static long factorial (int n) { long result; if (n<=1) { result = 1; } else { 1.call long recursiveCall = factorial (n-1); result = n*recursiveCall; } return result; 2.call 3.call } 1st call: n = 3 result = recursiveCall = 2 2nd call: (finished) n = 2 result = 2 recursiveCall = 1 3rd call: (finished) n = 1 result = 1

  19. public static long factorial (int n) { long result; if (n<=1) { result = 1; } else { long recursiveCall = factorial (n-1); result = n*recursiveCall; 1.call } return result; 2.call 3.call } 1st call: n = 3 result = 6 recursiveCall = 2 2nd call: (finished) n = 2 result = 2 recursiveCall = 1 3rd call: (finished) n = 1 result = 1

  20. public static long factorial (int n) { long result; if (n<=1) { result = 1; } else { long recursiveCall = factorial (n-1); result = n*recursiveCall; } return result; 2.call 3.call 1.call } 1st call: (finished) n = 3 result = 6 recursiveCall = 2 2nd call: (finished) n = 2 result = 2 recursiveCall = 1 3rd call: (finished) n = 1 result = 1

  21. public static void foo(int x) { QUIZ System.out.println(x); if (x>1) foo(x-1); Recursive method call } What is the result of the method call foo(4) ? 1. Prints 4 2. Prints 1 3. Prints 1 2 3 4 4. Prints 4 3 2 1 5. Prints something else 6. Compiler error: method foo is not allowed to call itself 7. Runtime error: method foo is not allowed to call itself 8. I don’t know

  22. Recursive Function: Fibonacci Numbers • The sequence of Fibonacci numbers is • First 10 terms are – 1, 1, 2, 3, 5, 8, 13, 21, 34, 55

  23. Recursive Function: Fibonacci Numbers • Recursive method corresponding to recursive definition of Fibonacci numbers public long fib(int n) { if (n <= 1) return 1; else return fib(n - 1) + fib(n - 2); }

  24. Structure of method calls public long fib(int n) { 1st call: if (n<=1) return 1; n = 4 else return fib(n-1) + fib(n-2); }

  25. Structure of method calls public long fib(int n) { 1st call: if (n<=1) return 1; n = 4 else return fib(n-1) + fib(n-2); } 2nd call: n = 3

  26. Structure of method calls public long fib(int n) { 1st call: if (n<=1) return 1; n = 4 else return fib(n-1) + fib(n-2); } 2nd call: n = 3 3rd call: n = 2

  27. Structure of method calls public long fib(int n) { 1st call: if (n<=1) return 1; n = 4 else return fib(n-1) + fib(n-2); } 2nd call: n = 3 3rd call: n = 2 4th call: n = 1

  28. Structure of method calls public long fib(int n) { 1st call: if (n<=1) return 1; n = 4 else return fib(n-1) + fib(n-2); } 2nd call: n = 3 3rd call: n = 2 4th call: n = 1 returns 1

  29. Structure of method calls public long fib(int n) { 1st call: if (n<=1) return 1; n = 4 else return fib(n-1) + fib(n-2); } 2nd call: n = 3 3rd call: n = 2 4th call: 5th call: n = 1 n = 0 returns 1

  30. Structure of method calls public long fib(int n) { 1st call: if (n<=1) return 1; n = 4 else return fib(n-1) + fib(n-2); } 2nd call: n = 3 3rd call: n = 2 4th call: 5th call: n = 1 n = 0 returns 1 returns 1

  31. Structure of method calls public long fib(int n) { 1st call: if (n<=1) return 1; n = 4 else return fib(n-1) + fib(n-2); } 2nd call: n = 3 3rd call: n = 2 returns 2 4th call: 5th call: n = 1 n = 0 returns 1 returns 1

  32. Structure of method calls public long fib(int n) { 1st call: if (n<=1) return 1; n = 4 else return fib(n-1) + fib(n-2); } 2nd call: n = 3 3rd call: 6th call: n = 2 n = 1 returns 2 4th call: 5th call: n = 1 n = 0 returns 1 returns 1

  33. Structure of method calls public long fib(int n) { 1st call: if (n<=1) return 1; n = 4 else return fib(n-1) + fib(n-2); } 2nd call: n = 3 3rd call: 6th call: n = 2 n = 1 returns 2 returns 1 4th call: 5th call: n = 1 n = 0 returns 1 returns 1

  34. Structure of method calls public long fib(int n) { 1st call: if (n<=1) return 1; n = 4 else return fib(n-1) + fib(n-2); } 2nd call: n = 3 returns 3 3rd call: 6th call: n = 2 n = 1 returns 2 returns 1 4th call: 5th call: n = 1 n = 0 returns 1 returns 1

  35. Structure of method calls public long fib(int n) { 1st call: if (n<=1) return 1; n = 4 else return fib(n-1) + fib(n-2); } 2nd call: 7th call: n = 3 n = 2 returns 3 3rd call: 6th call: n = 2 n = 1 returns 2 returns 1 4th call: 5th call: n = 1 n = 0 returns 1 returns 1

  36. Structure of method calls public long fib(int n) { 1st call: if (n<=1) return 1; n = 4 else return fib(n-1) + fib(n-2); } 2nd call: 7th call: n = 3 n = 2 returns 3 8th call: 3rd call: 6th call: n = 1 n = 2 n = 1 returns 2 returns 1 4th call: 5th call: n = 1 n = 0 returns 1 returns 1

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend