Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Introduction to Recursion  Introduction to Recursion  Example 1: Factorial  Example 2: Reversing Strings  Example 3: Fibonacci  Infinite Recursion.

Similar presentations


Presentation on theme: "1 Introduction to Recursion  Introduction to Recursion  Example 1: Factorial  Example 2: Reversing Strings  Example 3: Fibonacci  Infinite Recursion."— Presentation transcript:

1 1 Introduction to Recursion  Introduction to Recursion  Example 1: Factorial  Example 2: Reversing Strings  Example 3: Fibonacci  Infinite Recursion  Review Exercises

2 2 Introduction to Recursion  We saw earlier that a method can call another method leading to the creation of activation records on the runtime stack.  Recursion is one of the powerful techniques of solving problems.  A recursive method is a method that calls itself directly or indirectly  A well-defined recursive method has:  A base case that determines the stopping condition in the method  A recursive step which must always get “closer” to the base case from one invocation to another.  The code of a recursive method must be structured to handle both the base case and the recursive case.  Each call to the method sets up a new execution environment, with new parameters and local variables.

3 3 Example 1: The Factorial Function factorial(n) = n*factorial(n-1), if n > 0 1, if n = 0 public class Factorial{ public static void main(String[] args){ long answer = factorial(5); System.out.println(answer); } public long factorial(int n) { if(n == 0) return 1L; else return n*factorial(n-1); }

4 4 The Factorial: Sample Execution Trace factorial (6) = = (6*factorial(5)) = (6*(5*factorial(4))) = (6*(5*(4*factorial(3)))) = (6*(5*(4*(3*factorial(2))))) = (6*(5*(4*(3*(2*factorial(1)))))) = (6*(5*(4*(3*(2*1))))) = (6*(5*(4*(3*2)))) = (6*(5*(4*6))) = (6*(5*24)) = (6*120) = 720

5 5 Example 2: Reversing Strings public void reverseString(String str, int i) { if(i < str.length()){ reverseString(str, i+1); System.out.print(str.charAt(i)); }

6 6 Example 3: The Fibonacci Function fibonacci(n) = fibonacci(n-1) + fibonacci(n-2), if n >= 2 1, if n < 2 public class Fibonacci{ public static void main(String[] args){ long answer = fibonacci(4); System.out.println(answer); } public static long fibonacci(int n) { if (n < 2) return 1L; else return fibonacci(n-1) + fibonacci(n-2); }

7 7 Fibonacci Call Tree public static long fibonacci(int n) { if (n < 2) return 1L; else return fibonacci(n-1) + fibonacci(n-2); }

8 8 Infinite Recursion  A recursive method must always call itself with a smaller argument  Infinite recursion results when:  The base case is omitted.  Recursive calls are not getting closer to the base case.  In theory, infinite recursive methods will execute “forever”  In practice, the system reports a stack overflow error.

9 9 Drill Questions 1.Write a recursive method public int sumUpTo(int n) which adds up all integers from 1 to n. 2.Write a recursive method public int multiply(x,y) that multiplies two integers x and y using repeated additions and without using multiplication. 3.Write a recursive method public int decimalToBinary(int n) that takes and integer parameter and prints its binary equivalent. 4. Write a recursive method public boolean isPalindrome(String str) that returns true if str is a palindrome and returns false otherwise. Note: a palindrome is a string that has the same characters when read from left to right or from right to left (Examples: eye, radar, madam, dad, mom, 202).


Download ppt "1 Introduction to Recursion  Introduction to Recursion  Example 1: Factorial  Example 2: Reversing Strings  Example 3: Fibonacci  Infinite Recursion."

Similar presentations


Ads by Google