Download presentation
Presentation is loading. Please wait.
Published byMorgan Matthews Modified over 8 years ago
1
CSC 212 – Data Structures Lecture 28: More Hash and Dictionaries
2
Question of the Day What do you get when you cross a mountain climber and a grape? Nothing, you cannot cross a scalar.
3
Hash Table Used to implement Map Must accept a key and return a value Tries for O (1) time by using arrays… … but may end up needing O (n) time Do not know Keys type ahead of time Flexible design must handle anything Must consider what we want from hash
4
Hashing Want use array to hold our table Indices provide O (1) access times But array index must be an int With int Keys, could just use array from start Not flexible at all So, hash function serves three purposes: Convert Key to int in repeatable manner Limit range of int to size of the array Spread entries out evenly across the range
5
Hash Function First thing hash must do: turn Key into int Easy for numeric data types For String, add value of each character Makes “spot”, “pots”, “stop”, & “opts” equal Improve upon this using polynomial like x 0 x 1 a x 2 a 2 … x n 1 a n 1 would compute ‘ s ’ ‘ p ’* z ‘ o ’* z 2 ‘ t ’* z 3 Basically, has to be specific to type of Key
6
Compression Need to limit table size Unused entries waste space and do not help When z =33, “spot” = 4,293,383 “triskaidekaphobia” = too big for my calculator To be useful, hash must be compressible Easiest compression uses modulus (%) Using prime number exploits splits regularity MAD method first does multiply and add Can help spread hash more evenly
7
Collisions Two Keys could always share hash code Array can only hold one value per entry Need to some way of working around this Three commonly used handling schemes Separate chaining: array contains Lists of entries hashed to that index Linear probing: loop through array looking for first open array location Double hashing: use more hash to find an empty array location
8
Separate Chaining Table is an array of List “Chain” whenever there is a collision Each entry at index in which it is hashed Search List to see if a there is a matching Key 0 1 2 3 4 4512290004 | “Jane”9811010004 | “Joe” 0256120001| “Bob”
9
Linear Probing Table is array of Entrys Normally, Entry located where key is hashed If index used, circle through array for first empty one Collisions cause pockets of filled slots, slowing access Collisions means key could be in any location But either fill removed slots or mark specially Only search until first empty specially marked slot 41 18445932223173 0123456789101112
10
Double Hashing Solution to bad hash is more hash! Table is again array of Entrys When there is a collision, re-hash key using second hash function Re-use second hash value to probe on repeated collisions Re-multiplier and table size should be relatively prime, so entire table will be scanned
11
Store integers using double hashing N 13 h(k) k mod 13 d(k) 7 k mod 7 Insert keys: 18, 41, 22, 44, 59, 32, 31 Example of Double Hashing 0123456789101112
12
Dictionary ADT dic·tion·ar·y 1. Reference book containing alphabetical list of words, with information given for each word 2. Book listing words with translations into other language Dictionary ADT maps a key to 1 or more values Used by Google, computer security logs, databases Like a Map, Dictionary works with entries But Dictionary removes an Entry and adds findAll() to return Iterator over entries with given key
13
Implementing a Dictionary Dictionary similar to a Map Can use List or hash table Implementation is virtually identical except: Allow multiple entries with same key When searching in remove, must match key and value Add findAll() to return instance of class implementing Iterator Class could just have array to which you add entries with key matching search key
14
Your Turn Get back into groups and do activity
15
Before Next Lecture… Keep up with your reading! Complete Week #10 Assignment Review Programming Assignment #3
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.