Code Skiller logoCB Logo
Logo LearnLearnLogo PracticePracticeLogo HireHireLogo IDEIDE

Implementation of a separate chaining collision resolution technique

User image

Published by

sanya sanya

Published at: 6th Aug, 2023
3.255 mins read

Algorithm:

  1. Define a hash table data structure: Start by defining a hash table data structure that will store key-value pairs. Each element of the hash table will be a linked list that will handle collisions using separate chaining.

  2. Choose a hash function: Select a suitable hash function that takes a key as input and returns an index in the hash table. The hash function should distribute the keys uniformly across the table.

  3. Initialize the hash table: Create an array with a fixed size for the hash table. Each element of the array will be a linked list to handle collisions. Initialize all the elements to NULL.

  4. Insertion: To insert a key-value pair into the hash table, apply the hash function to the key to determine the index. If the linked list at that index is empty, create a new node with the key-value pair and add it to the list. If there are already nodes in the linked list, iterate through the list and check if the key already exists. If it does, update the corresponding value. If not, create a new node and append it to the list.

  5. Search: To find the value associated with a given key, apply the hash function to determine the index. Traverse the linked list at that index and search for the key. If found, return the corresponding value. If not found, signify that key does not exist.

  6. Deletion: To remove a key-value pair from the hash table, apply the hash function to determine the index. Traverse the linked list at that index and search for the key. If found, remove the corresponding node from the list. If not found, just return.

Code:

#include <bits/stdc++.h>

using namespace std;

class node{

public:

string key;

int val;

node*next;

node(string k, int v){

val=v;

key=k;

next=NULL;

}

};

class hashmap{

int total_size;

int cur_size;

node**arr;

public:

hashmap(int size){

cur_size=0;

total_size=size;

arr=new node*[total_size];

for(int i=0;i<total_size;i++){

arr[i]=NULL;

}

}

int hashfunction(string key){

int ans=0;

int mul=1;

for(int i=0;i<key.length();i++){

ans+=(key[i]%total_size*mul%total_size)%total_size;

mul=(mul*29)%total_size;

}

ans=ans%total_size;

return ans;

}

}

void insert (string key, int val){

int index=hashfunction(key);

node*n=new node(key, val);

n->next=arr[index];

arr[index]=n;

cur_size++;

}

node*search(string key){

int index=hashfunction(key);

node*head=arr[index];

while(head){

if(head->key==key){

return head;

}

head=head->next;

}

return NULL;

}

void deleteKey (int key){

node* it= search(key);

delete it;

}

};

int main(){

//Creation

hashmap h(10);

//Insertion

h.insert(“A”,1);

h.insert(“B”,2);

h.insert(“C”,3);

//Searching

node*newhead=h.search(“C”);

cout<val;

//Delete a key

deleteKey(“A”);

return 0;

}

The time complexity of each function in a separate chaining hash table depends on several factors, including the size of the hash table (number of buckets or linked lists), the distribution of keys, and the efficiency of the hash function. The expected time complexities in the average case are as follows:

- Insertion: O(1)

  • On average, inserting a key-value pair takes constant time as long as the hash function distributes the keys evenly across the hash table.
  • However, in the worst case, when all keys hash to the same index and create a long linked list, the time complexity can degrade to O(n), where n is the number of elements in the list.

- Search: O(1)

  • Similar to insertion, the average time complexity for searching a key and retrieving the corresponding value is constant, assuming a good distribution of keys.
  • In the worst case, when all keys hash to the same index, the time complexity can be O(n), where n is the number of elements in the list.

- Deletion: O(1)

  • Deleting a key-value pair also takes constant time on average, assuming a good distribution of keys.
  • In the worst case, when all keys hash to the same index, the time complexity can be O(n), where n is the number of elements in the list.

Library

WEB DEVELOPMENT

FAANG QUESTIONS