Download presentation
Presentation is loading. Please wait.
Published byDaniella Sherman Modified over 9 years ago
1
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 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.Cynthia LeeCreative Commons Attribution-NonCommercial 4.0 International Licensehttp://peerinstruction4cs.org
2
Today’s Topics 1. TreeMap 2. HashMap 2
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) ValueType get(KeyType k) ValueType delete(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
What does this tree map look like after we put ("Leonard", "Wei")? (A)(B) (C)(D)
9
What does this tree map look like after we put ("Paul", "Kube")? (A) (B) (C)(D)
10
What does this tree map look like after we put ("Maria", "Clancy")? (A)(B) (C)(D)
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 Array indexValue 0 1 2 3 4 5 6 7 8 Where does “Annie” go if hashkey(“Annie”) = 3? A. 0 B. 1 C. 2 D. 3 E. Other
17
Closed Addressing with Linear Probing Array indexValue 0 1 2 3Annie 4 5 6 7 8 Where does “Juan” go if hashkey(“Juan”) = 4? A. 1 B. 2 C. 3 D. 4 E. Other
18
Closed Addressing with Linear Probing Array indexValue 0 1 2 3Annie 4Juan 5 6 7 8 Where does “Julian” go if hashkey(“Julian”) = 3? A. 1 B. 2 C. 3 D. 4 E. Other
19
Closed Addressing with Linear Probing Array indexValue 0 1 2 3Annie 4Juan 5Julian 6 7 8 Where does “Solange” go if hashkey(“Solange ”) = 5? A. 3 B. 4 C. 5 D. 6 E. Other
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.