SLIDE 1
61A Lecture 21 Announcements Binary Trees Binary Tree Class 4 - - PowerPoint PPT Presentation
61A Lecture 21 Announcements Binary Trees Binary Tree Class 4 - - PowerPoint PPT Presentation
61A Lecture 21 Announcements Binary Trees Binary Tree Class 4 Binary Tree Class class BTree(Tree): 4 Binary Tree Class A binary tree is a tree that has class BTree(Tree): a left branch and a right branch 4 Binary Tree Class A binary
SLIDE 2
SLIDE 3
Binary Trees
SLIDE 4
Binary Tree Class
4
SLIDE 5
Binary Tree Class
4
class BTree(Tree):
SLIDE 6
Binary Tree Class
A binary tree is a tree that has a left branch and a right branch
4
class BTree(Tree):
SLIDE 7
Binary Tree Class
A binary tree is a tree that has a left branch and a right branch
4
7 3 1 5 9 11 class BTree(Tree):
SLIDE 8
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
4
7 3 1 5 9 11 class BTree(Tree):
SLIDE 9
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
4
7 3 1 5 9 11
E
class BTree(Tree):
SLIDE 10
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
4
7 3 1 5 9 11
E E: An empty tree
class BTree(Tree):
SLIDE 11
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
4
7 3 1 5 9 11
E E: An empty tree
class BTree(Tree): empty = Tree(None)
SLIDE 12
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 BTree always has exactly two branches
4
7 3 1 5 9 11
E E: An empty tree
class BTree(Tree): empty = Tree(None)
SLIDE 13
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 BTree always has exactly two branches
4
7 3 1 5 9 11
E E: An empty tree E E E E E E
class BTree(Tree): empty = Tree(None)
SLIDE 14
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 BTree always has exactly two branches
4
7 3 1 5 9 11
E E: An empty tree E E E E E E
class BTree(Tree): empty = Tree(None) def __init__(self, root, left=empty, right=empty): Tree.__init__(self, root, [left, right])
SLIDE 15
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 BTree always has exactly two branches
4
7 3 1 5 9 11
E E: An empty tree E E E E E E
class BTree(Tree): empty = Tree(None) def __init__(self, root, left=empty, right=empty): Tree.__init__(self, root, [left, right]) @property def left(self): return self.branches[0]
SLIDE 16
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 BTree always has exactly two branches
4
7 3 1 5 9 11
E E: An empty tree E E E E E E
class BTree(Tree): empty = Tree(None) def __init__(self, root, left=empty, right=empty): Tree.__init__(self, root, [left, right]) @property def left(self): return self.branches[0] @property def right(self): return self.branches[1]
SLIDE 17
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 BTree always has exactly two branches
4
7 3 1 5 9 11
E E: An empty tree E E E E E E
class BTree(Tree): empty = Tree(None) def __init__(self, root, left=empty, right=empty): Tree.__init__(self, root, [left, right]) @property def left(self): return self.branches[0] @property def right(self): return self.branches[1] t = BTree(3, BTree(1), BTree(7, BTree(5), BTree(9, BTree.empty, BTree(11))))
SLIDE 18
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 BTree always has exactly two branches
4
7 3 1 5 9 11
E E: An empty tree E E E E E E
class BTree(Tree): empty = Tree(None) def __init__(self, root, left=empty, right=empty): Tree.__init__(self, root, [left, right]) @property def left(self): return self.branches[0] @property def right(self): return self.branches[1] t = BTree(3, BTree(1), BTree(7, BTree(5), BTree(9, BTree.empty, BTree(11)))) (Demo)
SLIDE 19
Binary Search Trees
SLIDE 20
Binary Search
A strategy for finding a value in a sorted list: check the middle and eliminate half
6
SLIDE 21
Binary Search
A strategy for finding a value in a sorted list: check the middle and eliminate half
6
[1, 2, 4, 8, 16, 32, 64] 20 in
SLIDE 22
Binary Search
A strategy for finding a value in a sorted list: check the middle and eliminate half
6
[1, 2, 4, 8, 16, 32, 64] 20 in
SLIDE 23
Binary Search
A strategy for finding a value in a sorted list: check the middle and eliminate half
6
[1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] 20 in
SLIDE 24
Binary Search
A strategy for finding a value in a sorted list: check the middle and eliminate half
6
[1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] 20 in
SLIDE 25
Binary Search
A strategy for finding a value in a sorted list: check the middle and eliminate half
6
[1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] 20 in
SLIDE 26
Binary Search
A strategy for finding a value in a sorted list: check the middle and eliminate half
6
[1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] 20 in
SLIDE 27
Binary Search
A strategy for finding a value in a sorted list: check the middle and eliminate half
6
[1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] 20 in False
SLIDE 28
Binary Search
A strategy for finding a value in a sorted list: check the middle and eliminate half
6
[1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] 20 in False [1, 2, 4, 8, 16, 32] 4 in
SLIDE 29
Binary Search
A strategy for finding a value in a sorted list: check the middle and eliminate half
6
[1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] 20 in False [1, 2, 4, 8, 16, 32] 4 in
SLIDE 30
Binary Search
A strategy for finding a value in a sorted list: check the middle and eliminate half
6
[1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] 20 in False [1, 2, 4, 8, 16, 32] [1, 2, 4, 8, 16, 32] 4 in
SLIDE 31
Binary Search
A strategy for finding a value in a sorted list: check the middle and eliminate half
6
[1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] 20 in False [1, 2, 4, 8, 16, 32] [1, 2, 4, 8, 16, 32] 4 in
SLIDE 32
Binary Search
A strategy for finding a value in a sorted list: check the middle and eliminate half
6
[1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] 20 in False [1, 2, 4, 8, 16, 32] [1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32] 4 in
SLIDE 33
Binary Search
A strategy for finding a value in a sorted list: check the middle and eliminate half
6
[1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] 20 in False [1, 2, 4, 8, 16, 32] [1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32] 4 in
SLIDE 34
Binary Search
A strategy for finding a value in a sorted list: check the middle and eliminate half
6
[1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] 20 in False [1, 2, 4, 8, 16, 32] [1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32] 4 in True
SLIDE 35
Binary Search
A strategy for finding a value in a sorted list: check the middle and eliminate half
6
[1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] 20 in False [1, 2, 4, 8, 16, 32] [1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32] 4 in True
For a sorted list of length n, what Theta expression describes the time required?
SLIDE 36
Binary Search
A strategy for finding a value in a sorted list: check the middle and eliminate half
6
[1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] 20 in False [1, 2, 4, 8, 16, 32] [1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32] 4 in True
For a sorted list of length n, what Theta expression describes the time required? Θ(log n)
SLIDE 37
Binary Search Trees
7
SLIDE 38
Binary Search Trees
A binary search tree is a binary tree where each root value is:
7
SLIDE 39
Binary Search Trees
A binary search tree is a binary tree where each root value is:
- Larger than all entries in its left branch and
7
SLIDE 40
Binary Search Trees
A binary search tree is a binary tree where each root value is:
- Larger than all entries in its left branch and
- Smaller than all entries in its right branch
7
SLIDE 41
Binary Search Trees
A binary search tree is a binary tree where each root value 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
SLIDE 42
Binary Search Trees
A binary search tree is a binary tree where each root value 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
7
SLIDE 43
Binary Search Trees
A binary search tree is a binary tree where each root value 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
7
SLIDE 44
Binary Search Trees
A binary search tree is a binary tree where each root value 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
7
(Demo)
SLIDE 45
Discussion Questions
What's the largest element in a binary search tree?
8
SLIDE 46
Discussion Questions
What's the largest element in a binary search tree?
8
def largest(t): if _________________________: return _________________ else: return _________________
SLIDE 47
Discussion Questions
What's the largest element in a binary search tree?
8
def largest(t): if _________________________: return _________________ else: return _________________ 5 3 1 7 9 11
SLIDE 48
Discussion Questions
What's the largest element in a binary search tree?
8
def largest(t): if _________________________: return _________________ else: return _________________ 5 3 1 7 9 11 5 3 1 7 9 8
SLIDE 49
Discussion Questions
What's the largest element in a binary search tree?
8
def largest(t): if _________________________: return _________________ else: return _________________ t.right is BTree.empty t.root 5 3 1 7 9 11 5 3 1 7 9 8
SLIDE 50
Discussion Questions
What's the largest element in a binary search tree?
8
def largest(t): if _________________________: return _________________ else: return _________________ t.right is BTree.empty t.root largest(t.right) 5 3 1 7 9 11 5 3 1 7 9 8
SLIDE 51
Discussion Questions
What's the largest element in a binary search tree?
8
def largest(t): if _________________________: return _________________ else: return _________________ What's the second largest element in a binary search tree? t.right is BTree.empty t.root largest(t.right) 5 3 1 7 9 11 5 3 1 7 9 8
SLIDE 52
Discussion Questions
What's the largest element in a binary search tree?
8
def largest(t): if _________________________: return _________________ else: return _________________ What's the second largest element in a binary search tree? def second(t): if t.is_leaf(): return None elif _______________________: return t.root elif _______________________: return _________________ else: return _________________ t.right is BTree.empty t.root largest(t.right) 5 3 1 7 9 11 5 3 1 7 9 8
SLIDE 53
Discussion Questions
What's the largest element in a binary search tree?
8
def largest(t): if _________________________: return _________________ else: return _________________ What's the second largest element in a binary search tree? def second(t): if t.is_leaf(): return None elif _______________________: return t.root elif _______________________: return _________________ else: return _________________ t.right is BTree.empty t.root largest(t.right) 5 3 1 7 9 11 5 3 1 7 9 8
SLIDE 54
Discussion Questions
What's the largest element in a binary search tree?
8
def largest(t): if _________________________: return _________________ else: return _________________ What's the second largest element in a binary search tree? def second(t): if t.is_leaf(): return None elif _______________________: return t.root elif _______________________: return _________________ else: return _________________ t.right is BTree.empty t.root largest(t.right) 5 3 1 7 9 11 5 3 1 7 9 8
SLIDE 55
Discussion Questions
What's the largest element in a binary search tree?
8
def largest(t): if _________________________: return _________________ else: return _________________ What's the second largest element in a binary search tree? def second(t): if t.is_leaf(): return None elif _______________________: return t.root elif _______________________: return _________________ else: return _________________ t.right is BTree.empty t.root largest(t.right) 5 3 1 7 9 11 5 3 1 7 9 8
SLIDE 56
Discussion Questions
What's the largest element in a binary search tree?
8
def largest(t): if _________________________: return _________________ else: return _________________ What's the second largest element in a binary search tree? def second(t): if t.is_leaf(): return None elif _______________________: return t.root elif _______________________: return _________________ else: return _________________ t.right is BTree.empty t.root largest(t.right) 5 3 1 7 9 11 5 3 1 7 9 8
SLIDE 57
Discussion Questions
What's the largest element in a binary search tree?
8
def largest(t): if _________________________: return _________________ else: return _________________ What's the second largest element in a binary search tree? def second(t): if t.is_leaf(): return None elif _______________________: return t.root elif _______________________: return _________________ else: return _________________ t.right is BTree.empty t.root largest(t.right) 5 3 1 7 9 11 5 3 1 7 9 8 t.right.is_leaf()
SLIDE 58
Discussion Questions
What's the largest element in a binary search tree?
8
def largest(t): if _________________________: return _________________ else: return _________________ What's the second largest element in a binary search tree? def second(t): if t.is_leaf(): return None elif _______________________: return t.root elif _______________________: return _________________ else: return _________________ t.right is BTree.empty t.root largest(t.right) 5 3 1 7 9 11 5 3 1 7 9 8 t.right is BTree.empty t.right.is_leaf()
SLIDE 59
Discussion Questions
What's the largest element in a binary search tree?
8
def largest(t): if _________________________: return _________________ else: return _________________ What's the second largest element in a binary search tree? def second(t): if t.is_leaf(): return None elif _______________________: return t.root elif _______________________: return _________________ else: return _________________ t.right is BTree.empty t.root largest(t.right) 5 3 1 7 9 11 5 3 1 7 9 8 t.right is BTree.empty largest(t.left) t.right.is_leaf()
SLIDE 60
Discussion Questions
What's the largest element in a binary search tree?
8
def largest(t): if _________________________: return _________________ else: return _________________ What's the second largest element in a binary search tree? def second(t): if t.is_leaf(): return None elif _______________________: return t.root elif _______________________: return _________________ else: return _________________ t.right is BTree.empty t.root largest(t.right) 5 3 1 7 9 11 5 3 1 7 9 8 t.right is BTree.empty largest(t.left) t.right.is_leaf() second(t.right)
SLIDE 61
Sets as Binary Search Trees
SLIDE 62
Membership in Binary Search Trees
10
SLIDE 63
Membership in Binary Search Trees
contains traverses the tree
10
SLIDE 64
Membership in Binary Search Trees
contains traverses the tree
- If the element is not the root, it can only be in either the left or right branch
10
SLIDE 65
Membership in Binary Search Trees
contains traverses the tree
- If the element is not the root, it can only be in either the left or right branch
- By focusing on one branch, we reduce the set by the size of the other branch
10
SLIDE 66
Membership in Binary Search Trees
contains traverses the tree
- If the element is not the root, it can only be in either the left or right branch
- By focusing on one branch, we reduce the set by the size of the other branch
5 3 1 7 9 11
10
SLIDE 67
Membership in Binary Search Trees
contains traverses the tree
- If the element is not the root, it can only be in either the left or right branch
- By focusing on one branch, we reduce the set by the size of the other branch
5 3 1 7 9 11 9
10
SLIDE 68
Membership in Binary Search Trees
contains traverses the tree
- If the element is not the root, it can only be in either the left or right branch
- By focusing on one branch, we reduce the set by the size of the other branch
5 3 1 7 9 11 def contains(s, v): 9
10
SLIDE 69
Membership in Binary Search Trees
contains traverses the tree
- If the element is not the root, it can only be in either the left or right branch
- By focusing on one branch, we reduce the set by the size of the other branch
5 3 1 7 9 11 def contains(s, v): if s is BTree.empty: return False 9
10
SLIDE 70
Membership in Binary Search Trees
contains traverses the tree
- If the element is not the root, it can only be in either the left or right branch
- By focusing on one branch, we reduce the set by the size of the other branch
5 3 1 7 9 11 def contains(s, v): if s is BTree.empty: return False elif s.root == v: return True 9
10
SLIDE 71
Membership in Binary Search Trees
contains traverses the tree
- If the element is not the root, it can only be in either the left or right branch
- By focusing on one branch, we reduce the set by the size of the other branch
5 3 1 7 9 11 def contains(s, v): if s is BTree.empty: return False elif s.root == v: return True elif s.root < v: return contains(s.right, v) 9
10
SLIDE 72
Membership in Binary Search Trees
contains traverses the tree
- If the element is not the root, it can only be in either the left or right branch
- By focusing on one branch, we reduce the set by the size of the other branch
5 3 1 7 9 11 def contains(s, v): if s is BTree.empty: return False elif s.root == v: return True elif s.root < v: return contains(s.right, v) 9 If 9 is in the set, it is in this branch
10
SLIDE 73
Membership in Binary Search Trees
contains traverses the tree
- If the element is not the root, it can only be in either the left or right branch
- By focusing on one branch, we reduce the set by the size of the other branch
5 3 1 7 9 11 def contains(s, v): if s is BTree.empty: return False elif s.root == v: return True elif s.root < v: return contains(s.right, v) elif s.root > v: return contains(s.left, v) 9 If 9 is in the set, it is in this branch
10
SLIDE 74
Membership in Binary Search Trees
contains traverses the tree
- If the element is not the root, it can only be in either the left or right branch
- By focusing on one branch, we reduce the set by the size of the other branch
5 3 1 7 9 11 def contains(s, v): if s is BTree.empty: return False elif s.root == v: return True elif s.root < v: return contains(s.right, v) elif s.root > v: return contains(s.left, v) 9 If 9 is in the set, it is in this branch Order of growth?
10
SLIDE 75
Membership in Binary Search Trees
contains traverses the tree
- If the element is not the root, it can only be in either the left or right branch
- By focusing on one branch, we reduce the set by the size of the other branch
5 3 1 7 9 11 def contains(s, v): if s is BTree.empty: return False elif s.root == v: return True elif s.root < v: return contains(s.right, v) elif s.root > v: return contains(s.left, v) 9 If 9 is in the set, it is in this branch Order of growth?
10
- n average
Θ(h)
SLIDE 76
Membership in Binary Search Trees
contains traverses the tree
- If the element is not the root, it can only be in either the left or right branch
- By focusing on one branch, we reduce the set by the size of the other branch
5 3 1 7 9 11 def contains(s, v): if s is BTree.empty: return False elif s.root == v: return True elif s.root < v: return contains(s.right, v) elif s.root > v: return contains(s.left, v) 9 If 9 is in the set, it is in this branch Order of growth?
10
- n average
Θ(h) Θ(log n) on average for a balanced tree
SLIDE 77
Adjoining to a Tree Set
11
SLIDE 78
Adjoining to a Tree Set
5 3 1 7 9 11 8
11
SLIDE 79
Adjoining to a Tree Set
5 3 1 7 9 11 8 Right!
11
SLIDE 80
Adjoining to a Tree Set
5 3 1 7 9 11 8 Right!
11
SLIDE 81
Adjoining to a Tree Set
5 3 1 7 9 11 8 7 9 11 8 Right!
11
SLIDE 82
Adjoining to a Tree Set
5 3 1 7 9 11 8 7 9 11 8 Right! Left!
11
SLIDE 83
Adjoining to a Tree Set
5 3 1 7 9 11 8 7 9 11 8 7 8 Right! Left!
11
SLIDE 84
Adjoining to a Tree Set
5 3 1 7 9 11 8 7 9 11 8 7 8 Right! Left! E E
11
SLIDE 85
Adjoining to a Tree Set
5 3 1 7 9 11 8 7 9 11 8 7 8 Right! Left! Right! E E
11
SLIDE 86
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
11
SLIDE 87
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!
11
SLIDE 88
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!
11
SLIDE 89
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
11
SLIDE 90
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 7 8
11
SLIDE 91
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 7 8 7 9 11 8
11
SLIDE 92
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 7 8 7 9 11 8 5 3 1 7 9 11 8
11
SLIDE 93
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 7 8 7 9 11 8 5 3 1 7 9 11 8 (Demo)
11