0% found this document useful (0 votes)
8 views5 pages

Recursion Code Tracing Guide

Uploaded by

gincreate12
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Recursion Code Tracing Guide

Uploaded by

gincreate12
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Recursion Tracing:

For each problem, trace the code segment and identify the output of the code.

Problem 1

public static int mystery(int x) {


if (x == 2) {
return 2;
}
else {
return x * mystery(x - 2);
}
}

What is the base case?

When x == 2

What is the recursive call?

Recursive call: x*mystery(x-2)

Trace the code segment in the space below to determine the output of the call mystery(10).

Returned value Int x


80 10
48 8
24 6
8 4
2 2
Problem 2

public static int division(int y) {


if (y <= 0) {
return 1;
}
else {
return y / division(y - 3);
}
}

What is the base case?

When y <=0 (y is smaller than or equal to zero)

What is the recursive call?

Retun y/division(y-3)

Trace the code segment in the space below to determine the output of the call division(14).

Returned value Int y


1 14
1 11
1 8
2 5
-2 2
1 -1
Practice tracing recursive methods.

Problem 3

public static int sumNumbers(int[] numbers, int numbersLength) {


if (numbersLength <= 0) {
return 0;
}

return (sumNumbers(numbers, numbersLength - 1) +


numbers[numbersLength - 1]);
}

Call from main()

int[] numbers = {21, 2, 4, 82, 81, 33, 67, 52, 22, 23};
int sum = sumNumbers(numbers, [Link]);
[Link]("Sum: " + sum);

What is the base case?

When int numberslength <= 0 (numberslength is smaller than or equal to 0)

What is the recursive case?


Trace the code segment in the space below to determine the output of RecursionRunner.

Problem 4

public static int getListLength(ArrayList<Integer> list, int start) {


if (start == [Link]()) {
return 0;
}

return 1 + getListLength(list, start + 1);


}

Call from main:

ArrayList<Integer> numbers = new ArrayList<Integer>();


[Link](25);
[Link](32);
[Link](41);
[Link](28);
[Link](21);
[Link](45);
[Link](36);
[Link](19);
[Link](27);
[Link](15);
[Link](38);

int length = getListLength(numbers, 10);


[Link]("Numbers size: " + length);

What is the base case?

What is the recursive case?

Trace the code segment in the space below to determine the output of RecursionRunner.

Common questions

Powered by AI

The 'division(y)' function returns 1 if 'y <= 0' (base case). The recursive call is 'y / division(y - 3)'. For 'division(14)', the trace is: 14 / division(11) -> 11 / division(8) -> 8 / division(5) -> 5 / division(2) -> 2 / division(-1). When y reaches -1, it returns 1, resulting in divisions that compound to a result of 0 .

Recursion and iteration approach problems differently in computation. 'getListLength(list, start)' uses recursion, naturally aligning with problems divisible into similar subproblems. Iterative methods loop through conditions until a particular criterion is met, often requiring more explicit handling of intermediate states. While recursion minimizes code complexity and aligns with problem structure, iteration can be more efficient in terms of memory and execution time due to linear operations without the overhead of function calls .

In recursion, each function call is placed onto the call stack. Recursive calls create a chain where the stack frames contain each call's parameters. When 'mystery(x)' calls itself, the stack stores the intermediate values until it unwinds starting from the base case. 'sumNumbers(numbers, numbersLength)' similarly builds a stack of partial sums. Proper management is key to avoiding stack overflow and ensuring that recursive functions return correctly to each previous state .

Choosing recursion involves assessing problem suitability, such as when problems are inherently divisible into smaller similar problems, like 'mystery(x)' handling multiplications or 'sumNumbers' aggregating array elements. Considerations include ensuring correct base cases, managing computational complexity, checking feasibility of recursive depth, and potential overhead of stacking function calls. Recursion often aligns with clear, elegant solutions but must be balanced with performance concerns .

Base cases in recursion prevent infinite loops and ensure termination by providing a stopping condition. In 'mystery(x)', it ensures termination at 'x == 2'. In 'division(y)', it is crucial for stopping at 'y <= 0'. Without these, recursive calls would continue indefinitely, leading to stack overflow. Each base case defines a condition where calculation directly returns a value instead of further recursive calls .

The function 'mystery(x)' uses recursion by calling 'x * mystery(x - 2)'. The base case is when 'x == 2', returning 2. For 'mystery(10)', the trace is: 10 * mystery(8) -> 8 * mystery(6) -> 6 * mystery(4) -> 4 * mystery(2). When x reaches 2, it returns 2, resulting in 10 * 8 * 6 * 4 * 2 = 3840 as the final output .

Recursive functions such as 'division(y)' must handle cases where recursive depth becomes excessive, leading to stack overflow. They may also have inefficient performance for large inputs due to repeated calculations, as seen when dividing large numbers deeply. Mitigation strategies include ensuring well-defined base cases, minimizing redundant calls, and optimizing through techniques like memoization .

The function 'getListLength(list, start)' calculates the length of a list starting at a given index recursively. The base case returns 0 when 'start == list.size()'. The recursive call is '1 + getListLength(list, start + 1)'. For 'list' with 11 elements and 'start' at 10, the output is 1 because it only counts from the tenth index to the end .

The function 'sumNumbers(numbers, numbersLength)' recursively sums the elements of an integer array. The base case is when 'numbersLength <= 0', which returns 0. It recursively calls itself with 'numbersLength - 1' and adds 'numbers[numbersLength - 1]'. Given 'numbers = {21, 2, 4, 82, 81, 33, 67, 52, 22, 23}', it sums all elements to yield a result of 387 .

'getListLength(list, start)' exemplifies the divide and conquer principle, deconstructing the problem into the base case and recursive case. It shows typical recursive decomposition, counting elements one by one from the start index, combining results at return phases. This highlights patterns such as recursive problem breakdown, repeated structure handling, and result aggregation, fundamental in recursive problem-solving .

You might also like