0% found this document useful (0 votes)
6 views3 pages

Lab8 Hash ThamKhao

This document outlines a lab assignment for implementing a hash table with various collision handling techniques including Linear Probing, Quadratic Probing, Chaining using Linked List, Chaining using AVL Tree, and Double Hashing. Each method requires the creation of specific data structures and functions to manage key-value pairs effectively. The document also provides a polynomial rolling hash function for string hashing and example code for implementation.

Uploaded by

thhainguyen1206
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Lab8 Hash ThamKhao

This document outlines a lab assignment for implementing a hash table with various collision handling techniques including Linear Probing, Quadratic Probing, Chaining using Linked List, Chaining using AVL Tree, and Double Hashing. Each method requires the creation of specific data structures and functions to manage key-value pairs effectively. The document also provides a polynomial rolling hash function for string hashing and example code for implementation.

Uploaded by

thhainguyen1206
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Hash Table Data structures and Algorithms CSC10004

Weekly Lab

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


Hash Table 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


Hash Table 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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy