1 / 19

CSE 12 – Basic Data Structures

Télécharger la présentation

CSE 12 – Basic 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. CS2 in Java Peer Instruction Materials by Cynthia Lee is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.Based on a work at http://peerinstruction4cs.org.Permissions beyond the scope of this license may be available at http://peerinstruction4cs.org. CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12

  2. Today’s Topics • TreeMap • HashMap

  3. Review: the Map Interface

  4. Map Interface • Want to hold (key, value) associations • Want to be able to “look up” values quickly using the key • “Siri, what is Diego’s phone number?” • key = “Diego”, value = 858-555-0176 • Basic operations of this ADT are something like this: • void put(KeyType k, ValueType v) • ValueTypeget(KeyType k) • ValueTypedelete(KeyType k)

  5. TreeMap An implementation of the Map (or “Dictionary”) interface that has guaranteed log(n) worst case.

  6. Implementing Map interface with a Binary Search Tree (BST) • Usually we think of a hash table as the go-to implementation of the Map interface • But Binary Search Trees are another option • C++’s Standard Template Library (STL) uses a Red-Black tree for their Map • STL in C++ is like Collections in Java

  7. Implementing Map interface with a Binary Search Tree (BST) • The next few slides will explore your intuitions (guesses) about how to place (key, value) pairs into a Binary Search Tree in a way that makes sense for implementing Map • The examples list (key, value) pairs in parentheses like this: (“Mike”, “Clancy”) • So this is a dictionary that lets you find somebody’s last name if all you know is their first name

  8. (B) (C) (D) What does this tree map look like after we put ("Leonard", "Wei")? (A)

  9. (B) (C) (D) What does this tree map look like after we put ("Paul", "Kube")? (A)

  10. (B) (C) (D) What does this tree map look like after we put ("Maria", "Clancy")? (A)

  11. Hash Tables (HashMaps) Implementing the Map interface with Hash Tables

  12. Imagine you want to look up your neighbors’ names, based on their house number • House numbers: 2555 through 10567 (roughly 4000 houses) • Names: one last name per house

  13. Array vs Tree • You could store them in a balanced TreeMap of some kind • Log(n) to do get, put, delete • Or you could store them in an array • Array is really fast lookup! O(1) • Just look in myarray[housenumber] to get the name

  14. Hash Table is just a modified, more flexible array • Keys don’t have to be integers 0-(size-1) • (Ideally) avoids big gaps like our gap from 0 to 2555 in the house numbers • Hash function is what at makes this all work:

  15. Hash key collisions • Hash function takes key and maps it to an integer • Sometimes will map two DIFFERENT keys to the same integer • “Collision” • We can NOT overwrite the value the way we would if it really were the same key • Need a way of storing multiple values in a given “place” in the hash table

  16. Closed Addressing with Linear Probing • Where does “Annie” go if hashkey(“Annie”) = 3? • 0 • 1 • 2 • 3 • Other

  17. Closed Addressing with Linear Probing • Where does “Juan” go if hashkey(“Juan”) = 4? • 1 • 2 • 3 • 4 • Other

  18. Closed Addressing with Linear Probing • Where does “Julian” go if hashkey(“Julian”) = 3? • 1 • 2 • 3 • 4 • Other

  19. Closed Addressing with Linear Probing • Where does “Solange” go if hashkey(“Solange”) = 5? • 3 • 4 • 5 • 6 • Other

More Related