61a lecture 21 announcements binary trees binary tree
play

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


  1. 61A Lecture 21

  2. Announcements

  3. Binary Trees

  4. Binary Tree Class 4

  5. Binary Tree Class class BTree(Tree): 4

  6. Binary Tree Class A binary tree is a tree that has class BTree(Tree): a left branch and a right branch 4

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. Binary Search Trees

  20. Binary Search A strategy for finding a value in a sorted list: check the middle and eliminate half 6

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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

  28. 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

  29. 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

  30. 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

  31. 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

  32. 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

  33. 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

  34. 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

  35. 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

  36. 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

  37. Binary Search Trees 7

  38. Binary Search Trees A binary search tree is a binary tree where each root value is: 7

  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

  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

  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 9 1 5 11 7

  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 3 9 1 7 1 5 11 5 9 11 7

  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 5 3 9 3 9 1 7 1 5 11 5 9 1 7 11 11 7

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend