0% found this document useful (0 votes)
28 views

Lab 7 - Hash Table

Uploaded by

lenhatthanh1004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Lab 7 - Hash Table

Uploaded by

lenhatthanh1004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab 7 Data structures and Algorithms CSC10004

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.

1.1 Linear Probing


Implement a struct to represent hash table using linear probing with the following variables:

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

• int capacity: size of the hash table.

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.

• void removeKey(string key): remove an element from the hash table.

And other variables, functions if necessary.

University of Science Faculty of Information Technology Page 1


Lab 7 Data structures and Algorithms CSC10004

Here is example code:


1 struct hashTable
2 {
3 // Variables ( Attributes )
4 struct hashNode
5 {
6 string key ;
7 int value ;
8 };
9

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:

• s: The key as a string of length n.

• s[i]: ASCII code of the character at position i from s.

• 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.

University of Science Faculty of Information Technology Page 2


Lab 7 Data structures and Algorithms CSC10004

1.2 Quadratic Probing


Implement a hash table using quadratic probing with the same variables and functions as in
Linear Probing.

1.3 Chaining using Linked List


Implement a hash table using chaining with the same variables and functions as in Linear Probing.
However, the hashNode will need to be modified a bit to implement a linked list, as shown below:
1 struct hashNode
2 {
3 string key ;
4 int value ;
5 hashNode * next ; // Add this line
6 };

Also, you will need to implement some additional functions to work with linked lists.

1.4 Chaining using AVL Tree


Implement a hash table using chaining with the same variables and functions as in Linear Probing.
However, the hashNode will need to be modified a bit to implement a AVL Tree, as shown below:
1 struct hashNode
2 {
3 string key ;
4 int value ;
5 hashNode * left ; // Add this line
6 hashNode * right ; // And add this line , too !
7 };

Also, you will need to implement some additional functions to work with AVL Trees.

1.5 Double Hashing


Implement a hash table using double hashing with the same variables as in Linear Probing, and
the same functions but adding a secondary hash function.
The secondary hash function h2 (s) could be h2 (s) = h1 (s) mod prime, where h1 (s) is the
primary hash function (equation 1), and prime is a prime number smaller than the table capacity.
The index for probing is calculated as index = (h1 (s) + i × h2 (s)) mod capacity where i is the
probe iteration.

University of Science Faculty of Information Technology Page 3


Lab 7 Data structures and Algorithms CSC10004

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.

University of Science Faculty of Information Technology Page 4

You might also like