12
COMP-202: Foundations of Programming Lecture 23: Recursion Jackie Cheung, Winter 2016

COMP-202: Foundations of Programmingcs202/2016-01/web/sec2/lecture... · 2016. 4. 1. · prev = curr; curr = twobefore ... Write a version of printArray(int[] arr)that prints each

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: COMP-202: Foundations of Programmingcs202/2016-01/web/sec2/lecture... · 2016. 4. 1. · prev = curr; curr = twobefore ... Write a version of printArray(int[] arr)that prints each

COMP-202: Foundations of Programming

Lecture 23: Recursion

Jackie Cheung, Winter 2016

Page 2: COMP-202: Foundations of Programmingcs202/2016-01/web/sec2/lecture... · 2016. 4. 1. · prev = curr; curr = twobefore ... Write a version of printArray(int[] arr)that prints each

Warm-Up Questions

What is recursion? How do you tell if there is recursion in a piece of code?

What are the two key components of a recursive method?

2

Page 3: COMP-202: Foundations of Programmingcs202/2016-01/web/sec2/lecture... · 2016. 4. 1. · prev = curr; curr = twobefore ... Write a version of printArray(int[] arr)that prints each

Recursion and Iteration

Recursion and iteration (loops) are equally expressive!

Anything recursion can do, iteration can do.

Anything iteration can do, recursion can do.

3

Page 4: COMP-202: Foundations of Programmingcs202/2016-01/web/sec2/lecture... · 2016. 4. 1. · prev = curr; curr = twobefore ... Write a version of printArray(int[] arr)that prints each

Iterative Version of factorial

public static int factorial_it(int n) {

int prod = 1;

for (int i = 1; i <= n; i++) {

prod *= i;

}

return prod;

}

4

Page 5: COMP-202: Foundations of Programmingcs202/2016-01/web/sec2/lecture... · 2016. 4. 1. · prev = curr; curr = twobefore ... Write a version of printArray(int[] arr)that prints each

Iterative Version of fibonaccipublic static int fibonacci_it(int n) {

int curr = 1;

int prev = 0;

int twobefore = 0;

for (int i = 1; i < n; i++) {

twobefore = prev;

prev = curr;

curr = twobefore + prev;

}

return curr;

}

5

Page 6: COMP-202: Foundations of Programmingcs202/2016-01/web/sec2/lecture... · 2016. 4. 1. · prev = curr; curr = twobefore ... Write a version of printArray(int[] arr)that prints each

Exercise

Write a version of printArray(int[] arr) that prints each element of an array, separated by a comma. Use recursion.

• Actually, we're going to write printArray(int[] arr, int start)

that prints everything in the array starting at position start.

• What is the base case?

• What is the recursive case?

6

Page 7: COMP-202: Foundations of Programmingcs202/2016-01/web/sec2/lecture... · 2016. 4. 1. · prev = curr; curr = twobefore ... Write a version of printArray(int[] arr)that prints each

Recursion vs. Iteration

If they're equally powerful, why use one or the other?

• Use whichever is easier to think in terms of, for a certain problem.

• For simple cases, iteration is usually easier and faster.

• For complex cases, recursion is often more elegant and simpler to code.

7

Page 8: COMP-202: Foundations of Programmingcs202/2016-01/web/sec2/lecture... · 2016. 4. 1. · prev = curr; curr = twobefore ... Write a version of printArray(int[] arr)that prints each

Recursive Data Structures

Data structures can also be recursively defined!

Let’s consider arrays, and an alternative to storing a flat list of items that is recursive.

8

Page 9: COMP-202: Foundations of Programmingcs202/2016-01/web/sec2/lecture... · 2016. 4. 1. · prev = curr; curr = twobefore ... Write a version of printArray(int[] arr)that prints each

Alternate Definition of a List

We saw arrays in this class, which are contiguous elements in memory.

As an alternative, we could define a list of a recursive data structure.

9

Not testable on final exam

int[]arr[0] arr[1] arr[2] arr[3] arr[4] arr[5]

3 4 2 6 5 -1

Page 10: COMP-202: Foundations of Programmingcs202/2016-01/web/sec2/lecture... · 2016. 4. 1. · prev = curr; curr = twobefore ... Write a version of printArray(int[] arr)that prints each

Linked List

LinkedList class (for ints):private int val;

private LinkedList next;

View:

10

el 0

val: 3

next:

el 1

val: 4

next:

el 2

val: 2

next:

el 3

val: 6

next:

el 4

val: 5

next:

el 5

val: -1

next:

Page 11: COMP-202: Foundations of Programmingcs202/2016-01/web/sec2/lecture... · 2016. 4. 1. · prev = curr; curr = twobefore ... Write a version of printArray(int[] arr)that prints each

Linked List Operations

You could then define many of the list operations using recursion.

• Searching through a list

• Printing all elements in a list

Other advantages:• Adding and removing elements is easier than

with an array.

On the other hand, some operations are harder, like getting the ith element.

11

Page 12: COMP-202: Foundations of Programmingcs202/2016-01/web/sec2/lecture... · 2016. 4. 1. · prev = curr; curr = twobefore ... Write a version of printArray(int[] arr)that prints each

Example

Let’s implement a LinkedListInt class together!• Creating a LinkedListInt

• Adding an element (at the end)

• Setting the element at a certain position

• Removing an element from a position

• Printing the elements of the LinkedListInt

• Searching for the index of an element in the LinkedListInt

12