Introduction to Recursion

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

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.

Example 1 The Factorial Function

- factorial(n)

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 nfactorial(n-1)

The Factorial Sample Execution Trace

- factorial (6)
- (6factorial(5))
- (6(5factorial(4)))
- (6(5(4factorial(3))))
- (6(5(4(3factorial(2))))

) - (6(5(4(3(2factorial(1)

))))) - (6(5(4(3(21)))))
- (6(5(4(32))))
- (6(5(46)))
- (6(524))
- (6120)
- 720

Example 2 Reversing Strings

- public void reverseString(String str, int i)

- if(i lt str.length())
- reverseString(str, i1)
- System.out.print(str.charAt(i))

Example 3 The Fibonacci Function

- fibonacci(n)

1, if n lt 2

fibonacci(n-1) fibonacci(n-2), if n gt 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 lt 2) return 1L else return fibonacci(n-1) fibonacci(n-2)

Fibonacci Call Tree

public static long fibonacci(int n) if (n lt

2) return 1L else return

fibonacci(n-1) fibonacci(n-2)

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.

Drill Questions

- Write a recursive method public int sumUpTo(int

n) which adds up all integers from 1 to n. - Write a recursive method public int multiply(x,y)

that multiplies two integers x and y using

repeated additions and without using

multiplication. - 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).