Lab 2 - Recursion
Lab 2 - Recursion
Assume cup is an object for a cup of coffee with the instance methods isEmpty() and takeOneSip(). You can break the
problem into two subproblems: one is to drink one sip of coffee, and the other is to drink the rest of the coffee in the
cup. The second problem is the same as the original problem, but smaller in size.
The base case for the problem is when the cup is empty.
What should we keep in mind?
All recursive methods have the following characteristics:
• The method is implemented using an if−else or a switch statement that leads to different cases.
• One or more base cases (the simplest case) are used to stop recursion.
• Every recursive call reduces the original problem, bringing it increasingly closer to a base case until it becomes that
case.
In general, to solve a problem using recursion, you break it into subproblems. Each subprob lem is the same as the
original problem, but smaller in size. You can apply the same approach to each subproblem to solve it recursively.
1. Write a recursive mathematical definition for computing 2^n for a positive integer n.
2. Now it’s your turn. Write a recursive mathematical definition for computing x^n for a
positive integer n and a real number x.
2. SOLUTION - Now it’s your turn. Write a recursive mathematical definition for
computing x^n for a positive integer n and a real number x.
3. Recursive Palindrome. Let’s find out if a word is palindrome or not.
Helper Methods!
Sometimes you can find a solution to the original problem by defining a recursive function to a problem similar to the
original problem. This new method is called a recursive helper method. The original problem can be solved by
invoking the recursive helper method.
The recursive isPalindrome method, as you already know, is not as efficient as we would like, because it creates a new
string for every recursive call. To avoid creating new strings, you can use two parameters, the low and high indices, to
indicate the range of the substring. These two indices must be passed to the recursive method.
Since the original method is isPalindrome(String s), we can create the new method isPalindrome(String s, int low, int
high) to accept additional information on the string.
4. Let’s see RecursivePalindromeHelper
Fibonacci Series
The Fibonacci series was named for Leonardo Fibonacci, a medieval mathematician, who originated it to model the
growth of the rabbit population. It can be applied in numeric optimization and in various other areas.
How do you find fib(index) for a given index? It is easy to find fib(2) because you know fib(0) and fib(1). Assuming
you know fib(index − 2) and fib(index − 1), you can obtain fib(index) immediately.
Thus, the problem of computing fib(index) is reduced to computing fib(index − 2) and fib(index − 1). When doing so,
you apply the idea recursively until index is reduced to 0 or 1.
5. Naive Fibonacci Series. What is the Big O notation of the following code?
Why is it called Naïve Fibonacci? The term “naive” is used to describe a simple and straightforward approach to
solving a problem. In the context of the Fibonacci sequence, the naive approach is to use recursion to calculate the nth
term of the sequence.
This method is simple to understand and implement, but it is not the most efficient way to calculate the Fibonacci
sequence. Can you find the Big O Notation?
Naive Fibonacci Series.