Download presentation
Presentation is loading. Please wait.
1
Hash Tables
2
Container of elements where each element has an associated key Each key is mapped to a value that determines the table cell where element should be placed Mapping is called the hash function What is a Hash Table?
3
Hash Table Operations size() isEmpty() find(k) – find element with key k insertItem(k, e) – insert element e with key k removeElement(k) – remove element with key k elements() – return Iterator of elements keys() – return Iterator of keys
4
Array Implementation element is name key is id number map id number to array index best to use prime number as table size [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] bob 1234 jane 1344 sally 1354
5
Why a Hash Table? Random access to items –Don’t have to perform search to find item Complexity of find/remove/insert?
6
Hash Function The hash function has to map the key to a value –Bad option: add up ASCII values of characters in key and % by number of table elements –Better option: h k = k 0 + 37k 1 + 37 2 k 2 for(i=0; i < key.length(); i++) hashVal= 37*hashVal + key[i] hashVal %= tableSize
7
Collisions It is still possible that two keys will map to the same value Collision resolution strategies –Separate chaining –Open addressing
8
Separate Chaining Keep a list of elements that map to same value Insertion algorithm Removal algorithm Could use another data structure instead of a linked list [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] 8040 62 43 3 57 5989
9
Load Factor Load factor – λ – is ratio of number of elements to the table size Average length of a list is λ –Average deletion time –Insertion time Ideally, λ ≈ 1
10
Open Addressing When collision occurs, try cells h 0 (x), h 1 (x), h 2 (x),… where h i (x) = (hash(x)+f(i) % TableSize) λ <.5 Insertion/find algorithm Lazy deletion algorithm
11
Linear Probing – f(i) = i del {89} ins {28} ins {39} find {69} Empty TableAfter 89After 18After 49After 58After 69 049 158 269 3 4 5 6 7 818 989
12
Quadratic Probing Linear probing can lead to primary clustering Quadratic probing – f(i) = i 2
13
Double Hashing When a collision occurs, apply a second hash function to x and probe at a distance hash 2 (x), 2hash 2 (x), …
14
Rehashing As table gets full, insertions may slow because of more collisions Rehash by creating table at least twice as large and reinserting all elements into new table –requires rehashing all keys (because of new table size) Running time? When should rehashing occur?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.