Lab08 - DS - Hash Tables
Lab08 - DS - Hash Tables
Lab08 - DS - Hash Tables
LAB MANUAL # 08
Course Instructor: Lecturer Anum Abdul Salam
Total 100
Lab Description:
Hash tables are another type of data structures. Before going into the details of hash table, we
will see why we need hash tables? In binary search trees complexity of operations (insert, search,
delete) is said to be O(logN). This is only possible if tree is balanced. If the BST is not balanced,
complexity of these operations is not O(logN).
Hash Tables
A hash table is a collection of items which are stored in such a way as to make it easy to find
them later. Each position of the hash table, often called a slot, can hold an item and is named by
an integer value starting at 0. For example, we will have a slot named 0, a slot named 1, a slot
named 2, and so on. Initially, the hash table contains no items so every slot is empty. We can
implement a hash table by using a list with each element initialized to the special value.
The mapping between an item and the slot where that item belongs in the hash table is called the
hash function. The hash function will take any item in the collection and return an integer in the
range of slot names, between 0 and m-1. Assume that we have the set of integer items 54, 26, 93,
17, 77, and 31. Our first hash function, sometimes referred to as the “remainder method,” simply
takes an item and divides it by the table size, returning the remainder as its hash value
(h(item)=item%11). Figure 11.2 gives all of the hash values for our example items. Note that this
remainder method (modulo arithmetic) will typically be present in some form in all hash
functions, since the result must be in the range of slot names.
Figure 8.2: Mapping values in hash table using hash function Item%size
Now when we want to search for an item, we simply use the hash function to compute the slot
name for the item and then check the hash table to see if it is present. This searching operation is
O(1), since a constant amount of time is required to compute the hash value and then index the
hash table at that location. If everything is where it should be, we have found a constant time
search algorithm.
You can probably already see that this technique is going to work only if each item maps to a
unique location in the hash table. For example, if the item 44 had been the next item in our
collection, it would have a hash value of 0 (44%11==0). Since 77 also had a hash value of 0, we
would have a problem. According to the hash function, two or more items would need to be in
the same slot. This is referred to as a collision (it may also be called a “clash”). Clearly,
collisions create a problem for the hashing technique. We will discuss them in detail later.
1. Chaining
2. re-hashing,
3. using neighboring slots (linear probing),
4. quadratic probing,
Linear Probing
Start at the original hash value position and then move in a sequential manner through the slots
until we encounter the first slot that is empty. Note that we may need to go back to the first slot
(circularly) to cover the entire hash table. By systematically visiting each slot one at a time, we
are performing an open addressing technique called linear probing.
When we attempt to place 44 into slot 0, a collision occurs. Under linear probing, we look
sequentially, slot by slot, until we find an open position. In this case, we find slot 1. Again, 55
should go in slot 0 but must be placed in slot 2 since it is the next open position. The final value
of 20 hashes to slot 9. Since slot 9 is full, we begin to do linear probing. We visit slots 10, 0, 1,
and 2, and finally find an empty slot at position 3.
Chaining
An alternative method for handling the collision problem is to allow each slot to hold a reference
to a collection (or chain) of items. Chaining allows many items to exist at the same location in
the hash table. When collisions happen, the item is still placed in the proper slot of the hash table.
As more and more items hash to the same location, the difficulty of searching for the item in the
collection increases. Figure 11.6 shows the items as they are added to a hash table that uses
chaining to resolve collisions.
Figure 8.5: Chaining
LAB TASK:
1. Create a Website login system. System should provide an interactive panel to register the
users. User can login the system if password matched. Insert passwords into the Hash Table
retrieve one user's Password structure from the Hash Table compare retrieved user password
to input password and print "Authentication failure" or "Authentication successful". Add in a
line to insert passwords into the table. Build and Run your program.
Note:
• Use STL in the Lab Task
Sr. Operations Expected Results Results/Status
Code:
#include <iostream>
#include <string>
using namespace std;
class HashTable {
private:
HashNode* table[TABLE_SIZE];
public:
HashTable() {
for (int i = 0; i < TABLE_SIZE; ++i) {
table[i] = nullptr;
}
}
int main() {
HashTable hashTable;
// Register users
hashTable.insertUser({ "user1", "password1" });
hashTable.insertUser({ "user2", "password2" });
// Login
string username, password;
cout << "Enter username: ";
cin >> username;
cout << "Enter password: ";
cin >> password;
hashTable.authenticateUser(username, password);
// Remove user
string removeUsername;
cout << "Enter username to remove: ";
cin >> removeUsername;
hashTable.removeUser(removeUsername);
return 0;
}