61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN...
Transcript of 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN...
![Page 1: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/1.jpg)
61A Lecture 21
Friday, March 13
![Page 2: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/2.jpg)
Announcements
2
![Page 3: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/3.jpg)
Announcements
• Project 3 is due Thursday 10/23 @ 11:59pm
2
![Page 4: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/4.jpg)
Announcements
• Project 3 is due Thursday 10/23 @ 11:59pm
§Please submit two ways: the normal way and using python3 ok --submit!
2
![Page 5: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/5.jpg)
Announcements
• Project 3 is due Thursday 10/23 @ 11:59pm
§Please submit two ways: the normal way and using python3 ok --submit!
§You can view your ok submission on the ok website: http://ok.cs61a.org
2
![Page 6: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/6.jpg)
Announcements
• Project 3 is due Thursday 10/23 @ 11:59pm
§Please submit two ways: the normal way and using python3 ok --submit!
§You can view your ok submission on the ok website: http://ok.cs61a.org
• Midterm 2 is on Thursday 3/19 7pm-9pm
2
![Page 7: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/7.jpg)
Announcements
• Project 3 is due Thursday 10/23 @ 11:59pm
§Please submit two ways: the normal way and using python3 ok --submit!
§You can view your ok submission on the ok website: http://ok.cs61a.org
• Midterm 2 is on Thursday 3/19 7pm-9pm§Review session on Tuesday 3/17 5pm-6:30pm in 2050 VLSB
2
![Page 8: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/8.jpg)
Announcements
• Project 3 is due Thursday 10/23 @ 11:59pm
§Please submit two ways: the normal way and using python3 ok --submit!
§You can view your ok submission on the ok website: http://ok.cs61a.org
• Midterm 2 is on Thursday 3/19 7pm-9pm§Review session on Tuesday 3/17 5pm-6:30pm in 2050 VLSB
§HKN review session on Sunday 3/15 12-3pm in 10 Evans
2
![Page 9: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/9.jpg)
Announcements
• Project 3 is due Thursday 10/23 @ 11:59pm
§Please submit two ways: the normal way and using python3 ok --submit!
§You can view your ok submission on the ok website: http://ok.cs61a.org
• Midterm 2 is on Thursday 3/19 7pm-9pm§Review session on Tuesday 3/17 5pm-6:30pm in 2050 VLSB
§HKN review session on Sunday 3/15 12-3pm in 10 Evans
§Conflict form submissions are due Friday 3/13!
2
![Page 10: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/10.jpg)
Announcements
• Project 3 is due Thursday 10/23 @ 11:59pm
§Please submit two ways: the normal way and using python3 ok --submit!
§You can view your ok submission on the ok website: http://ok.cs61a.org
• Midterm 2 is on Thursday 3/19 7pm-9pm§Review session on Tuesday 3/17 5pm-6:30pm in 2050 VLSB
§HKN review session on Sunday 3/15 12-3pm in 10 Evans
§Conflict form submissions are due Friday 3/13!
§1 2-sided sheet of hand-written notes created by you + 2 official study guides
2
![Page 11: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/11.jpg)
Announcements
• Project 3 is due Thursday 10/23 @ 11:59pm
§Please submit two ways: the normal way and using python3 ok --submit!
§You can view your ok submission on the ok website: http://ok.cs61a.org
• Midterm 2 is on Thursday 3/19 7pm-9pm§Review session on Tuesday 3/17 5pm-6:30pm in 2050 VLSB
§HKN review session on Sunday 3/15 12-3pm in 10 Evans
§Conflict form submissions are due Friday 3/13!
§1 2-sided sheet of hand-written notes created by you + 2 official study guides
§Same exam location as midterm 1. See http://cs61a.org/exams/midterm2.html
2
![Page 12: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/12.jpg)
Announcements
• Project 3 is due Thursday 10/23 @ 11:59pm
§Please submit two ways: the normal way and using python3 ok --submit!
§You can view your ok submission on the ok website: http://ok.cs61a.org
• Midterm 2 is on Thursday 3/19 7pm-9pm§Review session on Tuesday 3/17 5pm-6:30pm in 2050 VLSB
§HKN review session on Sunday 3/15 12-3pm in 10 Evans
§Conflict form submissions are due Friday 3/13!
§1 2-sided sheet of hand-written notes created by you + 2 official study guides
§Same exam location as midterm 1. See http://cs61a.org/exams/midterm2.html
§Today's lecture contains the last of the Midterm 2 material (Monday is just examples)
2
![Page 13: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/13.jpg)
Announcements
• Project 3 is due Thursday 10/23 @ 11:59pm
§Please submit two ways: the normal way and using python3 ok --submit!
§You can view your ok submission on the ok website: http://ok.cs61a.org
• Midterm 2 is on Thursday 3/19 7pm-9pm§Review session on Tuesday 3/17 5pm-6:30pm in 2050 VLSB
§HKN review session on Sunday 3/15 12-3pm in 10 Evans
§Conflict form submissions are due Friday 3/13!
§1 2-sided sheet of hand-written notes created by you + 2 official study guides
§Same exam location as midterm 1. See http://cs61a.org/exams/midterm2.html
§Today's lecture contains the last of the Midterm 2 material (Monday is just examples)
• No lecture next Wednesday 3/18
2
![Page 14: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/14.jpg)
Announcements
• Project 3 is due Thursday 10/23 @ 11:59pm
§Please submit two ways: the normal way and using python3 ok --submit!
§You can view your ok submission on the ok website: http://ok.cs61a.org
• Midterm 2 is on Thursday 3/19 7pm-9pm§Review session on Tuesday 3/17 5pm-6:30pm in 2050 VLSB
§HKN review session on Sunday 3/15 12-3pm in 10 Evans
§Conflict form submissions are due Friday 3/13!
§1 2-sided sheet of hand-written notes created by you + 2 official study guides
§Same exam location as midterm 1. See http://cs61a.org/exams/midterm2.html
§Today's lecture contains the last of the Midterm 2 material (Monday is just examples)
• No lecture next Wednesday 3/18
• No discussion sections next Thursday 3/19 or Friday 3/20
2
![Page 15: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/15.jpg)
Announcements
• Project 3 is due Thursday 10/23 @ 11:59pm
§Please submit two ways: the normal way and using python3 ok --submit!
§You can view your ok submission on the ok website: http://ok.cs61a.org
• Midterm 2 is on Thursday 3/19 7pm-9pm§Review session on Tuesday 3/17 5pm-6:30pm in 2050 VLSB
§HKN review session on Sunday 3/15 12-3pm in 10 Evans
§Conflict form submissions are due Friday 3/13!
§1 2-sided sheet of hand-written notes created by you + 2 official study guides
§Same exam location as midterm 1. See http://cs61a.org/exams/midterm2.html
§Today's lecture contains the last of the Midterm 2 material (Monday is just examples)
• No lecture next Wednesday 3/18
• No discussion sections next Thursday 3/19 or Friday 3/20
• Lecture next Friday 3/20 is a video (but a great one)
2
![Page 16: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/16.jpg)
Sets
![Page 17: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/17.jpg)
Sets
4
![Page 18: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/18.jpg)
Sets
One more built-in Python container type
4
![Page 19: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/19.jpg)
Sets
One more built-in Python container type
• Set literals are enclosed in braces
4
![Page 20: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/20.jpg)
Sets
One more built-in Python container type
• Set literals are enclosed in braces
• Duplicate elements are removed on construction
4
![Page 21: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/21.jpg)
Sets
One more built-in Python container type
• Set literals are enclosed in braces
• Duplicate elements are removed on construction
• Sets are unordered, just like dictionary entries
4
![Page 22: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/22.jpg)
Sets
One more built-in Python container type
• Set literals are enclosed in braces
• Duplicate elements are removed on construction
• Sets are unordered, just like dictionary entries
>>> s = {3, 2, 1, 4, 4}
4
![Page 23: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/23.jpg)
Sets
One more built-in Python container type
• Set literals are enclosed in braces
• Duplicate elements are removed on construction
• Sets are unordered, just like dictionary entries
>>> s = {3, 2, 1, 4, 4}>>> s{1, 2, 3, 4}
4
![Page 24: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/24.jpg)
Sets
One more built-in Python container type
• Set literals are enclosed in braces
• Duplicate elements are removed on construction
• Sets are unordered, just like dictionary entries
>>> s = {3, 2, 1, 4, 4}>>> s{1, 2, 3, 4}>>> 3 in sTrue
4
![Page 25: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/25.jpg)
Sets
One more built-in Python container type
• Set literals are enclosed in braces
• Duplicate elements are removed on construction
• Sets are unordered, just like dictionary entries
>>> s = {3, 2, 1, 4, 4}>>> s{1, 2, 3, 4}>>> 3 in sTrue>>> len(s)4
4
![Page 26: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/26.jpg)
Sets
One more built-in Python container type
• Set literals are enclosed in braces
• Duplicate elements are removed on construction
• Sets are unordered, just like dictionary entries
>>> s = {3, 2, 1, 4, 4}>>> s{1, 2, 3, 4}>>> 3 in sTrue>>> len(s)4>>> s.union({1, 5}){1, 2, 3, 4, 5}
4
![Page 27: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/27.jpg)
Sets
One more built-in Python container type
• Set literals are enclosed in braces
• Duplicate elements are removed on construction
• Sets are unordered, just like dictionary entries
>>> s = {3, 2, 1, 4, 4}>>> s{1, 2, 3, 4}>>> 3 in sTrue>>> len(s)4>>> s.union({1, 5}){1, 2, 3, 4, 5}>>> s.intersection({6, 5, 4, 3}){3, 4}
4
![Page 28: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/28.jpg)
Implementing Sets
5
![Page 29: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/29.jpg)
Implementing Sets
What we should be able to do with a set:
5
![Page 30: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/30.jpg)
Implementing Sets
What we should be able to do with a set:
• Membership testing: Is a value an element of a set?
5
![Page 31: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/31.jpg)
Implementing Sets
What we should be able to do with a set:
• Membership testing: Is a value an element of a set?
• Union: Return a set with all elements in set1 or set2
5
![Page 32: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/32.jpg)
Implementing Sets
What we should be able to do with a set:
• Membership testing: Is a value an element of a set?
• Union: Return a set with all elements in set1 or set2
Union
1
34
2
35
1
34
2
5
5
![Page 33: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/33.jpg)
Implementing Sets
What we should be able to do with a set:
• Membership testing: Is a value an element of a set?
• Union: Return a set with all elements in set1 or set2
• Intersection: Return a set with any elements in set1 and set2
Union
1
34
2
35
1
34
2
5
5
![Page 34: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/34.jpg)
Implementing Sets
What we should be able to do with a set:
• Membership testing: Is a value an element of a set?
• Union: Return a set with all elements in set1 or set2
• Intersection: Return a set with any elements in set1 and set2
Union
1
34
2
35
1
34
2
5
Intersection
1
34
2
35
3
5
![Page 35: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/35.jpg)
Implementing Sets
What we should be able to do with a set:
• Membership testing: Is a value an element of a set?
• Union: Return a set with all elements in set1 or set2
• Intersection: Return a set with any elements in set1 and set2
• Adjoin: Return a set with all elements in s and a value v
Union
1
34
2
35
1
34
2
5
Intersection
1
34
2
35
3
5
![Page 36: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/36.jpg)
Implementing Sets
What we should be able to do with a set:
• Membership testing: Is a value an element of a set?
• Union: Return a set with all elements in set1 or set2
• Intersection: Return a set with any elements in set1 and set2
• Adjoin: Return a set with all elements in s and a value v
Union
1
34
2
35
1
34
2
5
Intersection
1
34
2
35
3
Adjoin
1
34
2
1
34
2
5
![Page 37: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/37.jpg)
Sets as Unordered Sequences
![Page 38: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/38.jpg)
Sets as Unordered Sequences
Proposal 1: A set is represented by a linked list that contains no duplicate items.
7
![Page 39: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/39.jpg)
Sets as Unordered Sequences
Proposal 1: A set is represented by a linked list that contains no duplicate items.
7
def empty(s): return s is Link.empty
![Page 40: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/40.jpg)
Sets as Unordered Sequences
Proposal 1: A set is represented by a linked list that contains no duplicate items.
7
def empty(s): return s is Link.empty
def set_contains(s, v): """Return whether set s contains value v.
>>> s = Link(1, Link(2, Link(3))) >>> set_contains(s, 2) True """
![Page 41: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/41.jpg)
Sets as Unordered Sequences
Proposal 1: A set is represented by a linked list that contains no duplicate items.
7
def empty(s): return s is Link.empty
def set_contains(s, v): """Return whether set s contains value v.
>>> s = Link(1, Link(2, Link(3))) >>> set_contains(s, 2) True """ if empty(s): return False
![Page 42: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/42.jpg)
Sets as Unordered Sequences
Proposal 1: A set is represented by a linked list that contains no duplicate items.
7
def empty(s): return s is Link.empty
def set_contains(s, v): """Return whether set s contains value v.
>>> s = Link(1, Link(2, Link(3))) >>> set_contains(s, 2) True """ if empty(s): return False elif s.first == v: return True
![Page 43: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/43.jpg)
Sets as Unordered Sequences
Proposal 1: A set is represented by a linked list that contains no duplicate items.
7
def empty(s): return s is Link.empty
def set_contains(s, v): """Return whether set s contains value v.
>>> s = Link(1, Link(2, Link(3))) >>> set_contains(s, 2) True """ if empty(s): return False elif s.first == v: return True else: return set_contains(s.rest, v)
![Page 44: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/44.jpg)
Sets as Unordered Sequences
Proposal 1: A set is represented by a linked list that contains no duplicate items.
7
Time order of growth
def empty(s): return s is Link.empty
def set_contains(s, v): """Return whether set s contains value v.
>>> s = Link(1, Link(2, Link(3))) >>> set_contains(s, 2) True """ if empty(s): return False elif s.first == v: return True else: return set_contains(s.rest, v)
![Page 45: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/45.jpg)
Sets as Unordered Sequences
Proposal 1: A set is represented by a linked list that contains no duplicate items.
7
Time order of growth
⇥(1)def empty(s): return s is Link.empty
def set_contains(s, v): """Return whether set s contains value v.
>>> s = Link(1, Link(2, Link(3))) >>> set_contains(s, 2) True """ if empty(s): return False elif s.first == v: return True else: return set_contains(s.rest, v)
![Page 46: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/46.jpg)
Sets as Unordered Sequences
Proposal 1: A set is represented by a linked list that contains no duplicate items.
7
Time order of growth
⇥(1)
Time depends on whether & where v appears in s
def empty(s): return s is Link.empty
def set_contains(s, v): """Return whether set s contains value v.
>>> s = Link(1, Link(2, Link(3))) >>> set_contains(s, 2) True """ if empty(s): return False elif s.first == v: return True else: return set_contains(s.rest, v)
![Page 47: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/47.jpg)
Sets as Unordered Sequences
Proposal 1: A set is represented by a linked list that contains no duplicate items.
7
Time order of growth
⇥(1)
⇥(n)
Time depends on whether & where v appears in s
def empty(s): return s is Link.empty
def set_contains(s, v): """Return whether set s contains value v.
>>> s = Link(1, Link(2, Link(3))) >>> set_contains(s, 2) True """ if empty(s): return False elif s.first == v: return True else: return set_contains(s.rest, v)
![Page 48: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/48.jpg)
Sets as Unordered Sequences
Proposal 1: A set is represented by a linked list that contains no duplicate items.
7
Time order of growth
⇥(1)
⇥(n)
Time depends on whether & where v appears in s
Assuming v either does not appear in s
or appears in a uniformly
distributed random location
def empty(s): return s is Link.empty
def set_contains(s, v): """Return whether set s contains value v.
>>> s = Link(1, Link(2, Link(3))) >>> set_contains(s, 2) True """ if empty(s): return False elif s.first == v: return True else: return set_contains(s.rest, v)
![Page 49: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/49.jpg)
Sets as Unordered Sequences
Proposal 1: A set is represented by a linked list that contains no duplicate items.
(Demo)
7
Time order of growth
⇥(1)
⇥(n)
Time depends on whether & where v appears in s
Assuming v either does not appear in s
or appears in a uniformly
distributed random location
def empty(s): return s is Link.empty
def set_contains(s, v): """Return whether set s contains value v.
>>> s = Link(1, Link(2, Link(3))) >>> set_contains(s, 2) True """ if empty(s): return False elif s.first == v: return True else: return set_contains(s.rest, v)
![Page 50: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/50.jpg)
Sets as Unordered Sequences
8
![Page 51: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/51.jpg)
Sets as Unordered Sequences
8
def adjoin_set(s, v): if set_contains(s, v): return s else: return Link(v, s)
![Page 52: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/52.jpg)
Sets as Unordered SequencesTime order of growth
8
def adjoin_set(s, v): if set_contains(s, v): return s else: return Link(v, s)
![Page 53: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/53.jpg)
�(n)
Sets as Unordered SequencesTime order of growth
8
def adjoin_set(s, v): if set_contains(s, v): return s else: return Link(v, s)
![Page 54: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/54.jpg)
�(n)
Sets as Unordered SequencesTime order of growth
The size of the set
8
def adjoin_set(s, v): if set_contains(s, v): return s else: return Link(v, s)
![Page 55: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/55.jpg)
�(n)
Sets as Unordered SequencesTime order of growth
The size of the set
8
def adjoin_set(s, v): if set_contains(s, v): return s else: return Link(v, s)
def intersect_set(set1, set2): in_set2 = lambda v: set_contains(set2, v) return keep_if(set1, in_set2)
![Page 56: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/56.jpg)
�(n)
Sets as Unordered SequencesTime order of growth
The size of the set
8
Need a new version defined for Link instances
def adjoin_set(s, v): if set_contains(s, v): return s else: return Link(v, s)
def intersect_set(set1, set2): in_set2 = lambda v: set_contains(set2, v) return keep_if(set1, in_set2)
![Page 57: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/57.jpg)
�(n)
Sets as Unordered Sequences
�(n2)
Time order of growth
The size of the set
8
Need a new version defined for Link instances
def adjoin_set(s, v): if set_contains(s, v): return s else: return Link(v, s)
def intersect_set(set1, set2): in_set2 = lambda v: set_contains(set2, v) return keep_if(set1, in_set2)
![Page 58: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/58.jpg)
�(n)
Sets as Unordered Sequences
�(n2)
Time order of growth
The size of the set
If sets are the same size
8
Need a new version defined for Link instances
def adjoin_set(s, v): if set_contains(s, v): return s else: return Link(v, s)
def intersect_set(set1, set2): in_set2 = lambda v: set_contains(set2, v) return keep_if(set1, in_set2)
![Page 59: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/59.jpg)
�(n)
Sets as Unordered Sequences
�(n2)
Time order of growth
The size of the set
If sets are the same size
8
Need a new version defined for Link instances
def adjoin_set(s, v): if set_contains(s, v): return s else: return Link(v, s)
def intersect_set(set1, set2): in_set2 = lambda v: set_contains(set2, v) return keep_if(set1, in_set2)
def union_set(set1, set2): not_in_set2 = lambda v: not set_contains(set2, v) set1_not_set2 = keep_if(set1, not_in_set2) return extend(set1_not_set2, set2)
![Page 60: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/60.jpg)
�(n)
Sets as Unordered Sequences
�(n2)
Time order of growth
The size of the set
If sets are the same size
8
Need a new version defined for Link instances
Need a new version defined for Link instances
def adjoin_set(s, v): if set_contains(s, v): return s else: return Link(v, s)
def intersect_set(set1, set2): in_set2 = lambda v: set_contains(set2, v) return keep_if(set1, in_set2)
def union_set(set1, set2): not_in_set2 = lambda v: not set_contains(set2, v) set1_not_set2 = keep_if(set1, not_in_set2) return extend(set1_not_set2, set2)
![Page 61: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/61.jpg)
�(n)
�(n2)
Sets as Unordered Sequences
�(n2)
Time order of growth
The size of the set
If sets are the same size
8
Need a new version defined for Link instances
Need a new version defined for Link instances
def adjoin_set(s, v): if set_contains(s, v): return s else: return Link(v, s)
def intersect_set(set1, set2): in_set2 = lambda v: set_contains(set2, v) return keep_if(set1, in_set2)
def union_set(set1, set2): not_in_set2 = lambda v: not set_contains(set2, v) set1_not_set2 = keep_if(set1, not_in_set2) return extend(set1_not_set2, set2)
![Page 62: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/62.jpg)
�(n)
�(n2)
Sets as Unordered Sequences
�(n2)
Time order of growth
The size of the set
If sets are the same size
8
(Demo)
Need a new version defined for Link instances
Need a new version defined for Link instances
def adjoin_set(s, v): if set_contains(s, v): return s else: return Link(v, s)
def intersect_set(set1, set2): in_set2 = lambda v: set_contains(set2, v) return keep_if(set1, in_set2)
def union_set(set1, set2): not_in_set2 = lambda v: not set_contains(set2, v) set1_not_set2 = keep_if(set1, not_in_set2) return extend(set1_not_set2, set2)
![Page 63: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/63.jpg)
Sets as Ordered Sequences
![Page 64: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/64.jpg)
Sets as Ordered Sequences
Proposal 2: A set is represented by a linked list with unique elements that is ordered from least to greatest
10
![Page 65: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/65.jpg)
Sets as Ordered Sequences
Proposal 2: A set is represented by a linked list with unique elements that is ordered from least to greatest
10
Parts of the program that... Assume that sets are... Using...
![Page 66: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/66.jpg)
Sets as Ordered Sequences
Proposal 2: A set is represented by a linked list with unique elements that is ordered from least to greatest
10
Parts of the program that... Assume that sets are... Using...
Use sets to contain values
![Page 67: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/67.jpg)
Sets as Ordered Sequences
Proposal 2: A set is represented by a linked list with unique elements that is ordered from least to greatest
10
Parts of the program that... Assume that sets are... Using...
Use sets to contain values Unordered collections
![Page 68: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/68.jpg)
Sets as Ordered Sequences
Proposal 2: A set is represented by a linked list with unique elements that is ordered from least to greatest
10
Parts of the program that... Assume that sets are... Using...
Use sets to contain values Unordered collections empty, set_contains, adjoin_set, intersect_set, union_set
![Page 69: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/69.jpg)
Sets as Ordered Sequences
Proposal 2: A set is represented by a linked list with unique elements that is ordered from least to greatest
10
Parts of the program that... Assume that sets are... Using...
Use sets to contain values Unordered collections empty, set_contains, adjoin_set, intersect_set, union_set
Implement set operations
![Page 70: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/70.jpg)
Sets as Ordered Sequences
Proposal 2: A set is represented by a linked list with unique elements that is ordered from least to greatest
10
Parts of the program that... Assume that sets are... Using...
Use sets to contain values Unordered collections empty, set_contains, adjoin_set, intersect_set, union_set
Implement set operations Ordered linked lists
![Page 71: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/71.jpg)
Sets as Ordered Sequences
Proposal 2: A set is represented by a linked list with unique elements that is ordered from least to greatest
10
Parts of the program that... Assume that sets are... Using...
Use sets to contain values Unordered collections empty, set_contains, adjoin_set, intersect_set, union_set
Implement set operations Ordered linked lists first, rest, <, >, ==
![Page 72: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/72.jpg)
Sets as Ordered Sequences
Proposal 2: A set is represented by a linked list with unique elements that is ordered from least to greatest
10
Parts of the program that... Assume that sets are... Using...
Use sets to contain values Unordered collections empty, set_contains, adjoin_set, intersect_set, union_set
Implement set operations Ordered linked lists first, rest, <, >, ==
![Page 73: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/73.jpg)
Sets as Ordered Sequences
Proposal 2: A set is represented by a linked list with unique elements that is ordered from least to greatest
10
Parts of the program that... Assume that sets are... Using...
Use sets to contain values Unordered collections empty, set_contains, adjoin_set, intersect_set, union_set
Implement set operations Ordered linked lists first, rest, <, >, ==
Different parts of a program may make different assumptions about data
![Page 74: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/74.jpg)
Sets as Ordered Sequences
Proposal 2: A set is represented by a linked list with unique elements that is ordered from least to greatest
11
![Page 75: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/75.jpg)
Sets as Ordered Sequences
Proposal 2: A set is represented by a linked list with unique elements that is ordered from least to greatest
11
def intersect_set(set1, set2):
![Page 76: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/76.jpg)
Sets as Ordered Sequences
Proposal 2: A set is represented by a linked list with unique elements that is ordered from least to greatest
11
def intersect_set(set1, set2): if empty(set1) or empty(set2): return Link.empty
![Page 77: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/77.jpg)
Sets as Ordered Sequences
Proposal 2: A set is represented by a linked list with unique elements that is ordered from least to greatest
11
def intersect_set(set1, set2): if empty(set1) or empty(set2): return Link.empty else:
![Page 78: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/78.jpg)
Sets as Ordered Sequences
Proposal 2: A set is represented by a linked list with unique elements that is ordered from least to greatest
11
def intersect_set(set1, set2): if empty(set1) or empty(set2): return Link.empty else: e1, e2 = set1.first, set2.first
![Page 79: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/79.jpg)
Sets as Ordered Sequences
Proposal 2: A set is represented by a linked list with unique elements that is ordered from least to greatest
11
def intersect_set(set1, set2): if empty(set1) or empty(set2): return Link.empty else: e1, e2 = set1.first, set2.first if e1 == e2: return Link(e1, intersect_set(set1.rest, set2.rest))
![Page 80: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/80.jpg)
Sets as Ordered Sequences
Proposal 2: A set is represented by a linked list with unique elements that is ordered from least to greatest
11
def intersect_set(set1, set2): if empty(set1) or empty(set2): return Link.empty else: e1, e2 = set1.first, set2.first if e1 == e2: return Link(e1, intersect_set(set1.rest, set2.rest)) elif e1 < e2: return intersect_set(set1.rest, set2)
![Page 81: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/81.jpg)
Sets as Ordered Sequences
Proposal 2: A set is represented by a linked list with unique elements that is ordered from least to greatest
11
def intersect_set(set1, set2): if empty(set1) or empty(set2): return Link.empty else: e1, e2 = set1.first, set2.first if e1 == e2: return Link(e1, intersect_set(set1.rest, set2.rest)) elif e1 < e2: return intersect_set(set1.rest, set2) elif e2 < e1: return intersect_set(set1, set2.rest)
![Page 82: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/82.jpg)
Sets as Ordered Sequences
Proposal 2: A set is represented by a linked list with unique elements that is ordered from least to greatest
11
def intersect_set(set1, set2): if empty(set1) or empty(set2): return Link.empty else: e1, e2 = set1.first, set2.first if e1 == e2: return Link(e1, intersect_set(set1.rest, set2.rest)) elif e1 < e2: return intersect_set(set1.rest, set2) elif e2 < e1: return intersect_set(set1, set2.rest)
Order of growth?
![Page 83: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/83.jpg)
Sets as Ordered Sequences
Proposal 2: A set is represented by a linked list with unique elements that is ordered from least to greatest
11
def intersect_set(set1, set2): if empty(set1) or empty(set2): return Link.empty else: e1, e2 = set1.first, set2.first if e1 == e2: return Link(e1, intersect_set(set1.rest, set2.rest)) elif e1 < e2: return intersect_set(set1.rest, set2) elif e2 < e1: return intersect_set(set1, set2.rest)
�(n)Order of growth?
![Page 84: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/84.jpg)
Sets as Binary Search Trees
![Page 85: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/85.jpg)
Binary Search Trees
13
![Page 86: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/86.jpg)
Binary Search Trees
Proposal 3: A set is represented as a Tree with two branches. Each entry is:
13
![Page 87: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/87.jpg)
Binary Search Trees
Proposal 3: A set is represented as a Tree with two branches. Each entry is:
• Larger than all entries in its left branch and
13
![Page 88: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/88.jpg)
Binary Search Trees
Proposal 3: A set is represented as a Tree with two branches. Each entry is:
• Larger than all entries in its left branch and
• Smaller than all entries in its right branch
13
![Page 89: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/89.jpg)
Binary Search Trees
Proposal 3: A set is represented as a Tree with two branches. Each entry is:
• Larger than all entries in its left branch and
• Smaller than all entries in its right branch
7
3
1 5
9
11
13
![Page 90: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/90.jpg)
Binary Search Trees
Proposal 3: A set is represented as a Tree with two branches. Each entry is:
• Larger than all entries in its left branch and
• Smaller than all entries in its right branch
7
3
1 5
9
11
7
3
1
5 9
11
13
![Page 91: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/91.jpg)
Binary Search Trees
Proposal 3: A set is represented as a Tree with two branches. Each entry is:
• Larger than all entries in its left branch and
• Smaller than all entries in its right branch
7
3
1 5
9
11
7
3
1
5 9
11
5
3
1 7
9
11
13
![Page 92: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/92.jpg)
Binary Tree Class
14
![Page 93: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/93.jpg)
Binary Tree Class
A binary tree is a tree that has a left branch and a right branch
14
![Page 94: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/94.jpg)
Binary Tree Class
A binary tree is a tree that has a left branch and a right branch
14
7
3
1
5 9
11
![Page 95: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/95.jpg)
Binary Tree Class
A binary tree is a tree that has a left branch and a right branch
Idea: Fill the place of a missing left branch with an empty tree
14
7
3
1
5 9
11
![Page 96: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/96.jpg)
Binary Tree Class
A binary tree is a tree that has a left branch and a right branch
Idea: Fill the place of a missing left branch with an empty tree
14
7
3
1
5 9
11E
![Page 97: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/97.jpg)
Binary Tree Class
A binary tree is a tree that has a left branch and a right branch
Idea: Fill the place of a missing left branch with an empty tree
14
7
3
1
5 9
11E
E: An empty tree
![Page 98: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/98.jpg)
Binary Tree Class
A binary tree is a tree that has a left branch and a right branch
Idea: Fill the place of a missing left branch with an empty tree
Idea 2: An instance of BinaryTree always has exactly two branches
14
7
3
1
5 9
11E
E: An empty tree
![Page 99: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/99.jpg)
Binary Tree Class
A binary tree is a tree that has a left branch and a right branch
Idea: Fill the place of a missing left branch with an empty tree
Idea 2: An instance of BinaryTree always has exactly two branches
14
7
3
1
5 9
11E
E: An empty tree
E E
E E
E E
![Page 100: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/100.jpg)
Binary Tree Class
A binary tree is a tree that has a left branch and a right branch
Idea: Fill the place of a missing left branch with an empty tree
Idea 2: An instance of BinaryTree always has exactly two branches
14
7
3
1
5 9
11E
E: An empty tree
E E
E E
E E
class BinaryTree(Tree):
![Page 101: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/101.jpg)
Binary Tree Class
A binary tree is a tree that has a left branch and a right branch
Idea: Fill the place of a missing left branch with an empty tree
Idea 2: An instance of BinaryTree always has exactly two branches
14
7
3
1
5 9
11E
E: An empty tree
E E
E E
E E
class BinaryTree(Tree): empty = Tree(None) empty.is_empty = True
![Page 102: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/102.jpg)
Binary Tree Class
A binary tree is a tree that has a left branch and a right branch
Idea: Fill the place of a missing left branch with an empty tree
Idea 2: An instance of BinaryTree always has exactly two branches
14
7
3
1
5 9
11E
E: An empty tree
E E
E E
E E
class BinaryTree(Tree): empty = Tree(None) empty.is_empty = True
def __init__(self, entry, left=empty, right=empty): Tree.__init__(self, entry, (left, right)) self.is_empty = False
![Page 103: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/103.jpg)
Binary Tree Class
A binary tree is a tree that has a left branch and a right branch
Idea: Fill the place of a missing left branch with an empty tree
Idea 2: An instance of BinaryTree always has exactly two branches
14
7
3
1
5 9
11E
E: An empty tree
E E
E E
E E
class BinaryTree(Tree): empty = Tree(None) empty.is_empty = True
def __init__(self, entry, left=empty, right=empty): Tree.__init__(self, entry, (left, right)) self.is_empty = False
@property def left(self): return self.branches[0]
![Page 104: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/104.jpg)
Binary Tree Class
A binary tree is a tree that has a left branch and a right branch
Idea: Fill the place of a missing left branch with an empty tree
Idea 2: An instance of BinaryTree always has exactly two branches
14
7
3
1
5 9
11E
E: An empty tree
E E
E E
E E
class BinaryTree(Tree): empty = Tree(None) empty.is_empty = True
def __init__(self, entry, left=empty, right=empty): Tree.__init__(self, entry, (left, right)) self.is_empty = False
@property def left(self): return self.branches[0]
@property def right(self): return self.branches[1]
![Page 105: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/105.jpg)
Binary Tree Class
A binary tree is a tree that has a left branch and a right branch
Idea: Fill the place of a missing left branch with an empty tree
Idea 2: An instance of BinaryTree always has exactly two branches
14
7
3
1
5 9
11E
E: An empty tree
E E
E E
E E
class BinaryTree(Tree): empty = Tree(None) empty.is_empty = True
def __init__(self, entry, left=empty, right=empty): Tree.__init__(self, entry, (left, right)) self.is_empty = False
@property def left(self): return self.branches[0]
@property def right(self): return self.branches[1]
Bin = BinaryTree
![Page 106: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/106.jpg)
Binary Tree Class
A binary tree is a tree that has a left branch and a right branch
Idea: Fill the place of a missing left branch with an empty tree
Idea 2: An instance of BinaryTree always has exactly two branches
14
7
3
1
5 9
11E
E: An empty tree
E E
E E
E E
class BinaryTree(Tree): empty = Tree(None) empty.is_empty = True
def __init__(self, entry, left=empty, right=empty): Tree.__init__(self, entry, (left, right)) self.is_empty = False
@property def left(self): return self.branches[0]
@property def right(self): return self.branches[1]
Bin = BinaryTreet = Bin(3, Bin(1), Bin(7, Bin(5), Bin(9, Bin.empty, Bin(11))))
![Page 107: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/107.jpg)
Membership in Binary Search Trees
15
![Page 108: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/108.jpg)
Membership in Binary Search Trees
set_contains traverses the tree
15
![Page 109: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/109.jpg)
Membership in Binary Search Trees
set_contains traverses the tree
• If the element is not the entry, it can only be in either the left or right branch
15
![Page 110: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/110.jpg)
Membership in Binary Search Trees
set_contains traverses the tree
• If the element is not the entry, it can only be in either the left or right branch
• By focusing on one branch, we reduce the set by about half with each recursive call
15
![Page 111: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/111.jpg)
Membership in Binary Search Trees
set_contains traverses the tree
• If the element is not the entry, it can only be in either the left or right branch
• By focusing on one branch, we reduce the set by about half with each recursive call
5
3
1 7
9
11
15
![Page 112: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/112.jpg)
Membership in Binary Search Trees
set_contains traverses the tree
• If the element is not the entry, it can only be in either the left or right branch
• By focusing on one branch, we reduce the set by about half with each recursive call
5
3
1 7
9
11
9
15
![Page 113: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/113.jpg)
Membership in Binary Search Trees
set_contains traverses the tree
• If the element is not the entry, it can only be in either the left or right branch
• By focusing on one branch, we reduce the set by about half with each recursive call
5
3
1 7
9
11
def set_contains(s, v): 9
15
![Page 114: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/114.jpg)
Membership in Binary Search Trees
set_contains traverses the tree
• If the element is not the entry, it can only be in either the left or right branch
• By focusing on one branch, we reduce the set by about half with each recursive call
5
3
1 7
9
11
def set_contains(s, v): if s.is_empty: return False
9
15
![Page 115: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/115.jpg)
Membership in Binary Search Trees
set_contains traverses the tree
• If the element is not the entry, it can only be in either the left or right branch
• By focusing on one branch, we reduce the set by about half with each recursive call
5
3
1 7
9
11
def set_contains(s, v): if s.is_empty: return False elif s.entry == v: return True
9
15
![Page 116: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/116.jpg)
Membership in Binary Search Trees
set_contains traverses the tree
• If the element is not the entry, it can only be in either the left or right branch
• By focusing on one branch, we reduce the set by about half with each recursive call
5
3
1 7
9
11
def set_contains(s, v): if s.is_empty: return False elif s.entry == v: return True elif s.entry < v: return set_contains(s.right, v)
9
15
![Page 117: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/117.jpg)
Membership in Binary Search Trees
set_contains traverses the tree
• If the element is not the entry, it can only be in either the left or right branch
• By focusing on one branch, we reduce the set by about half with each recursive call
5
3
1 7
9
11
def set_contains(s, v): if s.is_empty: return False elif s.entry == v: return True elif s.entry < v: return set_contains(s.right, v)
9
If 9 is in the set, it is in this branch
15
![Page 118: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/118.jpg)
Membership in Binary Search Trees
set_contains traverses the tree
• If the element is not the entry, it can only be in either the left or right branch
• By focusing on one branch, we reduce the set by about half with each recursive call
5
3
1 7
9
11
def set_contains(s, v): if s.is_empty: return False elif s.entry == v: return True elif s.entry < v: return set_contains(s.right, v) elif s.entry > v: return set_contains(s.left, v)
9
If 9 is in the set, it is in this branch
15
![Page 119: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/119.jpg)
Membership in Binary Search Trees
set_contains traverses the tree
• If the element is not the entry, it can only be in either the left or right branch
• By focusing on one branch, we reduce the set by about half with each recursive call
5
3
1 7
9
11
def set_contains(s, v): if s.is_empty: return False elif s.entry == v: return True elif s.entry < v: return set_contains(s.right, v) elif s.entry > v: return set_contains(s.left, v)
9
If 9 is in the set, it is in this branch
Order of growth?15
![Page 120: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/120.jpg)
Membership in Binary Search Trees
set_contains traverses the tree
• If the element is not the entry, it can only be in either the left or right branch
• By focusing on one branch, we reduce the set by about half with each recursive call
5
3
1 7
9
11
def set_contains(s, v): if s.is_empty: return False elif s.entry == v: return True elif s.entry < v: return set_contains(s.right, v) elif s.entry > v: return set_contains(s.left, v)
9
If 9 is in the set, it is in this branch
Order of growth?15
on average⇥(h)
![Page 121: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/121.jpg)
Membership in Binary Search Trees
set_contains traverses the tree
• If the element is not the entry, it can only be in either the left or right branch
• By focusing on one branch, we reduce the set by about half with each recursive call
5
3
1 7
9
11
def set_contains(s, v): if s.is_empty: return False elif s.entry == v: return True elif s.entry < v: return set_contains(s.right, v) elif s.entry > v: return set_contains(s.left, v)
9
If 9 is in the set, it is in this branch
Order of growth?15
on average⇥(h) ⇥(log n) on average for a balanced tree
![Page 122: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/122.jpg)
Adjoining to a Tree Set
16
![Page 123: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/123.jpg)
Adjoining to a Tree Set
5
3
1 7
9
11
8
16
![Page 124: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/124.jpg)
Adjoining to a Tree Set
5
3
1 7
9
11
8
Right!
16
![Page 125: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/125.jpg)
Adjoining to a Tree Set
5
3
1 7
9
11
8
Right!
16
![Page 126: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/126.jpg)
Adjoining to a Tree Set
5
3
1 7
9
11
8
7
9
11
8
Right!
16
![Page 127: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/127.jpg)
Adjoining to a Tree Set
5
3
1 7
9
11
8
7
9
11
8
Right! Left!
16
![Page 128: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/128.jpg)
Adjoining to a Tree Set
5
3
1 7
9
11
8
7
9
11
8
7
8
Right! Left!
16
![Page 129: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/129.jpg)
Adjoining to a Tree Set
5
3
1 7
9
11
8
7
9
11
8
7
8
Right! Left!
E E
16
![Page 130: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/130.jpg)
Adjoining to a Tree Set
5
3
1 7
9
11
8
7
9
11
8
7
8
Right! Left! Right!
E E
16
![Page 131: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/131.jpg)
Adjoining to a Tree Set
5
3
1 7
9
11
8
7
9
11
8
7
8
Right! Left! Right!
E E
8
E
16
![Page 132: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/132.jpg)
Adjoining to a Tree Set
5
3
1 7
9
11
8
7
9
11
8
7
8
Right! Left! Right!
E E
8
E
Stop!
16
![Page 133: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/133.jpg)
Adjoining to a Tree Set
5
3
1 7
9
11
8
7
9
11
8
7
8
Right! Left! Right!
E E
8
E
Stop!
16
![Page 134: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/134.jpg)
Adjoining to a Tree Set
5
3
1 7
9
11
8
7
9
11
8
7
8
Right! Left! Right!
E E
8
E
Stop!
8
16
![Page 135: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/135.jpg)
Adjoining to a Tree Set
5
3
1 7
9
11
8
7
9
11
8
7
8
Right! Left! Right!
E E
8
E
Stop!
87
8
16
![Page 136: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/136.jpg)
Adjoining to a Tree Set
5
3
1 7
9
11
8
7
9
11
8
7
8
Right! Left! Right!
E E
8
E
Stop!
87
87
9
11
8
16
![Page 137: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/137.jpg)
Adjoining to a Tree Set
5
3
1 7
9
11
8
7
9
11
8
7
8
Right! Left! Right!
E E
8
E
Stop!
87
87
9
11
8
5
3
1 7
9
11
8
16
![Page 138: 61A Lecture 21 - University of California, Berkeleycs61a/sp15/assets/slides/21-Sets_1pp.pdf · HKN review session on Sunday 3/15 12-3pm in 10 Evans Conflict form submissions are due](https://reader030.fdocuments.us/reader030/viewer/2022041205/5d58e3f988c993f97c8b4cc1/html5/thumbnails/138.jpg)
Adjoining to a Tree Set
5
3
1 7
9
11
8
7
9
11
8
7
8
Right! Left! Right!
E E
8
E
Stop!
87
87
9
11
8
5
3
1 7
9
11
8 (Demo)16