Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 212 – Data Structures Lecture 26: Hash Tables.

Similar presentations


Presentation on theme: "CSC 212 – Data Structures Lecture 26: Hash Tables."— Presentation transcript:

1

2 CSC 212 – Data Structures Lecture 26: Hash Tables

3 Question of the Day Two English words change their pronounciation when their first letter is capitalized. What are they? Polish/polish Reading/reading

4 Entry Interface ADT representing search data  Each Entry is key-value pair  key is what we have and use…  … but we actually want the value public interface Entry { public K key(); public V value(); }

5 Map ADT Represents searchable Collections  Data items are entries (key-value pairs)  Instances map keys to values Keys contained in at most one entry  So each key mapped to at most one value Values may be in multiple entries  So many keys refer to same value Basis of searching data structures

6 Map Interface public interface Map extends Collection { public V put(K key, V val) throws InvalidKeyException; public V get(K key) throws InvalidKeyException; public V remove(K key) throws InvalidKeyException; public Iterable keys(); public Iterable values(); public Iterable > entries(); }

7 PositionList-Based Implementation PositionList holds entries in any order Independent of PositionList’s implementation  Relies on methods defined by Interface Positions Entrys 9 c 6 c 5 c 8 c PositionList Map

8 Map Performance Want simple & fast implementation  Google: Search speed measured in TB/s  List-based Map: get, remove, put takes O(n) time Would love to use arrays  Implementation is easy  Insertion, access, and removal in O(1) time  But ranks or array indices are ints, not K

9 Hashing To The Rescue For each key, hash function computes integer from 0 to N - 1  For example, h(x) = x mod N  Value h(x) is “hash value” of x Hash table stores all the entries  (Nothing to do with eateries in Amsterdam)  Really just an array of size N Goal is storing entry (k, v) at index h(k)  1 st (good) implementation of a Map

10 Hash Table Example Stores instances of Entry Array has 10,000 indices Hash function is h(x)  x mod 10,000 What if execute call: put(212710001, “Ike Oh”);     0 1 2 3 4 9997 9998 9999 … 4512290004 | “Jill Roe” 9811010002 | “Bob Dole” 2007519998 | “Rhi Smith” 0256120001 | “Jay Doe”

11 Collisions Name when keys hash to same index Ideal hash spreads out equally and evenly  Limit/avoid collisions  But also want to keep table small But good hash hard to find  Depends on what you have to work with  Even harder to make a good hash Could try to work with collisions

12 Bucket Arrays Each item in array is itself a List “Chain” whenever there is a collision  Nothing to do with road rage  Instead, just add new Entry onto List

13 Bucket Arrays But what if have really bad hash?  Suppose always hash to same index All entries now in single List  Back to O(n) execution times  (Also get bad case of the munchies)

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


Download ppt "CSC 212 – Data Structures Lecture 26: Hash Tables."

Similar presentations


Ads by Google