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 tree is a tree that has class BTree(Tree): a left branch and a right branch 3 1 7 5 9 11 4
Binary Tree Class A binary tree is a tree that has class BTree(Tree): a left branch and a right branch Idea : Fill the place of a missing left branch with an empty tree 3 1 7 5 9 11 4
Binary Tree Class A binary tree is a tree that has class BTree(Tree): a left branch and a right branch Idea : Fill the place of a missing left branch with an empty tree 3 1 7 5 9 E 11 4
Binary Tree Class A binary tree is a tree that has class BTree(Tree): a left branch and a right branch Idea : Fill the place of a missing left branch with an empty tree 3 E: An empty tree 1 7 5 9 E 11 4
Binary Tree Class A binary tree is a tree that has class BTree(Tree): a left branch and a right branch empty = Tree(None) Idea : Fill the place of a missing left branch with an empty tree 3 E: An empty tree 1 7 5 9 E 11 4
Binary Tree Class A binary tree is a tree that has class BTree(Tree): a left branch and a right branch empty = Tree(None) Idea : Fill the place of a missing left branch with an empty tree Idea 2 : An instance of BTree always has exactly two branches 3 E: An empty tree 1 7 5 9 E 11 4
Binary Tree Class A binary tree is a tree that has class BTree(Tree): a left branch and a right branch empty = Tree(None) Idea : Fill the place of a missing left branch with an empty tree Idea 2 : An instance of BTree always has exactly two branches 3 E: An empty tree 1 7 E E 5 9 E E E 11 E E 4
Binary Tree Class A binary tree is a tree that has class BTree(Tree): a left branch and a right branch empty = Tree(None) Idea : Fill the place of a missing def __init__(self, root, left=empty, right=empty): left branch with an empty tree Tree.__init__(self, root, [left, right]) Idea 2 : An instance of BTree always has exactly two branches 3 E: An empty tree 1 7 E E 5 9 E E E 11 E E 4
Binary Tree Class A binary tree is a tree that has class BTree(Tree): a left branch and a right branch empty = Tree(None) Idea : Fill the place of a missing def __init__(self, root, left=empty, right=empty): left branch with an empty tree Tree.__init__(self, root, [left, right]) Idea 2 : An instance of BTree @property always has exactly two branches def left(self): return self.branches[0] 3 E: An empty tree 1 7 E E 5 9 E E E 11 E E 4
Binary Tree Class A binary tree is a tree that has class BTree(Tree): a left branch and a right branch empty = Tree(None) Idea : Fill the place of a missing def __init__(self, root, left=empty, right=empty): left branch with an empty tree Tree.__init__(self, root, [left, right]) Idea 2 : An instance of BTree @property always has exactly two branches def left(self): return self.branches[0] 3 E: An empty tree @property def right(self): 1 7 return self.branches[1] E E 5 9 E E E 11 E E 4
Binary Tree Class A binary tree is a tree that has class BTree(Tree): a left branch and a right branch empty = Tree(None) Idea : Fill the place of a missing def __init__(self, root, left=empty, right=empty): left branch with an empty tree Tree.__init__(self, root, [left, right]) Idea 2 : An instance of BTree @property always has exactly two branches def left(self): return self.branches[0] 3 E: An empty tree @property def right(self): 1 7 return self.branches[1] E E 5 9 t = BTree(3, BTree(1), BTree(7, BTree(5), E E E BTree(9, BTree.empty, 11 BTree(11)))) E E 4
Binary Tree Class A binary tree is a tree that has class BTree(Tree): a left branch and a right branch empty = Tree(None) Idea : Fill the place of a missing def __init__(self, root, left=empty, right=empty): left branch with an empty tree Tree.__init__(self, root, [left, right]) Idea 2 : An instance of BTree @property always has exactly two branches def left(self): return self.branches[0] 3 E: An empty tree @property def right(self): 1 7 return self.branches[1] E E 5 9 t = BTree(3, BTree(1), BTree(7, BTree(5), E E E BTree(9, BTree.empty, 11 BTree(11)))) E E (Demo) 4
Binary Search Trees
Binary Search A strategy for finding a value in a sorted list: check the middle and eliminate half 6
Binary Search A strategy for finding a value in a sorted list: check the middle and eliminate half 20 in [1, 2, 4, 8, 16, 32, 64] 6
Binary Search A strategy for finding a value in a sorted list: check the middle and eliminate half 20 in [1, 2, 4, 8, 16, 32, 64] 6
Binary Search A strategy for finding a value in a sorted list: check the middle and eliminate half 20 in [1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] 6
Binary Search A strategy for finding a value in a sorted list: check the middle and eliminate half 20 in [1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] 6
Binary Search A strategy for finding a value in a sorted list: check the middle and eliminate half 20 in [1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] 6
Binary Search A strategy for finding a value in a sorted list: check the middle and eliminate half 20 in [1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] 6
Binary Search A strategy for finding a value in a sorted list: check the middle and eliminate half 20 in [1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] False 6
Binary Search A strategy for finding a value in a sorted list: check the middle and eliminate half 20 in [1, 2, 4, 8, 16, 32, 64] 4 in [1, 2, 4, 8, 16, 32] [1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] False 6
Binary Search A strategy for finding a value in a sorted list: check the middle and eliminate half 20 in [1, 2, 4, 8, 16, 32, 64] 4 in [1, 2, 4, 8, 16, 32] [1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] False 6
Binary Search A strategy for finding a value in a sorted list: check the middle and eliminate half 20 in [1, 2, 4, 8, 16, 32, 64] 4 in [1, 2, 4, 8, 16, 32] [1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32] [1, 2, 4, 8, 16, 32, 64] False 6
Binary Search A strategy for finding a value in a sorted list: check the middle and eliminate half 20 in [1, 2, 4, 8, 16, 32, 64] 4 in [1, 2, 4, 8, 16, 32] [1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32] [1, 2, 4, 8, 16, 32, 64] False 6
Binary Search A strategy for finding a value in a sorted list: check the middle and eliminate half 20 in [1, 2, 4, 8, 16, 32, 64] 4 in [1, 2, 4, 8, 16, 32] [1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32] [1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] False 6
Binary Search A strategy for finding a value in a sorted list: check the middle and eliminate half 20 in [1, 2, 4, 8, 16, 32, 64] 4 in [1, 2, 4, 8, 16, 32] [1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32] [1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] False 6
Binary Search A strategy for finding a value in a sorted list: check the middle and eliminate half 20 in [1, 2, 4, 8, 16, 32, 64] 4 in [1, 2, 4, 8, 16, 32] [1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32] [1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] False True 6
Binary Search A strategy for finding a value in a sorted list: check the middle and eliminate half 20 in [1, 2, 4, 8, 16, 32, 64] 4 in [1, 2, 4, 8, 16, 32] [1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32] [1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] False True For a sorted list of length n, what Theta expression describes the time required? 6
Binary Search A strategy for finding a value in a sorted list: check the middle and eliminate half 20 in [1, 2, 4, 8, 16, 32, 64] 4 in [1, 2, 4, 8, 16, 32] [1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32] [1, 2, 4, 8, 16, 32, 64] [1, 2, 4, 8, 16, 32, 64] False True For a sorted list of length n, what Theta expression describes the time required? Θ (log n ) 6
Binary Search Trees 7
Binary Search Trees A binary search tree is a binary tree where each root value is: 7
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
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
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 9 1 5 11 7
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 3 9 1 7 1 5 11 5 9 11 7
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 5 3 9 3 9 1 7 1 5 11 5 9 1 7 11 11 7
Recommend
More recommend