Question

Let’s work together to develop a call tree for the execution of the following recursive method....

Let’s work together to develop a call tree for the execution of the following recursive method. (The method allows us to recursively generate the nth integer in the Fibonacci sequence, although you don’t need to be familiar with that sequence to understand this problem.)

public static int fib(int n) {
    if (n == 0 || n == 1) {
        return 1;
    } else {
        int prev1 = fib(n - 2);
        int prev2 = fib(n - 1);
        return prev1 + prev2;
    }
}

Assume that we begin with the following initial call to this method:

fib(4)
  1. Let’s draw a call tree for the sequence of calls that are involved in computing the result of fib(4). As we do so, we’ll number each call of the method in the order in which they are made.

  2. The order in which the calls are made is not the same as the order in which the calls return. A given invocation of the method can’t return until both of the calls that it makes (fib(n - 2) and fib(n - 1)) return.

    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 (fib(4)) returns ...
    

    where you replace the ... with its return value.

  3. Re-write the fibonacci recursive method to not use multiple return statements. Does it work the same way?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the solution below.

call Tree for fibcu) (1) fib(4) y return value of children 6) fible) + føb(3)63 fibco) + fibca) call Number fiblo) + fib( 1)Trying to write a Program without multiple returns :- public static int fibcn). if (n=1) return filo CN-2) + fibcn-1); ) retu

- - - - - - - - - - - - If there are any doubts, comment down here. We are right here to help you. Happy Learning..!! Please

Add a comment
Know the answer?
Add Answer to:
Let’s work together to develop a call tree for the execution of the following recursive method....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Complete java program below. Complete non-recursive version nthFibonacciWithLoop() method. Complete recursive version nthFibonacciWithRecursion() method. public class...

    Complete java program below. Complete non-recursive version nthFibonacciWithLoop() method. Complete recursive version nthFibonacciWithRecursion() method. public class Fibonacci { // Fib(N): N N = 0 or N = 1 // Fib(N-1) + Fib(N-2) N > 1 // For example, // Fib(0) = 0 // Fib(1) = 1 // Fib(2) = Fib(1) + Fib(0) = 1 + 0 = 1 // Fib(3) = Fib(2) + Fib(1) = Fib(2) + 1 = (Fib(1) + Fib(0)) + 1 = 1 + 0 + 1...

  • om me insert e Search - Assignment - Word Design Layout References Mailings Review Lucida Sans...

    om me insert e Search - Assignment - Word Design Layout References Mailings Review Lucida Sans Typ 11 - A A A A - BIU XX A.D.A. View ру mat Painter rd CIT 111 Help! 21 - - 19. ABCD AaBbcDc AaB 1 Normal TNo Spac... Head Paragraph PROGRAMS SPRING 2020 8.3 points Write a method that determines if a positive integer is an abundant number. A number is abundanti the sum of its proper divisors (a divisor that isn't...

  • I just need to add comment for the code blow. Pleas add comment for each method...

    I just need to add comment for the code blow. Pleas add comment for each method and class if comments left out. package exercise01; /*************************************************************************** * <p> This program demonstrates the technique of recursion and includes * recursive methods that are defined for a variety of mathematical * functions. *    * <br>A recursive method is one that directly or indirectly calls itself * and must include: * <br>(1) end case: stopping condition * which terminates/ends recursion * <br>(2) reduction:...

  • For the following recursive implementation of a method to compute the Fibonacci S integer n, circle...

    For the following recursive implementation of a method to compute the Fibonacci S integer n, circle the line number(s) the them: s) that comprise the three parts of a recursive algorithm and label 1. public static long fibonacci(int n) ( 2 if( 1) 3. return 1; 4. else if (n 2) S. return; 6. else 7. (long fibNminus1 fibonacci(n - 1); 8. long fibNminus2- fibonacci(n -2); 9. long fibN fibNminusl + fibNminus2; 10. return fibN; 12.)

  • Extra Credit - Fibonacci Function (Lec. 5 topic: Recursive function and runtime stack. Use recursion to...

    Extra Credit - Fibonacci Function (Lec. 5 topic: Recursive function and runtime stack. Use recursion to calculate the Fibonacci Function 1.) Use a recursive function called fib() to calculate the Fibonacci Function for the following values for the variable n. int n = 10; int n = 20; int n = 30; int n = 40; int n = 45; int n = 46; 2.) In addition to calculating and displaying the results, use a "timer" to see how long...

  • PROBLEM: Write a recursive method named Division that takes two integers X and Y returns the...

    PROBLEM: Write a recursive method named Division that takes two integers X and Y returns the result of integer division (i.e., 8 / 3 = 2). Please comment on every line of code. EXISITNG CODE: int Exponentiation(int X, int Y) { if (Y == 0) // base case return 1; else // recursive case return X * Exponentiation(X, Y - 1); //make recursive call } int Multiply(int X, int Y){ if(Y == 0){ return 0; } else{ return X +...

  • Given below is a C code to compute Fibonacci numbers recursively. Suppose that the activation rec...

    Given below is a C code to compute Fibonacci numbers recursively. Suppose that the activation record for f includes the following elements in order: (return value, argument n, local s, local t); there will normally be other elements in the activation record as well. The questions below assume that the initial call is f(5). 5B. 3M int f(int n) int t, S if (n <2) return 1 s-f(n-1) t = f(n-2); return s+t Show the complete activation tree. How does...

  • Question 32 (Programming) The Fibonacci sequence of number is defined as: In this question we examine...

    Question 32 (Programming) The Fibonacci sequence of number is defined as: In this question we examine a property of this sequence. a) Write a C function definition with header int fib(int [ ] a, int n) which generates an array, a of the first n Fibonacci numbers. (Hint: You do not have to write this recursively. You just have to generate each array entry from the previous two entries.) b) Two numbers are said to be coprime if they have...

  • c++ problem Write a recursive method int mul(int a, int b) that computes the product of...

    c++ problem Write a recursive method int mul(int a, int b) that computes the product of two nonnegative integers a and b. You are not allowed to use multiplication () Hint: use addition (+) and recursion. Write a method evenDigits that accepts an integer parameter n and that returns the integer formed by removing the odd digits from n. The following table shows several calls and their expected return values: 1- 2- Call Valued Returned evenDigits (8342116); 8426 evenDigits(4109); 4...

  • Recursive Tracing. For each call to the following method, indicate what value is returned: public static...

    Recursive Tracing. For each call to the following method, indicate what value is returned: public static int mystery(int n) { if (n < 0) { return -mystery(-n); } else if (n == 0) { return 0; } else { return mystery(n / 10) * 10 + 9 - (n % 10); } Call Value Returned mystery(0) mystery(5) mystery(13) mystery(297) mystery(-3456) } Can any one help me with it?

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT