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
  • Read the following code, threaded recursive calculation of Fibonacci number of n: #include <stdio.h> #include <stdlib.h>...

    Read the following code, threaded recursive calculation of Fibonacci number of n: #include <stdio.h> #include <stdlib.h> #include <pthread.h> void *fib(void *arg); int main(int argc, char **argv){     int n = atoi(argv[1]);     printf("%d\n", (int)fib(n)); } void *fib(void *arg){     int n;     pthread_t thread1;     pthread_t thread2;     void *a;     void *b;     int c;     n = (int)arg;     if (n <= 0) return 0;     if (n == 1) return 1;     pthread_create(&thread1, NULL, fib, n-1);    ...

  • Recursion Tree Goal: Predict the output of a recursive method call using a recursion tree. Draw...

    Recursion Tree Goal: Predict the output of a recursive method call using a recursion tree. Draw the recursion tree of the following source code, showing all method calls and outputs: public class Main { public static void main(String[] args) { f(3, 4); } public static void f(int x, int y) { if(x + y > 1) { f(x - 2, y - 1); System.out.print(x + " "); f(y, x - 2); System.out.print(2 * x + y + " "); }...

  • 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...

  • CMPS 290 Programming Assignment Arrays and Recursion – Fibonacci Numbers In this programming assignment you will...

    CMPS 290 Programming Assignment Arrays and Recursion – Fibonacci Numbers In this programming assignment you will be working with arrays and multiple function calls, including a recursive function call. The goal of the assignment will be to, using a pre-set array of sequence numbers, calculate the Fibonacci sequence number for each value in the array (see the example at the bottom if this isn’t clear). Instructions and Requirements: • Create a program that assembles and runs to find the correct...

  • 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...

  • The following is a correct implementation of a non-recursive method to find the nth Fibonacci number...

    The following is a correct implementation of a non-recursive method to find the nth Fibonacci number in Java: public static long fib(int n){    long[ ] fibNums = new long[n + 1];    fibNums[0] = 0;    finNums[1] = 1;    for(int i = 2; i <= n; i++){       fibNums[i] = fibNums [i - 1] + finBums[i - 2]; } return fibNums[n]; } I am having trouble understanding the code. Why the creation of the long array make this...

  • 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 +...

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