lecture 7: tree recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-tree_recursion... ·...

142
Brian Hou June 29, 2016 Lecture 7: Tree Recursion

Upload: others

Post on 18-Jul-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Brian Hou June 29, 2016

Lecture 7: Tree Recursion

Page 2: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Announcements

Page 3: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Announcements

• Project 1 is due tomorrow, +1 EC point if submitted today

Page 4: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Announcements

• Project 1 is due tomorrow, +1 EC point if submitted today• Run ok --submit to check against hidden tests

Page 5: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Announcements

• Project 1 is due tomorrow, +1 EC point if submitted today• Run ok --submit to check against hidden tests• Check your submission at ok.cs61a.org

Page 6: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Announcements

• Project 1 is due tomorrow, +1 EC point if submitted today• Run ok --submit to check against hidden tests• Check your submission at ok.cs61a.org• Invite your partner (watch this video)

Page 7: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Announcements

• Project 1 is due tomorrow, +1 EC point if submitted today• Run ok --submit to check against hidden tests• Check your submission at ok.cs61a.org• Invite your partner (watch this video)

• Homework 2 is due today, Homework 1 solutions uploaded

Page 8: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Announcements

• Project 1 is due tomorrow, +1 EC point if submitted today• Run ok --submit to check against hidden tests• Check your submission at ok.cs61a.org• Invite your partner (watch this video)

• Homework 2 is due today, Homework 1 solutions uploaded• Quiz 2 is tomorrow at the beginning of lecture

Page 9: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Announcements

• Project 1 is due tomorrow, +1 EC point if submitted today• Run ok --submit to check against hidden tests• Check your submission at ok.cs61a.org• Invite your partner (watch this video)

• Homework 2 is due today, Homework 1 solutions uploaded• Quiz 2 is tomorrow at the beginning of lecture

• If you have an alternate time or are not enrolled in the class, please arrive at 11:45 am

Page 10: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Announcements

• Project 1 is due tomorrow, +1 EC point if submitted today• Run ok --submit to check against hidden tests• Check your submission at ok.cs61a.org• Invite your partner (watch this video)

• Homework 2 is due today, Homework 1 solutions uploaded• Quiz 2 is tomorrow at the beginning of lecture

• If you have an alternate time or are not enrolled in the class, please arrive at 11:45 am

• Week 2 checkoff must be done in lab today or tomorrow

Page 11: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Announcements

• Project 1 is due tomorrow, +1 EC point if submitted today• Run ok --submit to check against hidden tests• Check your submission at ok.cs61a.org• Invite your partner (watch this video)

• Homework 2 is due today, Homework 1 solutions uploaded• Quiz 2 is tomorrow at the beginning of lecture

• If you have an alternate time or are not enrolled in the class, please arrive at 11:45 am

• Week 2 checkoff must be done in lab today or tomorrow• Talk about hw01, lab02, lab03 with a lab assistant

Page 12: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Announcements

• Project 1 is due tomorrow, +1 EC point if submitted today• Run ok --submit to check against hidden tests• Check your submission at ok.cs61a.org• Invite your partner (watch this video)

• Homework 2 is due today, Homework 1 solutions uploaded• Quiz 2 is tomorrow at the beginning of lecture

• If you have an alternate time or are not enrolled in the class, please arrive at 11:45 am

• Week 2 checkoff must be done in lab today or tomorrow• Talk about hw01, lab02, lab03 with a lab assistant

• Alternate Exam Request: goo.gl/forms/FDQix4I5dNXPQDgw2

Page 13: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Hog Contest Rules

Page 14: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Hog Contest Rules

• Up to two people submit one entry; max one entry per person

Page 15: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Hog Contest Rules

• Up to two people submit one entry; max one entry per person

• Your score is the number of entries against which you win more than 50.00001% of the time

Page 16: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Hog Contest Rules

• Up to two people submit one entry; max one entry per person

• Your score is the number of entries against which you win more than 50.00001% of the time

• All strategies must be deterministic, pure functions of the current player and opponent scores

Page 17: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Hog Contest Rules

• Up to two people submit one entry; max one entry per person

• Your score is the number of entries against which you win more than 50.00001% of the time

• All strategies must be deterministic, pure functions of the current player and opponent scores

• Top 3 entries will receive EC

Page 18: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Hog Contest Rules

• Up to two people submit one entry; max one entry per person

• Your score is the number of entries against which you win more than 50.00001% of the time

• All strategies must be deterministic, pure functions of the current player and opponent scores

• Top 3 entries will receive EC• The real prize: honor and glory

Page 19: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Hog Contest Rules

• Up to two people submit one entry; max one entry per person

• Your score is the number of entries against which you win more than 50.00001% of the time

• All strategies must be deterministic, pure functions of the current player and opponent scores

• Top 3 entries will receive EC• The real prize: honor and glory

• Also: bragging rights

Page 20: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Hog Contest Rules

• Up to two people submit one entry; max one entry per person

• Your score is the number of entries against which you win more than 50.00001% of the time

• All strategies must be deterministic, pure functions of the current player and opponent scores

• Top 3 entries will receive EC• The real prize: honor and glory

• Also: bragging rights

Ready? cs61a.org/proj/hog_contest

Page 21: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Hog Contest Rules

• Up to two people submit one entry; max one entry per person

• Your score is the number of entries against which you win more than 50.00001% of the time

• All strategies must be deterministic, pure functions of the current player and opponent scores

• Top 3 entries will receive EC• The real prize: honor and glory

• Also: bragging rights

Ready? cs61a.org/proj/hog_contest

Page 22: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Roadmap

• This week (Functions), the goals are: • To understand the idea of

functional abstraction • To study this idea through:

• higher-order functions • recursion • orders of growth

Introduction

Functions

Data

Mutability

Objects

Interpretation

Paradigms

Applications

Page 23: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Recursion

Page 24: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Cascade Function

Page 25: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Cascade Function (demo)

Page 26: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Cascade Function

Output

123 12 1 12

(demo)

Page 27: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Cascade Function

Output

123 12 1 12

• Each cascade frame is from a different call to cascade.

(demo)

Page 28: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Cascade Function

Output

123 12 1 12

• Each cascade frame is from a different call to cascade.

• Until the Return value appears, that call has not completed.

(demo)

Page 29: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Cascade Function

Output

123 12 1 12

• Each cascade frame is from a different call to cascade.

• Until the Return value appears, that call has not completed.

• Any statement can appear before or after the recursive call.

(demo)

Page 30: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Cascade Function

Output

123 12 1 12

• Each cascade frame is from a different call to cascade.

• Until the Return value appears, that call has not completed.

• Any statement can appear before or after the recursive call.

(demo)

Page 31: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Cascade Function

Output

123 12 1 12

• Each cascade frame is from a different call to cascade.

• Until the Return value appears, that call has not completed.

• Any statement can appear before or after the recursive call.

(demo)

Page 32: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Cascade Function

Output

123 12 1 12

• Each cascade frame is from a different call to cascade.

• Until the Return value appears, that call has not completed.

• Any statement can appear before or after the recursive call.

(demo)

Page 33: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Cascade Function

Output

123 12 1 12

• Each cascade frame is from a different call to cascade.

• Until the Return value appears, that call has not completed.

• Any statement can appear before or after the recursive call.

(demo)

Page 34: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Two Definitions of Cascade

Page 35: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Two Definitions of Cascade (demo)

Page 36: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Two Definitions of Cascade

def cascade(n):

if n < 10:

print(n)

else:

print(n)

cascade(n // 10)

print(n)

def cascade(n):

print(n)

if n >= 10:

cascade(n // 10)

print(n)

(demo)

Page 37: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Two Definitions of Cascade

• If two implementations are equally clear, then shorter is usually better

def cascade(n):

if n < 10:

print(n)

else:

print(n)

cascade(n // 10)

print(n)

def cascade(n):

print(n)

if n >= 10:

cascade(n // 10)

print(n)

(demo)

Page 38: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Two Definitions of Cascade

• If two implementations are equally clear, then shorter is usually better

• In this case, the longer implementation is more clear (to me)

def cascade(n):

if n < 10:

print(n)

else:

print(n)

cascade(n // 10)

print(n)

def cascade(n):

print(n)

if n >= 10:

cascade(n // 10)

print(n)

(demo)

Page 39: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Two Definitions of Cascade

• If two implementations are equally clear, then shorter is usually better

• In this case, the longer implementation is more clear (to me)

• When learning to write recursive functions, put base cases first

def cascade(n):

if n < 10:

print(n)

else:

print(n)

cascade(n // 10)

print(n)

def cascade(n):

print(n)

if n >= 10:

cascade(n // 10)

print(n)

(demo)

Page 40: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Inverse Cascade

Page 41: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Inverse Cascade

Output

1 12 123 1234 123 12 1

def inverse_cascade(n):

grow(n)

print(n)

shrink(n)

Page 42: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Inverse Cascade

Output

1 12 123 1234 123 12 1

def inverse_cascade(n):

grow(n)

print(n)

shrink(n)

def f_then_g(f, g, n):

if n:

f(n)

g(n)

Page 43: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Inverse Cascade

Output

1 12 123 1234 123 12 1

def inverse_cascade(n):

grow(n)

print(n)

shrink(n)

def f_then_g(f, g, n):

if n:

f(n)

g(n)

grow = lambda n: f_then_g(grow, print, n // 10)

shrink = lambda n: f_then_g(print, shrink, n // 10)

Page 44: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Inverse Cascade

Output

1 12 123 1234 123 12 1

def inverse_cascade(n):

grow(n)

print(n)

shrink(n)

def f_then_g(f, g, n):

if n:

f(n)

g(n)

grow = lambda n: f_then_g(grow, print, n // 10)

shrink = lambda n: f_then_g(print, shrink, n // 10)

Page 45: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Fibonacci

Page 46: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Fibonacci Sequence

Page 47: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Fibonacci Sequence

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

Page 48: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Fibonacci Sequence

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n):

Page 49: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Fibonacci Sequence

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n):

... , 35

Page 50: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Fibonacci Sequence

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 9,227,465

... , 35

Page 51: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Fibonacci Sequence

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 9,227,465

... , 35

Page 52: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Fibonacci Sequence

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 9,227,465

... , 35

Page 53: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Fibonacci Sequence

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 9,227,465

... , 35

Page 54: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Fibonacci Sequence

def fib(n):

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 9,227,465

... , 35

Page 55: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Fibonacci Sequence

def fib(n):

pred, curr = 0, 1

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 9,227,465

... , 35

Page 56: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Fibonacci Sequence

def fib(n):

pred, curr = 0, 1

k = 1

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 9,227,465

... , 35

Page 57: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Fibonacci Sequence

def fib(n):

pred, curr = 0, 1

k = 1

while k < n:

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 9,227,465

... , 35

Page 58: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Fibonacci Sequence

def fib(n):

pred, curr = 0, 1

k = 1

while k < n:

pred, curr = curr, pred + curr

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 9,227,465

... , 35

Page 59: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Fibonacci Sequence

def fib(n):

pred, curr = 0, 1

k = 1

while k < n:

pred, curr = curr, pred + curr

The next Fibonacci number is the sum of the two

previous Fibonacci numbers

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 9,227,465

... , 35

Page 60: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Fibonacci Sequence

def fib(n):

pred, curr = 0, 1

k = 1

while k < n:

pred, curr = curr, pred + curr

k += 1 The next Fibonacci number is the sum of the two

previous Fibonacci numbers

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 9,227,465

... , 35

Page 61: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Fibonacci Sequence

def fib(n):

pred, curr = 0, 1

k = 1

while k < n:

pred, curr = curr, pred + curr

k += 1

return currThe next Fibonacci number

is the sum of the two previous Fibonacci numbers

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 9,227,465

... , 35

Page 62: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Fibonacci Sequence

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 9,227,465

... , 35

Page 63: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Fibonacci Sequence

def fib(n):

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 9,227,465

... , 35

Page 64: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Fibonacci Sequence

def fib(n):

if n == 0:

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 9,227,465

... , 35

Page 65: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Fibonacci Sequence

def fib(n):

if n == 0:

return 0

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 9,227,465

... , 35

Page 66: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Fibonacci Sequence

def fib(n):

if n == 0:

return 0

pred, curr = 0, 1

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 9,227,465

... , 35

Page 67: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Fibonacci Sequence

def fib(n):

if n == 0:

return 0

pred, curr = 0, 1

k = 1

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 9,227,465

... , 35

Page 68: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Fibonacci Sequence

def fib(n):

if n == 0:

return 0

pred, curr = 0, 1

k = 1

The next Fibonacci number is the sum of the two

previous Fibonacci numbers

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 9,227,465

... , 35

Page 69: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Fibonacci Sequence

def fib(n):

if n == 0:

return 0

pred, curr = 0, 1

k = 1

while k < n:

The next Fibonacci number is the sum of the two

previous Fibonacci numbers

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 9,227,465

... , 35

Page 70: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Fibonacci Sequence

def fib(n):

if n == 0:

return 0

pred, curr = 0, 1

k = 1

while k < n:

pred, curr = curr, pred + curr

The next Fibonacci number is the sum of the two

previous Fibonacci numbers

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 9,227,465

... , 35

Page 71: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Fibonacci Sequence

def fib(n):

if n == 0:

return 0

pred, curr = 0, 1

k = 1

while k < n:

pred, curr = curr, pred + curr

k += 1 The next Fibonacci number is the sum of the two

previous Fibonacci numbers

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 9,227,465

... , 35

Page 72: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Fibonacci Sequence

def fib(n):

if n == 0:

return 0

pred, curr = 0, 1

k = 1

while k < n:

pred, curr = curr, pred + curr

k += 1

return currThe next Fibonacci number

is the sum of the two previous Fibonacci numbers

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 9,227,465

... , 35

Page 73: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Fibonacci Sequence

def fib(n):

if n == 0:

return 0

pred, curr = 0, 1

k = 1

while k < n:

pred, curr = curr, pred + curr

k += 1

return currThe next Fibonacci number

is the sum of the two previous Fibonacci numbers

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n): ... , 9,227,465

... , 35

This correction was made on July 3 at 10PM

Page 74: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Fibonacci Sequence

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n):

Page 75: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Fibonacci Sequence

def fib(n):

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n):

Page 76: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Fibonacci Sequence

def fib(n):

if n == 0:

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n):

Page 77: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Fibonacci Sequence

def fib(n):

if n == 0:

return 0

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n):

Page 78: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Fibonacci Sequence

def fib(n):

if n == 0:

return 0

elif n == 1:

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n):

Page 79: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Fibonacci Sequence

def fib(n):

if n == 0:

return 0

elif n == 1:

return 1

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n):

Page 80: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Fibonacci Sequence

def fib(n):

if n == 0:

return 0

elif n == 1:

return 1

else:

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n):

Page 81: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Fibonacci Sequence

def fib(n):

if n == 0:

return 0

elif n == 1:

return 1

else:

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n):

The next Fibonacci number is the sum of the two

previous Fibonacci numbers

Page 82: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Fibonacci Sequence

def fib(n):

if n == 0:

return 0

elif n == 1:

return 1

else:

return fib(n-2) + fib(n-1)

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n):

The next Fibonacci number is the sum of the two

previous Fibonacci numbers

Page 83: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

The Fibonacci Sequence

def fib(n):

if n == 0:

return 0

elif n == 1:

return 1

else:

return fib(n-2) + fib(n-1)

0, 1, 2, 3, 4, 5, 6, 7, 8,n:

0, 1, 1, 2, 3, 5, 8, 13, 21,fib(n):

The next Fibonacci number is the sum of the two

previous Fibonacci numbers

Page 84: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Tree Recursion

def fib(n):

if n == 0:

return 0

elif n == 1:

return 1

else:

return fib(n-2) + fib(n-1)

Tree-shaped processes arise whenever executing the body of a recursive function makes more than one recursive call

Page 85: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

A Tree-Recursive Process

Page 86: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

A Tree-Recursive Process

fib(5)

Page 87: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

A Tree-Recursive Process

fib(5)

fib(3)

Page 88: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

A Tree-Recursive Process

fib(5)

fib(4)fib(3)

Page 89: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

A Tree-Recursive Process

fib(5)

fib(4)fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 90: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

A Tree-Recursive Process

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 91: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

A Tree-Recursive Process

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 92: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

A Tree-Recursive Process

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 93: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

A Tree-Recursive Process

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 94: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

A Tree-Recursive Process

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 95: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

A Tree-Recursive Process

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 96: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

A Tree-Recursive Process

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 97: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

A Tree-Recursive Process

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 98: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

A Tree-Recursive Process

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 99: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

A Tree-Recursive Process

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 100: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

A Tree-Recursive Process

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 101: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

A Tree-Recursive Process

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 102: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

A Tree-Recursive Process

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 103: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

A Tree-Recursive Process

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 104: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

A Tree-Recursive Process

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 105: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

A Tree-Recursive Process

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 106: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

A Tree-Recursive Process

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 107: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

A Tree-Recursive Process

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

(demo)

Page 108: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

A Tree-Recursive Process

fib(5)

fib(4)

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

fib(2)

fib(0) fib(1)

0 1

fib(3)

fib(1)

1

fib(2)

fib(0) fib(1)

0 1

Page 109: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Break!

Page 110: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Counting Partitions

Page 111: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Counting Partitions

Page 112: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

Page 113: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

How many different ways can I give out6 pieces of chocolate if nobody canhave more than 4 pieces?

count_partitions(6, 4)

Page 114: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

How many different ways can I give out6 pieces of chocolate if nobody canhave more than 4 pieces?

count_partitions(6, 4)

2 + 4 = 61 + 1 + 4 = 6

3 + 3 = 61 + 2 + 3 = 61 + 1 + 1 + 3 = 6

1 + 1 + 2 + 2 = 62 + 2 + 2 = 6

1 + 1 + 1 + 1 + 2 = 6

1 + 1 + 1 + 1 + 1 + 1 = 6

Page 115: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

Page 116: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

2 + 4 = 61 + 1 + 4 = 6

3 + 3 = 61 + 2 + 3 = 61 + 1 + 1 + 3 = 6

1 + 1 + 2 + 2 = 62 + 2 + 2 = 6

1 + 1 + 1 + 1 + 2 = 6

1 + 1 + 1 + 1 + 1 + 1 = 6

Page 117: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

2 + 4 = 61 + 1 + 4 = 6

3 + 3 = 61 + 2 + 3 = 61 + 1 + 1 + 3 = 6

1 + 1 + 2 + 2 = 62 + 2 + 2 = 6

1 + 1 + 1 + 1 + 2 = 6

1 + 1 + 1 + 1 + 1 + 1 = 6

Page 118: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

2 + 4 = 61 + 1 + 4 = 6

3 + 3 = 61 + 2 + 3 = 61 + 1 + 1 + 3 = 6

1 + 1 + 2 + 2 = 62 + 2 + 2 = 6

1 + 1 + 1 + 1 + 2 = 6

1 + 1 + 1 + 1 + 1 + 1 = 6

Page 119: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

2 + 4 = 61 + 1 + 4 = 6

3 + 3 = 61 + 2 + 3 = 61 + 1 + 1 + 3 = 6

1 + 1 + 2 + 2 = 62 + 2 + 2 = 6

1 + 1 + 1 + 1 + 2 = 6

1 + 1 + 1 + 1 + 1 + 1 = 6

Page 120: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

Page 121: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

Page 122: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

• Recursive decomposition: finding simpler instances of the problem.

Page 123: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

• Recursive decomposition: finding simpler instances of the problem.• Explore two possibilities:

Page 124: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

• Recursive decomposition: finding simpler instances of the problem.• Explore two possibilities:

Page 125: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

• Recursive decomposition: finding simpler instances of the problem.• Explore two possibilities:• Use at least one 4

Page 126: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

• Recursive decomposition: finding simpler instances of the problem.• Explore two possibilities:• Use at least one 4• Don't use any 4

Page 127: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

• Recursive decomposition: finding simpler instances of the problem.• Explore two possibilities:• Use at least one 4• Don't use any 4• Solve two simpler problems:

Page 128: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

• Recursive decomposition: finding simpler instances of the problem.• Explore two possibilities:• Use at least one 4• Don't use any 4• Solve two simpler problems:• count_partitions(2, 4)

Page 129: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

• Recursive decomposition: finding simpler instances of the problem.• Explore two possibilities:• Use at least one 4• Don't use any 4• Solve two simpler problems:• count_partitions(2, 4)• count_partitions(6, 3)

Page 130: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

• Recursive decomposition: finding simpler instances of the problem.• Explore two possibilities:• Use at least one 4• Don't use any 4• Solve two simpler problems:• count_partitions(2, 4)• count_partitions(6, 3)• Tree recursion often involves exploring different choices.

Page 131: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

• Recursive decomposition: finding simpler instances of the problem. • Explore two possibilities: • Use at least one 4 • Don't use any 4 • Solve two simpler problems: • count_partitions(2, 4) • count_partitions(6, 3) • Tree recursion often involves exploring different choices.

Page 132: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

def count_partitions(n, m):

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

• Recursive decomposition: finding simpler instances of the problem. • Explore two possibilities: • Use at least one 4 • Don't use any 4 • Solve two simpler problems: • count_partitions(2, 4) • count_partitions(6, 3) • Tree recursion often involves exploring different choices.

Page 133: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

def count_partitions(n, m):

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

• Recursive decomposition: finding simpler instances of the problem. • Explore two possibilities: • Use at least one 4 • Don't use any 4 • Solve two simpler problems: • count_partitions(2, 4) • count_partitions(6, 3) • Tree recursion often involves exploring different choices.

with_m = count_partitions(n-m, m)

Page 134: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

def count_partitions(n, m):

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

• Recursive decomposition: finding simpler instances of the problem. • Explore two possibilities: • Use at least one 4 • Don't use any 4 • Solve two simpler problems: • count_partitions(2, 4) • count_partitions(6, 3) • Tree recursion often involves exploring different choices.

with_m = count_partitions(n-m, m)

without_m = count_partitions(n, m-1)

Page 135: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

def count_partitions(n, m):

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

• Recursive decomposition: finding simpler instances of the problem. • Explore two possibilities: • Use at least one 4 • Don't use any 4 • Solve two simpler problems: • count_partitions(2, 4) • count_partitions(6, 3) • Tree recursion often involves exploring different choices.

with_m = count_partitions(n-m, m)

without_m = count_partitions(n, m-1)

return with_m + without_m

Page 136: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

def count_partitions(n, m):

if n == 0:

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

• Recursive decomposition: finding simpler instances of the problem. • Explore two possibilities: • Use at least one 4 • Don't use any 4 • Solve two simpler problems: • count_partitions(2, 4) • count_partitions(6, 3) • Tree recursion often involves exploring different choices.

with_m = count_partitions(n-m, m)

without_m = count_partitions(n, m-1)

return with_m + without_m

Page 137: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

def count_partitions(n, m):

if n == 0:

return 1

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

• Recursive decomposition: finding simpler instances of the problem. • Explore two possibilities: • Use at least one 4 • Don't use any 4 • Solve two simpler problems: • count_partitions(2, 4) • count_partitions(6, 3) • Tree recursion often involves exploring different choices.

with_m = count_partitions(n-m, m)

without_m = count_partitions(n, m-1)

return with_m + without_m

Page 138: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

def count_partitions(n, m):

if n == 0:

return 1

elif n < 0:

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

• Recursive decomposition: finding simpler instances of the problem. • Explore two possibilities: • Use at least one 4 • Don't use any 4 • Solve two simpler problems: • count_partitions(2, 4) • count_partitions(6, 3) • Tree recursion often involves exploring different choices.

with_m = count_partitions(n-m, m)

without_m = count_partitions(n, m-1)

return with_m + without_m

Page 139: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

def count_partitions(n, m):

if n == 0:

return 1

elif n < 0:

return 0

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

• Recursive decomposition: finding simpler instances of the problem. • Explore two possibilities: • Use at least one 4 • Don't use any 4 • Solve two simpler problems: • count_partitions(2, 4) • count_partitions(6, 3) • Tree recursion often involves exploring different choices.

with_m = count_partitions(n-m, m)

without_m = count_partitions(n, m-1)

return with_m + without_m

Page 140: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

def count_partitions(n, m):

if n == 0:

return 1

elif n < 0:

return 0

elif m == 0:

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

• Recursive decomposition: finding simpler instances of the problem. • Explore two possibilities: • Use at least one 4 • Don't use any 4 • Solve two simpler problems: • count_partitions(2, 4) • count_partitions(6, 3) • Tree recursion often involves exploring different choices.

with_m = count_partitions(n-m, m)

without_m = count_partitions(n, m-1)

return with_m + without_m

Page 141: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

def count_partitions(n, m):

if n == 0:

return 1

elif n < 0:

return 0

elif m == 0:

return 0

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

• Recursive decomposition: finding simpler instances of the problem. • Explore two possibilities: • Use at least one 4 • Don't use any 4 • Solve two simpler problems: • count_partitions(2, 4) • count_partitions(6, 3) • Tree recursion often involves exploring different choices.

with_m = count_partitions(n-m, m)

without_m = count_partitions(n, m-1)

return with_m + without_m

Page 142: Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... · Announcements • Project 1 is due tomorrow, +1 EC point if submitted today • Run

def count_partitions(n, m):

if n == 0:

return 1

elif n < 0:

return 0

elif m == 0:

return 0

else:

Counting Partitions

The number of partitions of a positive integer n, using parts up to size m, is the number of ways in which n can be expressed as the sum of positive integer parts up to m in increasing order.

• Recursive decomposition: finding simpler instances of the problem. • Explore two possibilities: • Use at least one 4 • Don't use any 4 • Solve two simpler problems: • count_partitions(2, 4) • count_partitions(6, 3) • Tree recursion often involves exploring different choices.

with_m = count_partitions(n-m, m)

without_m = count_partitions(n, m-1)

return with_m + without_m