Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you...
Transcript of Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you...
![Page 1: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/1.jpg)
Binary Trees
Introduction to Computer ScienceIris Howley
![Page 2: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/2.jpg)
TODAY’S LESSONBinary Trees
(A data structure for holding a different sort of data)
![Page 3: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/3.jpg)
GAME: TWENTY QUESTIONS
• The "Knower" thinks of a noun• The "Guesser(s)" ask yes/no questions in an attempt to guess the
noun• The Knower responds with only yes/no answers• The Guesser aims to find the Knower's noun with fewer than 20
questions.
![Page 4: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/4.jpg)
Hey Pixel.
Yeah, Dizzy?
![Page 5: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/5.jpg)
I'm thinking of something. Can you guess it?
What is it?
![Page 6: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/6.jpg)
Yes/No questions only.
Okay, is it a toy?
![Page 7: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/7.jpg)
No. Toys are dumb.
Is it tasty?
![Page 8: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/8.jpg)
Yes.
Is it savory?
![Page 9: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/9.jpg)
Yes.
Does it come from the fridge?
![Page 10: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/10.jpg)
No.
Is it dog snacks?
![Page 11: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/11.jpg)
No.
Is it from an animal?
![Page 12: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/12.jpg)
Yes.
Is it chicken?
![Page 13: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/13.jpg)
No, but nice try, loser.
Is it tuna?
![Page 14: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/14.jpg)
YASSSSSSSSSSSSSSS.
I'm a winner?
![Page 15: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/15.jpg)
Is it a toy?
Is it tasty?
Is it savory?
no
yes
Comes from fridge?
Twenty Questions
no
Is it dog snacks?no
Is it chicken?no
Is it tuna?
yes
![Page 16: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/16.jpg)
Is it a toy?
Is it tasty?
Does it have 4 legs?Is it savory?
no
noyesDoes it squeak?
yes
yes
Is a cat?
yes
Is it my squeaky goose?
Twenty Questions
no
Is it dog snacks?no
Is it chicken?no
Is it tuna?What does this structure resemble?
Comes from fridge?
yes
![Page 17: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/17.jpg)
Is it alive?
Is it food?
Does it have 4 legs?Is it sweet?
no
noyesDoes it have 8 legs?
yes
yes
Is it a table?
yes
Is it an octopus?no
Is it a pretzel?
Twenty Questions
A more balanced game might be clearer…
![Page 18: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/18.jpg)
![Page 19: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/19.jpg)
Is it alive?
Is it food?
Does it have 4 legs?Is it sweet?
no
noyesDoes it have 8 legs?
yes
yes
Is it a table?
yes
Is it an octopus?no
Is it a pretzel?
Twenty Questions Tree__slots__ = [_value, _left, _right]
If it’s a leaf, it’s a guess
‘yes’ goes to the left‘no’ to the right
Questions stored as the value
![Page 20: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/20.jpg)
questions?? ??? ?
?
??
?Please contact me!
![Page 21: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/21.jpg)
Building Binary Trees
Introduction to Computer ScienceIris Howley
![Page 22: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/22.jpg)
TODAY’S LESSONBinary Trees
(A data structure for holding a different sort of data)
![Page 23: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/23.jpg)
Is it alive?
Is it food?
Does it have 4 legs?Is it sweet?
no
noyesDoes it have 8 legs?
yes
yes
Is it a table?
yes
Is it an octopus?no
Is it a pretzel?
Twenty Questions Tree__slots__ = [_value, _left, _right]
If it’s a leaf, it’s a guess
‘yes’ goes to the left‘no’ to the right
Questions stored as the value
![Page 24: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/24.jpg)
Creating a Tree
• t2 = Tree('Does it have 8 legs?')• t3 = Tree('Is it food?')• mytree = Tree('Is it alive?', t2, t3)
![Page 25: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/25.jpg)
Adding Nodes to the Tree
• Octopus?
• t4 = ("octopus?")
•à t2 = Tree('8 legs?',t4)
![Page 26: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/26.jpg)
Accessing Nodes in a Tree
• print(mytree.value) 'Is it alive?'
• print(mytree.left.value) 'Does it have 8 legs?'
• print(mytree.left.left.value)'Is it an octopus?'
![Page 27: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/27.jpg)
What does this code do?
def mystery(self):if not self.right:
return self
return self.right.mystery()
See POGIL 45 on Binary Trees!
![Page 28: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/28.jpg)
Is it alive?
Is it food?
Does it have 4 legs?Is it sweet?
no
noyesDoes it have 8 legs?
yes
yes
Is it a table?
yes
Is it an octopus?no
Is it a pretzel?
Twenty Questions Tree
![Page 29: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/29.jpg)
Binary Tree
• Let’s write a contains(..) method for a tree§ (Application Question #3 from POGIL #45)
• >>> from tree import *• >>> mytree = Tree(99, Tree(33), Tree(66))• >>> 66 in mytree• True• >>> 24 in mytree• False
# __contains__() is implicitly called with “if ___ in <sequence>”
![Page 30: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/30.jpg)
Steps for Recursion
1. Know when to stop.2. Decide how to take one,
repeated step.3. Break the journey down into
that step plus a smaller journey.
30
![Page 31: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/31.jpg)
Contains Method for Tree
• Stopping/Base Case:1. We’ve found the value2. Or we’re a leaf!
• Small step§ Check if we’re the value
• Break the journey down§ Check the left child, then the right (if it’s not in the left side)
![Page 32: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/32.jpg)
Contains Method for tree
def __contains__(self, v): # Base case if self.value == v:
return True
l = v in self.left if self.left else False r = v in self.right if not l and self.right else False
return l or r
# __contains__() is implicitly called with “if ___ in <sequence>”
# if not l lets us skip the right side, if we found it in the left already
![Page 33: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/33.jpg)
questions?? ??? ?
?
??
?Please contact me!
![Page 34: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/34.jpg)
Using Binary Trees
Introduction to Computer ScienceIris Howley
![Page 35: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/35.jpg)
TODAY’S LESSONUsing a Binary Tree
(Let's use our new data structure for something fun!)
![Page 36: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/36.jpg)
Is it a toy?
Is it tasty?
Does it have 4 legs?Is it savory?
no
noyesDoes it squeak?
yes
yes
Is a cat?
yes
Is it my squeaky goose?
Twenty Questions Tree
no
Is it dog snacks?no
Is it chicken?no
Is it tuna?
Store our questions/answers so we can play the game with the computer.
Comes from fridge?
yes
![Page 37: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/37.jpg)
q20.py
A program to play 20 Questions, using our tree data structure
See q20.py on the course website.
![Page 38: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/38.jpg)
questions?? ??? ?
?
??
?Please contact me!
![Page 39: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/39.jpg)
Leftover Slides
![Page 40: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/40.jpg)
Steps for Recursion•Know when to stop.•Decide how to take one step.•Break the journey down into that step plus a smaller journey.
40
![Page 41: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/41.jpg)
__str__ versus __repr__
• __str__ returns a human-readable string representation of the object• Implicitly called with print(object) or str(object)• Also called with '{!s}'.format(object) in a format string
• __repr__ produces a machine-readable string representation of the object• Implicitly called in interactive python: >>> object• Also called with '{!r}'.format(object) in a format string
![Page 42: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/42.jpg)
OBJECT PERSISTENCE
Storing objects for future use.
![Page 43: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/43.jpg)
POGIL Activity #41 – object persistence
• Find a partner and spend a few minutes discussing your responses to the POGIL worksheet, Question 1-3.
• Be prepared to report out your responses!
This is a brand new POGIL activity, let me know if you encounter any issues, typos, etc.
![Page 44: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/44.jpg)
Time’s up!
![Page 45: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/45.jpg)
Report out!
![Page 46: Binary Trees - williams-cs.github.io · Hey Pixel. Yeah, Dizzy? I'm thinking of something. Can you guess it? What is it? Yes/No questions only. Okay, is it a toy? No. Toys are dumb.](https://reader034.fdocuments.us/reader034/viewer/2022050610/5fb10f0593a40d577f60ca8b/html5/thumbnails/46.jpg)
Object Persistence
HOW MIGHT THIS BE USEFUL FOR
OUR GAME OF TWENTY QUESTIONS?