Download presentation
Presentation is loading. Please wait.
Published byΕὔα Λαιμός Modified over 6 years ago
1
Hash table another data structure for implementing a map or a set
2
Quiz answer all question with true or false
A hash table stores the map elements in order of their key values A hash table makes use of an array or a vector Searching for an element in a hash table never requires more than 1 comparison
3
Quiz answer all question with true or false
A hash table stores the map elements in order of their key values false A hash table makes use of an array or a vector true Searching for an element in a hash table never requires more than 1 comparison false
4
What is a hash table? An array (or vector) with a fixed capacity (tsize) Add, find, retrieve and remove all start by applying a hash function to an item (or its key) Hash function uses the key to compute a value between 0 and tsize – 1 Hash(key) 0 .. Tsize - 1 Result is Known as the home address
5
A simple example Tsize is 7 Keys of items to be added are:
345, 617, 963, 712, 366 Hash function is: Key % tsize What is the home address of each item to be added?
6
1 2 3 4 5 6 345 % 7 2 617 % 7 1 963 % 7 4 712 % 7 5 366 % 7 a collision
7
How to deal with collisions?
Collisions are inevitable Home address becomes the starting point for finding Where to add a key-value pair Finding item with a given key Retrieving the value associated with a given key Finding key-value pair to be removed A hash table Needs a collision resolution strategy
8
Two kinds of hash tables
Open hash tables All items are stored in the array/vector at their home address (if open) or follow a probe sequence to find open spot Chained hash tables Items with the same home address are synonyms Synonyms are stored in a list starting at the home address For both all operations start by applying a hash function and then using the home address as the starting point for finding the item or where to store the item
9
an open hash table add the following items item home address 345 2
1 2 3 4 5 6 E E E E E E E
10
an open hash table Find: 712, 366, 49, 50 E F 617 F 345 F 366 F 963 F
1 2 3 4 5 6 E F 617 F 345 Find: 712, 366, 49, 50 F 366 F 963 F 712 E
11
an open hash table Remove: 345 E E F 617 F 617 F 345 R F 366 F 366 F
1 2 3 4 5 6 1 2 3 4 5 6 E E F 617 F 617 F 345 Remove: 345 R F 366 F 366 F 963 F 963 F 712 F 712 E E
12
a chained hash table add the following items item home address 345 2
1 2 3 4 5 6
13
a chained hash table Find: 712, 345, 49, 50 Remove: 345 617 366 345
1 2 3 4 5 6 617 366 345 Find: 712, 345, 49, 50 Remove: 345 963 712
14
Hash table performance
Depends on Load factor (n / tsize) must be < 1 for open hash tale Can be > 1 for chained hash table Quality of the hash function How uniformly are the home addresses produced distributed over the range 0 .. Tsize-1
15
Chained hash tables Hash function determines which synonym list to search With a good hash function synonym lists will be of nearly equal length What is the average synonym list length if we add n items to a hash table with a size of tsize? Given HT1 and ht2 Both have a load factor of 2 Ht1 holds 1000 items and ht2 holds 4000 items How does their expected performance compare? Time to find an item grows at the same rate as the load factor (not n) search for an item in a chained hash table is O(1) Search for an item in a closed hash table also increases at same rate as the load factor
16
Hash functions Simplest case
Item being hashed is an integer Return Item % tsize What if the item being hashed is a string? create an integer from the string Simplest way Sum the characters (each is stored as an asci value)
17
hash functions Key 0 .. tSize – 1
A good hash function uniformly distributes home addresses over the range 0 .. Tsize-1 What can be done to make a hash function good? Using a prime number for tsize helps reduce collisions Knowledge about keys can help devise a better hash function for a given set of key values Who uses (calls) the hash function? The program that makes use of a set/map? The implementer of the map? Who should write the hash function? The program that makes use of the map?
18
Map user writes Map implementer uses a hash function the hash function
Map Interface Map user writes a hash function Map implementer uses the hash function
19
How can the Map user pass the hash function to the map implementer?
a function (like a variable) has a type and a value The type of a function is its prototype (arguments and return type) The value of function is a pointer to a block of code to be executed Typedef int(*hashfunc) (keyType key, int tsize); Any function that has parameters of keytype and int and returns an int is of type *hashfunc User of the map writes a hash function and passes it to the map constructor which stores it as a data member
20
MAP IMPLEMENTATION typedef int(*hashFunc)(KeyType item, int tsize);
class Map{ public: Map(int size, hashFunc func); private: hashFunc hash; // the hash function };
21
MAP USER int hash(KeyType key, int tsize);
// input: size of the hash table and a key // output: returns an int between 0 and tsize - 1 int main () { Map myMap(size, hash); } int hash(KeyType key, int tsize){
22
Hash table performance
Two Key factors Load factor - n / tsize over .75 for open hash table likely to produce long probe sequences can be more than 1 for chained hash table Quality of the hash function How uniformly are the home addresses produced distributed over the range Tsize-1 Using a prime number for tsize helps reduce collisions Knowledge about keys can help devise a better hash function for a given set of key values
23
Choosing between a bst and a hash table for a map or set
What do we know about the number of items to be stored? Space needed for a bst is O(n) Space needed for a chained HT is O(n + tsize) Space for an open ht is O(tsize) What do we know about the items or keys to be compared? Need to be comparable for both Need a hash function for a ht Is it necessary to traverse elements in order of items or keys?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.