1 / 152

Search Trees: BSTs and B-Trees

Search Trees: BSTs and B-Trees. David Kauchak cs161 Summer 2009. Administrative. Midterm SCPD contacts Review session: Friday, 7/17 2:15-4:05pm in Skilling Auditorium Practice midterm Homework late/grading policy HW 2 solution HW 3 Feedback. Number guessing game.

jariah
Download Presentation

Search Trees: BSTs and B-Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Search Trees: BSTs and B-Trees David Kauchak cs161 Summer 2009

  2. Administrative • Midterm • SCPD contacts • Review session: Friday, 7/17 2:15-4:05pm in Skilling Auditorium • Practice midterm • Homework late/grading policy • HW 2 solution • HW 3 • Feedback

  3. Number guessing game • I’m thinking of a number between 1 and n • You are trying to guess the answer • For each guess, I’ll tell you “correct”, “higher” or “lower” • Describe an algorithm that minimizes the number of guesses

  4. Binary Search Trees • BST – A binary tree where a parent’s value is greater than its left child and less than or equal to it’s right child • Why not? • Can be implemented with with pointers or an array

  5. Example 12 8 14 9 20 5

  6. What else can we say? • All elements to the left of a node are less than the node • All elements to the right of a node are greater than or equal to the node • The smallest element is the left-most element • The largest element is the right-most element 12 8 14 9 20 5

  7. Another example: the loner 12

  8. Another example: the twig 12 8 5 1

  9. Operations • Search(T,k) – Does value k exist in tree T • Insert(T,k) – Insert value k into tree T • Delete(T,x) – Delete node x from tree T • Minimum(T) – What is the smallest value in the tree? • Maximum(T) – What is the largest value in the tree? • Successor(T,x) – What is the next element in sorted order after x • Predecessor(T,x) – What is the previous element in sorted order of x • Median(T) – return the median of the values in tree T

  10. Search • How do we find an element?

  11. Finding an element Search(T, 9) 12 8 14 9 20 5

  12. Finding an element Search(T, 9) 12 8 14 9 20 5

  13. Finding an element Search(T, 9) 12 8 14 9 20 5 9 > 12?

  14. Finding an element Search(T, 9) 12 8 14 9 20 5

  15. Finding an element Search(T, 9) 12 8 14 9 20 5

  16. Finding an element Search(T, 13) 12 8 14 9 20 5

  17. Finding an element Search(T, 13) 12 8 14 9 20 5

  18. Finding an element Search(T, 13) 12 8 14 9 20 5

  19. Finding an element Search(T, 13) 12 8 14 9 20 5 ?

  20. Iterative search

  21. Is BSTSearch correct?

  22. Running time of BST • Worst case? • O(height of the tree) • Average case? • O(height of the tree) • Best case? • O(1)

  23. Height of the tree • Worst case height? • n-1 • “the twig” • Best case height? • floor(log2n) • complete (or near complete) binary tree • Average case height? • Depends on two things: • the data • how we build the tree!

  24. Insertion

  25. Insertion Similar to search

  26. Insertion Similar to search Find the correct location in the tree

  27. Insertion keeps track of the previous node we visited so when we fall off the tree, we know

  28. Insertion add node onto the bottom of the tree

  29. Correctness? maintain BST property

  30. Correctness What happens if it is a duplicate?

  31. Inserting duplicate Insert(T, 14) 12 8 14 9 20 5

  32. Running time O(height of the tree)

  33. Running time O(height of the tree) Why not Θ(height of the tree)?

  34. Running time Insert(T, 15) 12 8 5 1

  35. Height of the tree • Worst case: “the twig” – When will this happen?

  36. Height of the tree • Best case: “complete” – When will this happen?

  37. Height of the tree • Average case for random data? Randomly inserted data into a BST generates a tree on average that is O(log n)

  38. Visiting all nodes • In sorted order 12 8 14 9 20 5

  39. Visiting all nodes 5 • In sorted order 12 8 14 9 20 5

  40. Visiting all nodes 5, 8 • In sorted order 12 8 14 9 20 5

  41. Visiting all nodes 5, 8, 9 • In sorted order 12 8 14 9 20 5

  42. Visiting all nodes 5, 8, 9, 12 • In sorted order 12 8 14 9 20 5

  43. Visiting all nodes 5, 8, 9, 12 • What’s happening? 12 8 14 9 20 5

  44. Visiting all nodes 5, 8, 9, 12, 14 • In sorted order 12 8 14 9 20 5

  45. Visiting all nodes 5, 8, 9, 12, 14, 20 • In sorted order 12 8 14 9 20 5

  46. Visiting all nodes in order

  47. Visiting all nodes in order any operation

  48. Is it correct? • Does it print out all of the nodes in sorted order?

  49. Running time? • Recurrence relation: • j nodes in the left subtree • n – j – 1 in the right subtree • Or • How much work is done for each call? • How many calls? • Θ(n)

  50. What about?

More Related