Lab. Sheet Seven: Data Structure Laboratory

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Data Structure

LABORATORY

Lab. Sheet seven

Hash Table

Computer engineering department


Second class
Hash table
Hash table (or hash map) is one of the possible implementations of dictionary
ADT. Hence, it maps unique keys to associated values. In the view of
implementation, hash table is an array-based data structure, which uses hash
function to convert the key into the index of an array element, where associated
value is to be sought.

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;

const int TABLE_SIZE = 128;

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;
}

int get(int key) {


int hash = (key % TABLE_SIZE);
while (table[hash] != NULL && table[hash]->getKey() != key)
hash = (hash + 1) % TABLE_SIZE;
if (table[hash] == NULL)
return -1;
else
return table[hash]->getValue();
}
void put(int key, int value) {
int hash = (key % TABLE_SIZE);
while (table[hash] != NULL && table[hash]->getKey() != key)
hash = (hash + 1) % TABLE_SIZE;
if (table[hash] != NULL)
delete table[hash];
table[hash] = new HashEntry(key, value);
}
void Remove(int key)
{
//program remove hash table entry function
}

~HashMap() {
for (int i = 0; i < TABLE_SIZE; i++)
if (table[i] != NULL)
delete table[i];
delete[] table;
}
};

int _tmain(int argc, _TCHAR* argv[])


{
HashMap hash;
int key, value;
int choice;
while (1)
{
cout<<"\n----------------------"<<endl;
cout<<"Operations on Hash Table"<<endl;
cout<<"\n----------------------"<<endl;
cout<<"1.Insert element into the table"<<endl;
cout<<"2.Search element from the key"<<endl;
cout<<"3.Delete element at a key"<<endl;
cout<<"4.Exit"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter element to be inserted: ";
cin>>value;
cout<<"Enter key at which element to be inserted: ";
cin>>key;
hash.put(key, value);
break;
case 2:
cout<<"Enter key of the element to be searched: ";
cin>>key;
if (hash.get(key) == -1)
{
cout<<"No element found at key "<<key<<endl;
continue;
}
else
{
cout<<"Element at key "<<key<<" : ";
cout<<hash.get(key)<<endl;
}
break;
case 3:
cout<<"Enter key of the element to be deleted: ";
cin>>key;
hash.Remove(key);
break;
case 4:
exit(1);
default:
cout<<"\nEnter correct option\n";
}
}
return 0;
}
Exercises:

1. Hash table Collision resolution by chaining.Chaining is a possible way to


resolve collisions. Each slot of the array contains a link to a singly-linked list
containing key-value pairs with the same hash. New key-value pairs are
added to the end of the list. Lookup algorithm searches through the list to
find matching key. Initially table slots contain nulls. List is being created,
when value with the certain hash is added for the first time.

Write a program to illustrat the above method.

2. in the previous exercise a problem occured when trying to delete an entry,


looking at the figure if we tried to delete Jack Williams, the entry Andrew
Wilson will be lost because the chain will be broke. Write a delete function
that can solve this problem.

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.

You might also like