1 of 14

Lecture 23 - CS159 Recursive Tracing

Prof. Alvin Chao

Due this week:

  • Fri 11pm Coding Bat Recursion
  • Sun 11pm HW 10

2 of 14

OpenDSA Tracing

3 of 14

Handout

4 of 14

Given some recursive code:

- What is the return result?�- What is the maximum depth of the call stack?�- How many times was the recursive method called?

https://pollev.com/chaoaj

5 of 14

Recursive Tracing

Characteristics of tracing mechanism

Visible representation of each call�Tracking of arguments for each call�Tracking of return value for each call

Use a circle for each dynamic call with arguments

Small computation inside if desired

Arrow for new call with solid line

Arrow for return with value and dotted line

6 of 14

Recursion Tracing

1. powerN

public static void main(String[] args) {

System.out.println("powerN(2, 3): " + powerN(2, 3));

}

public static int powerN(int base, int n) {

if (n == 0) {

return 1;

}

return base * powerN(base, n - 1);

}

- What is the return result? 8

- What is the maximum depth of the call stack of recursive calls? 4

- How many times was the recursive method called? 4

Main

2,3�2*?

2, 2�2*?

2,1�2*?

2,0

1

2

4

powerN

8

7 of 14

Example 1

public static void main(String[] args) {

System.out.println("bunnyEars(3): " + bunnyEars(3));

}

public static int bunnyEars(int bunnies) {

if (bunnies == 0) return 0;

return 2 + bunnyEars(bunnies - 1);

}

main

bunnyEars

0

2 + ?

2 + ?

2 + ?

1

2

3

6

4

0

2

  • What is the return result?

6

  • What is the maximum depth of the call stack of recursive calls?

4

  • How many times was the recursive method called?

4

8 of 14

Example 2

public static void main(String[] args) {

System.out.println("fibonacci(4): " + fibonacci(4));

}

public static int fibonacci(int n) {

if (n == 0)return 0;

if (n == 1)return 1;

return fibonacci(n - 1) + fibonacci(n - 2);

}

main

fibonacci

1

?

?

?

2

3

4

3

2

1

1

  • What is the return result?

3

  • What is the maximum depth of the call stack of recursive calls?

4

  • How many times was the recursive method called?

9

0

0

1

1

1

?

2

1

0

0

1

+ ?

+ ?

+ ?

+ ?

9 of 14

Example 3

public static void main(String[] args) {

System.out.println(array6(nums, 0));

}

public static boolean array6(int[] nums, int index) {

if (nums.length == 0) return false;

if (index == nums.length - 1) return nums[index] == 6;� return (nums[index] == 6) || array6(nums, index + 1);

}

main

array6

nums, 3

false || ?

false || ?

false || ?

nums, 2

nums, 1

nums, 0

true

true

true

true

  • What is the return result?

true

  • What is the maximum depth of the call stack of recursive calls?

4

  • How many times was the recursive method called?

4

nums

3 | 1 | 7 |6

10 of 14

Visualizer/Tracing Recursion

11 of 14

add_all

Python problem for tracing in vercel:

def add_all(nums):

"""Add all the integers in the given list, recursively.

The list may contain ints and/or other lists containing ints/lists.

Args:

nums (list): A list of ints and/or other lists containing ints/lists.

Returns:

(int): The sum of all the integers in the given list.

"""

Java:

/**

* Add all the integers in the given array recursively.

*/

public static int add_all(int[] nums){

}

12 of 14

triangle

13 of 14

sum_digits

14 of 14

count8