Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
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 1. 2. TreeMap HashMap Review: the Map Interface 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) ValueType get(KeyType k) ValueType delete(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 goto 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 What does this tree map look like after we put ("Leonard", "Wei")? (A) (C) (B) (D) What does this tree map look like after we put ("Paul", "Kube")? (B) (A) (C) (D) What does this tree map look like after we put ("Maria", "Clancy")? (A) (C) (B) (D) 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 work: function is what at makes this all 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? A. 0 B. 1 C. 2 D. 3 E. Other Array index 0 1 2 3 4 5 6 7 8 Value Closed Addressing with Linear Probing A. B. C. D. E. Where does “Juan” go if hashkey(“Juan”) = 4? 1 2 3 4 Other Array index Value 0 1 2 3 4 5 6 7 8 Annie Closed Addressing with Linear Probing Where does “Julian” go if hashkey(“Julian”) = 3? A. 1 B. 2 C. 3 D. 4 E. Other Array index Value 0 1 2 3 Annie 4 Juan 5 6 7 8 Closed Addressing with Linear Probing Where does “Solange” go if hashkey(“Solange ”) = 5? A. 3 B. 4 C. 5 D. 6 E. Other Array index Value 0 1 2 3 Annie 4 Juan 5 Julian 6 7 8