1 / 18

CS261 Data Structures

CS261 Data Structures. Binary Search Trees Concepts. Goals . Introduce the Binary Search Tree (BST) Conceptual implementation of Bag interface with the BST Performance of BST Bag operations. Binary Search Tree. Binary search trees are binary trees where every node’ s value is:

sunee
Download Presentation

CS261 Data Structures

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. CS261 Data Structures Binary Search Trees Concepts

  2. Goals • Introduce the Binary Search Tree (BST) • Conceptual implementation of Bag interface with the BST • Performance of BST Bag operations

  3. Binary Search Tree • Binary search trees are binary trees where every node’s value is: • Greater than all its descendents in the left subtree • Less than or equal to all its descendents in the right subtree • If tree is reasonably full (well balanced), searching for an element is O(log n). Why?

  4. Intuition 50 25 75 20 35 60 85 30 45 65 80

  5. Binary Search Tree: Example Alex Abner Angela Abigail Adela Alice Audrey Adam Agnes Allen Arthur

  6. BST Bag: Contains • Start at root • At each node, compare value to node value: • Return true if match • If value is less than node value, go to left child (and repeat) • If value is greater than node value, go to right child (and repeat) • If node is null, return false • Dividing in half each step as you traverse path from root to leaf (assuming reasonably full!!!)

  7. BST Bag: Contains/Find Example Alex Abner Agnes Angela Abigail Alice Audrey Adela Adam Agnes Allen Arthur Object to find

  8. BST Bag: Add Alex Abner Angela Abigail Adela Alice Audrey Adam Agnes Allen Arthur • Do the same type of traversal from root to leaf • When you find a null value, create a new node

  9. BST Bag: Add Example Alex Abner Aaron Angela Abigail Alice Audrey Adela Aaron Adam Agnes Allen Arthur Object to add “Aaron” should be added here Before first call to add

  10. BST Bag: Add Example Alex Abner Ariel Angela Abigail Alice Audrey Adela Aaron Adam Ariel Agnes Allen Arthur Next object to add “Ariel” should be added here After first call to add

  11. BST Bag: Remove Alex Abner Angela Abigail Adela Alice Audrey Adam Agnes Allen Arthur How would you remove Abigail? Audrey? Angela?

  12. Who fills the hole? • Answer: the leftmost child of the right subtree(smallest element in right subtree) • Try this on a few values • Alternatively: The rightmost child of the left subtree

  13. BST Bag: Remove Example Alex Abner Angela Abigail Alice Audrey Adela Adam Agnes Allen Arthur Element to remove Replace with: leftmost(rght) Before call to remove

  14. BST Bag: Remove Example Alex Abner Arthur Audrey Abigail Alice Adela Adam Agnes Allen After call to remove

  15. Special Case Angela Alice Audrey Allen Arthur • What if you don’t have a right child? • Try removing “Audrey” • Simply return left child

  16. Complexity Analysis (contains) • If reasonably full, you’re dividing in half at each step: O(log n) • Alternatively, we are running down a path from root to leaf • We can prove by induction that in a complete tree (which is reasonably full), the path from root to leaf is bounded by floor(log n), so O(log n)

  17. Binary Search Tree: Useful Collection? • We’ve shown all Bag operations to be proportional to the length of a path, rather than the number of elements in the tree • We’ve also said that in a reasonably full tree, this path is bounded by : floor( (log2n)) • This Bag is faster than our previous implementations!

  18. Comparison • Average Case Execution Times

More Related