Professional Documents
Culture Documents
Lab. Sheet Seven: Data Structure Laboratory
Lab. Sheet Seven: Data Structure Laboratory
Lab. Sheet Seven: Data Structure Laboratory
LABORATORY
Hash Table
Hash function
Hash function is very important part of hash table design. Hash function is
considered good, if it provides uniform distribution of hash values. Other hash
function's properties, required for quality hashing will be examined in detail later.
The reason, why hash function is a subject to the principal concern, is that poor
hash functions because collisions and some other unwanted effects, which badly
affect hash table overall performance.
Collisions
What happens, if hash function returns the same hash value for different keys? It
yields an effect, called collision. A popular collision-handling algorithms are
1. Open addressing
2. Chaining
3. Bucket addressing
Example
Hash function implemntation using Division Method , and collisions are resolved
using linear probing (open addressing strategy).
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class HashEntry {
private:
int key;
int value;
public:
HashEntry(int key, int value) {
this->key = key;
this->value = value;
}
int getKey() {
return key;
}
int getValue() {
return value;
}
};
class HashMap {
private:
HashEntry **table;
public:
HashMap() {
table = new HashEntry*[TABLE_SIZE];
for (int i = 0; i < TABLE_SIZE; i++)
table[i] = NULL;
}
~HashMap() {
for (int i = 0; i < TABLE_SIZE; i++)
if (table[i] != NULL)
delete table[i];
delete[] table;
}
};
3. For a small company. It use a list with 100 index to store the information of
its employees, Each of the employees has an ID number, first name, last
name, year of birth, job title and salary.
write a program to create hash table to store the information of (15)
employees. The class should have (insert, delete , search and display all)
functions.