Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... ·...
Transcript of Lecture 7: Tree Recursioninst.eecs.berkeley.edu/~cs61a/su16/assets/slides/07-Tree_Recursion... ·...
![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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/1.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/2.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/3.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/4.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/5.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/6.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/7.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/8.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/9.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/10.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/11.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/12.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/13.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/14.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/15.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/16.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/17.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/18.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/19.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/20.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/21.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/22.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/23.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/24.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/25.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/26.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/27.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/28.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/29.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/30.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/31.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/32.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/33.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/34.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/35.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/36.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/37.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/38.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/39.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/40.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/41.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/42.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/43.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/44.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/45.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/46.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/47.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/48.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/49.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/50.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/51.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/52.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/53.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/54.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/55.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/56.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/57.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/58.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/59.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/60.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/61.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/62.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/63.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/64.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/65.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/66.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/67.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/68.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/69.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/70.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/71.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/72.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/73.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/74.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/75.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/76.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/77.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/78.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/79.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/80.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/81.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/82.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/83.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/84.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/85.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/86.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/87.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/88.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/89.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/90.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/91.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/92.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/93.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/94.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/95.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/96.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/97.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/98.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/99.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/100.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/101.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/102.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/103.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/104.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/105.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/106.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/107.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/108.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/109.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/110.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/111.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/112.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/113.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/114.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/115.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/116.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/117.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/118.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/119.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/120.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/121.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/122.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/123.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/124.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/125.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/126.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/127.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/128.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/129.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/130.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/131.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/132.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/133.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/134.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/135.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/136.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/137.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/138.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/139.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/140.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/141.jpg)
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](https://reader034.fdocuments.net/reader034/viewer/2022050203/5f56fec9045a4e655841d422/html5/thumbnails/142.jpg)
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