CS 61A Discussion 3 Recursion Albert Xu Attendance: - - PowerPoint PPT Presentation

cs 61a discussion 3
SMART_READER_LITE
LIVE PREVIEW

CS 61A Discussion 3 Recursion Albert Xu Attendance: - - PowerPoint PPT Presentation

CS 61A Discussion 3 Recursion Albert Xu Attendance: links.cs61a.org/albert-disc Slides: albertxu.xyz/teaching/cs61a/ Announcements Recursion: an example Recursion: an example Here are the two key parts of recursion: 1) Base case 2) Recursive


slide-1
SLIDE 1

CS 61A Discussion 3

Recursion

Attendance: links.cs61a.org/albert-disc Slides: albertxu.xyz/teaching/cs61a/

Albert Xu

slide-2
SLIDE 2

Announcements

slide-3
SLIDE 3

Recursion: an example

slide-4
SLIDE 4

Recursion: an example

Here are the two key parts of recursion: 1) Base case 2) Recursive call Look for them in this example.

slide-5
SLIDE 5

Recursion: an example

Base case

slide-6
SLIDE 6

Recursion: an example

Base case Recursive call (and multiply it by n)

slide-7
SLIDE 7

Recursion: an example

Base case Recursive call (and multiply it by n)

This function works because n! = n * (n - 1)!, which is our recursive call.

slide-8
SLIDE 8

Recursion: dominoes

1 2 3 4 5 6 . . .

slide-9
SLIDE 9

Recursion: dominoes

1 2 3 4 5 6 . . .

slide-10
SLIDE 10

Recursion: dominoes

1 2 3 4 5 6 . . . Let’s calculate factorial(4)!

slide-11
SLIDE 11

Recursion: dominoes

1 2 3 4 5 6 . . . factorial(4) = 4 * factorial(3)

slide-12
SLIDE 12

Recursion: dominoes

1 2 3 4 5 6 . . . factorial(3) = 3 * factorial(2)

slide-13
SLIDE 13

Recursion: dominoes

1 2 3 4 5 6 . . . factorial(2) = 2 * factorial(1)

slide-14
SLIDE 14

Recursion: dominoes

1 2 3 4 5 6 . . . factorial(1) = 1 * factorial(0)

slide-15
SLIDE 15

Recursion: dominoes

1 2 3 4 5 6 . . . factorial(1) = 1 * 1

slide-16
SLIDE 16

Recursion: dominoes

1 2 3 4 5 6 . . . factorial(1) = 1 * 1

When you’re writing code, assume the recursive call works!

slide-17
SLIDE 17

Approaching Recursion

hard functions are hard to write.

slide-18
SLIDE 18

Approaching Recursion

hard functions are hard to write. the first step is often the most difficult

slide-19
SLIDE 19

Approaching Recursion

hard functions are hard to write. the first step is often the most difficult

Write triangular. This computes the nth triangular number, which is 0 + 1 + 2 + … + n

slide-20
SLIDE 20

Approaching Recursion

hard functions are hard to write. the first step is often the most difficult

Write triangular. This computes the nth triangular number, which is 0 + 1 + 2 + … + n

Steps: 1) Recursive call(s) - what is a convenient subproblem that our

  • verall problem shrinks to?
slide-21
SLIDE 21

Approaching Recursion

hard functions are hard to write. the first step is often the most difficult

Write triangular. This computes the nth triangular number, which is 0 + 1 + 2 + … + n

Steps: 1) Recursive call(s) - what is a convenient subproblem that our

  • verall problem shrinks to?
slide-22
SLIDE 22

Approaching Recursion

hard functions are hard to write. the first step is often the most difficult

Write triangular. This computes the nth triangular number, which is 0 + 1 + 2 + … + n

Steps: 1) Recursive call(s) - what is a convenient subproblem that our

  • verall problem shrinks to?

2) Build on the recursive call - what do I have to add to the result of the recursive call to get the answer to our overall problem

slide-23
SLIDE 23

Approaching Recursion

hard functions are hard to write. the first step is often the most difficult

Write triangular. This computes the nth triangular number, which is 0 + 1 + 2 + … + n

Steps: 1) Recursive call(s) - what is a convenient subproblem that our

  • verall problem shrinks to?

2) Build on the recursive call - what do I have to add to the result of the recursive call to get the answer to our overall problem

slide-24
SLIDE 24

Approaching Recursion

hard functions are hard to write. the first step is often the most difficult

Write triangular. This computes the nth triangular number, which is 0 + 1 + 2 + … + n

Steps: 1) Recursive call(s) - what is a convenient subproblem that our

  • verall problem shrinks to?

2) Build on the recursive call - what do I have to add to the result of the recursive call to get the answer to our overall problem 3) Base case - when do I stop… i.e. what is the smallest case

slide-25
SLIDE 25

Approaching Recursion

hard functions are hard to write. the first step is often the most difficult

Write triangular. This computes the nth triangular number, which is 0 + 1 + 2 + … + n

Steps: 1) Recursive call(s) - what is a convenient subproblem that our

  • verall problem shrinks to?

2) Build on the recursive call - what do I have to add to the result of the recursive call to get the answer to our overall problem 3) Base case - when do I stop… i.e. what is the smallest case

slide-26
SLIDE 26

Thanks for coming.

Have a great rest of your week! :)

Attendance: links.cs61a.org/albert-disc Slides: albertxu.xyz/teaching/cs61a/