Lab 7 - Hash Table
Lab 7 - Hash Table
Lab 7
Hash Table
In this lab, we will implement a data structure that can store and query data quickly: Hash table.
1 Hash Table
Students are required to implement a hash table with 5 different collision handling: Linear Probing,
Quadratic Probing, Chaining using Linked List, Chaining using AVL Tree and Double Hashing.
• vector<hashNode*> table: This is an array containing the data for the hash table, where
each item is a hashNode storing a key-value pair. For example:
1 struct hashNode
2 {
3 string key ;
4 int value ;
5 }
and functions:
• void init(unsigned int hashSize): initialize an empty hash table with the given size.
• void release(): free all dynamically allocated memory in the hash table.
• unsigned int hashFunctions(...): hash functions to compute the index for a given key.
• void add(string key, int value): add element. If the key existed, update the old value.
• int* searchValue(string key): search element in the table. If not existed, return NULL.
10 int capacity ;
11 vector < hashNode * > table ;
12
13 // Functions ( Methods )
14 void init ( unsigned int hashSize ) {...}
15 void release () {...}
16 unsigned int hashFunction ( string key ) {...}
17 void add ( string key , int value ) {...}
18 int * searchValue ( string key ) {...}
19 void removeKey ( string key ) {...}
20 }
To hash a string in hashFunction(string key), you can use polynomial rolling hash function.
The formula is:
n−1
!
X
hash(s) = (s[i] × pi ) mod m (1)
i=0
which:
• p = 31.
• m = 109 + 9.
In main.cpp, try using hash table. You can initialize the hash table with a small size, then perform
add, search, and remove operations on it before releasing the memory and ending the program.
No menu is required.
Also, you will need to implement some additional functions to work with linked lists.
Also, you will need to implement some additional functions to work with AVL Trees.
2 Submission
Your source code must be contributed in the form of a compressed file and named your submission
according to the format StudentID.zip. Here is a detail of the directory organization:
StudentID
Exercise 1.cpp
Exercise 2.cpp
Exercise 3.cpp
Exercise 4.cpp
Exercise 5.cpp
The end.