48 Views

Download Presentation
##### 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. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -

**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**Today’s Topics**• TreeMap • HashMap**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)**TreeMap**An implementation of the Map (or “Dictionary”) interface that has guaranteed log(n) worst case.**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**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**(B)**(C) (D) What does this tree map look like after we put ("Leonard", "Wei")? (A)**(B)**(C) (D) What does this tree map look like after we put ("Paul", "Kube")? (A)**(B)**(C) (D) What does this tree map look like after we put ("Maria", "Clancy")? (A)**Hash Tables (HashMaps)**Implementing the Map interface with Hash Tables**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**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**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:**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**Closed Addressing with Linear Probing**• Where does “Annie” go if hashkey(“Annie”) = 3? • 0 • 1 • 2 • 3 • Other**Closed Addressing with Linear Probing**• Where does “Juan” go if hashkey(“Juan”) = 4? • 1 • 2 • 3 • 4 • Other**Closed Addressing with Linear Probing**• Where does “Julian” go if hashkey(“Julian”) = 3? • 1 • 2 • 3 • 4 • Other**Closed Addressing with Linear Probing**• Where does “Solange” go if hashkey(“Solange”) = 5? • 3 • 4 • 5 • 6 • Other