Question: Problem 1 : A method that makes multiple recursive calls 8 points; individual - only A number of the algorithms that we consider in CS

Problem 1: A method that makes multiple recursive calls
8 points; individual-only
A number of the algorithms that we consider in CS 112 involve recursive methods in which a given invocation of the method may make two or more recursive calls. To understand how this type of algorithm works, it helps to draw a call tree for a given set of initial parameters.
In this problem, you will draw such a call tree to illustrate the execution of a simple recursive method that operates on integers. You may find it helpful to review our solutions to the similar problem in Lab 5 problem before continuing.
Here is the method you will trace:
```
public static int foo(int x, int y){
if (y =||| x =0|| Math.abs(x-y)>=4){
return x+y;
} else {
int result1= foo(x +1, y -2);
int result2= foo(x -2, y +1);
return result1+ result2;
}
}
```
Assume that we begin with the following initial call to this method:
foo(3,3\()\)
1. Construct a call tree that shows each invocation of the method, and that uses lines to connect a given invocation to the recursive calls that it makes (if any). Follow the same format that we use in our solutions for Lab 5.
In section 1-1 of ps5_partI, we have given you an initial diagram.
You should take the following steps to complete it:
Add each subsequent call with its parameters to the call tree in the appropriate place.
- If a given call is a base case, simply put its return value under the call, as we do in our Lab 6, Task 2 solution for calls to fib(1) and fib(0).
- If a given call is not a base case, use lines composed of / or \characters to connect the call to the recursive call(s) that it makes.
- Precede each call with a number that indicates the overall order in which the calls were made.
2. Underneath your call tree, list the order in which the calls return, as well as their return values. When you refer to a given call in this part of the problem, use its number from the call tree.
For example, the initial call always returns last, so the last line in this part of the problem should look like this:
call 1(foo(3,3) returns ...
where you replace the ... with its return value.
See our solution for Lab 5 for another example of what this part of your solution should look like.
Problem 1 : A method that makes multiple

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!