BS (CS) 3K Lab-12 Data Structures
BS (CS) 3K Lab-12 Data Structures
Lab-12
Data Structures
Quiz-02 Hashing
Section BS(CS) 3K
class HashTable;
class HashItem
private:
};
class HashTable
private:
HashItem* hashArray;
int capacity;
int currentElements;
void doubleCapacity()
public:
HashTable();
~HashTable();
};
About HashItem class:
It represents an item (key-value pair) to be inserted in a hash table. The status variable can have
0, 1 or 2. 0 means empty, 1 means deleted, 2 means occupied. Status variable will be used by get
and delete methods of HashTable implemented in the next questions. The default value assigned
to a HashItem is 0 (empty).
implement this class in this lab. All questions (which you see below) divides this task in parts.
Question1:
Implement the two constructors (default and overloaded), and destructor of the class
HashTable.
Question2:
Now, implement these functions which are given below, and are required for helping in handling
int getNextCandidateIndex(int key, int i): a private method that uses linear probing to return
the next candidate index for storing the item containing key k. Linear probing means that it will
simply add i to the hash value of key. This method does not check whether the candidate index
void doubleCapacity(): A private method which doubles the capacity of hash array and
rehashes
the existing items (Remember we have a dynamic hash array). Use getNextCandidateIndex
Question3:
Implement the public insert function
a. The insert method inserts the value at its appropriate location. Find the first candidate
b. To resolve hash collision, it will use the function getNextCandidateIndex(key, i) to get the
next candidate index. If the candidate index also has collision, then getNextCandidateIndex will
be called again with an increment in i. getNextCandidateIndex will be continued to call until we
find a valid index. Initially i will be 1.
c. If the loadFactor becomes 0.75, then it will call the doubleCapacity method to double the
capacity of array and rehash the existing items into the new array.
Hint: To check load factor, the overall formula used will be currentElements >= 0.75 * capacity
Question 4:
Implement the following functions for deletion of a given value from the hash table and
This method deletes the given key. It returns true if the key was found. If the key was not found
it returns false. When the key is found, simply set the status of the hashItem containing the key
to deleted (value of 1). It also uses status variable to search for the key intelligently ��
This method returns the value of the key. If the key is not found, it returns a message saying
“not found”. It also uses status variable to search for the key intelligently.
Question 5:
Rewrite the class in such a way that it performs Quadratic Probing, i.e., add the square of i to
int main() {
HashTable hashtable(5); // Create a hash map with capacity 5 (for simplicity, using string as
the value type)
hashtable.insert(101, "Alice");
hashtable.insert(201, "Bob");
hashtable.insert(301, "Charlie");
hashtable.insert(401, "David");
hashtable.insert(501, "Eva");
if (name.length()!=0) {
cout << "Value at key 201: " << name << endl;
else {
if (deleted) {
else {
if (name.length()!=0) {
cout << "Value at key 301: " << name << endl; // Should not be found after deletion
else {
return 0;