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: Expression evaluation – Randomization and recursion: Random plants/trees • General aspects – Termination – Recursion versus iteration: simplicity vs efficiency
Recursion in trees and flowers 3 smaller trees/branches = Tree trunk
Natural Recursion: Derivative of Rational Function � � ����� � ��� �→�
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
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; }
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
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 =
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 =
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
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 =
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 =
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
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 =
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
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
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
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
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
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
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
Recursive Function: Fibonacci Numbers • The sequence of Fibonacci numbers is • First 10 terms are – 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
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); }
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); }
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
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
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
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
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
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
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
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
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
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
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
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
Recommend
More recommend