Skip to content
Related Articles

Related Articles

Coalesced hashing
  • Difficulty Level : Hard
  • Last Updated : 05 Nov, 2019

Coalesced hashing is a collision avoidance technique when there is a fixed sized data. It is a combination of both Separate chaining and Open addressing. It uses the concept of Open Addressing(linear probing) to find first empty place for colliding element from the bottom of the hash table and the concept of Separate Chaining to link the colliding elements to each other through pointers. The hash function used is h=(key)%(total number of keys). Inside the hash table, each node has three fields:

  • h(key): The value of hash function for a key.
  • Data: The key itself.
  • Next: The link to the next colliding elements.

The basic operations of Coalesced hashing are:

  1. INSERT(key): The insert Operation inserts the key according to the hash value of that key if that hash value in the table is empty otherwise the key is inserted in first empty place from the bottom of the hash table and the address of this empty place is mapped in NEXT field of the previous pointing node of the chain.(Explained in example below).
  2. DELETE(Key): The key if present is deleted.Also if the node to be deleted contains the address of another node in hash table then this address is mapped in the NEXT field of the node pointing to the node which is to be deleted
  3. SEARCH(key): Returns True if key is present, otherwise return False.

The best case complexity of all these operations is O(1) and the worst case complexity is O(n) where n is the total number of keys.It is better than separate chaining because it inserts the colliding element in the memory of hash table only instead of creating a new linked list as in separate chaining.
Illustration:
Example:

n = 10
Input : {20, 35, 16, 40, 45, 25, 32, 37, 22, 55}

Hash function

h(key) = key%10

Steps:



  1. Initially empty hash table is created with all NEXT field initialised with NULL and h(key) values ranging from 0-9.
    Hash valueDataNext
    0NULL
    1NULL
    2NULL
    3NULL
    4NULL
    5NULL
    6NULL
    7NULL
    8NULL
    9NULL
  2. Let’s start with inserting 20, as h(20)=0 and 0 index is empty so we insert 20 at 0 index.
    Hash valueDataNext
    020NULL
    1NULL
    2NULL
    3NULL
    4NULL
    5NULL
    6NULL
    7NULL
    8NULL
    9NULL
  3. Next element to be inserted is 35, h(35)=5 and 5th index empty so we insert 35 there.
    Hash valueDataNext
    020NULL
    1NULL
    2NULL
    3NULL
    4NULL
    535NULL
    6NULL
    7NULL
    8NULL
    9NULL
  4. Next we have 16, h(16)=6 which is empty so 16 is inserted at 6 index value.
    Hash valueDataNext
    020NULL
    1NULL
    2NULL
    3NULL
    4NULL
    535NULL
    616NULL
    7NULL
    8NULL
    9NULL
  5. Now we have to insert 40, h(40)=0 which is already occupied so we search for the first empty block from the bottom and insert it there i.e 9 index value.Also the address of this newly inserted node(from address we mean index value of a node) i.e(9 )is initialised in the next field of 0th index value node.
    Hash valueDataNext
    0209
    1NULL
    2NULL
    3NULL
    4NULL
    535NULL
    616NULL
    7NULL
    8NULL
    940NULL
  6. To insert 45, h(45)=5 which is occupied so again we search for the empty block from the bottom i.e 8 index value and map the address of this newly inserted node i.e(8) to the Next field of 5th index value node i.e in the next field of key=35.
    Hash valueDataNext
    0209
    1NULL
    2NULL
    3NULL
    4NULL
    5358
    616NULL
    7NULL
    845NULL
    940NULL
  7. Next to insert 25, h(25)=5 is occupied so search for the first empty block from bottom i.e 7th index value and insert 25 there. Now it is important to note that the address of this new node cant be mapped on 5th index value node which is already pointing to some other node. To insert the address of new node we have to follow the link chain from the 5th index node until we get NULL in next field and map the address of new node to next field of that node i.e from 5th index node we go to 8th index node which contains NULL in next field so we insert address of new node i.e(7) in next field of 8th index node.
    Hash valueDataNext
    0209
    1NULL
    2NULL
    3NULL
    4NULL
    5358
    616NULL
    725NULL
    8457
    940NULL
  8. To insert 32, h(32)=2, which is empty so insert 32 at 2nd index value.
    Hash valueDataNext
    0209
    1NULL
    232NULL
    3NULL
    4NULL
    5358
    616NULL
    725NULL
    8457
    940NULL
  9. To insert 37, h(37)=7 which is occupied so search for the first free block from bottom which is 4th index value. So insert 37 at 4th index value and copy the address of this node in next field of 7th index value node.
    Hash valueDataNext
    0209
    1NULL
    232NULL
    3NULL
    437NULL
    5358
    616NULL
    7254
    8457
    940NULL
  10. To insert 22, h(22)=2 which is occupied so insert it at 3rd index value and map the address of this node in next field of 2nd index value node.
    Hash valueDataNext
    0209
    1NULL
    2323
    322NULL
    437NULL
    5358
    616NULL
    7254
    8457
    940NULL
  11. Finally, to insert 55 h(55)=5 which is occupied and the only empty space is 1st index value so insert 55 there. Now again to map the address of this new node we have to follow the chain starting from 5th index value node until we get NULL in next field i.e from 5th index->8th index->7th index->4th index which contains NULL in Next field, and we insert the address of newly inserted node at 4th index value node.
Hash valueDataNext
0209
155NULL
2323
322NULL
4371
5358
616NULL
7254
8457
940NULL

Deletion process is simple, for example:
Case 1: To delete key=37, first search for 37. If it is present then simply delete the data value and if the node contains any address in next field and the node to be deleted i.e 37 is itself pointed by some other node(i.e key=25) then copy that address in the next field of 37 to the next field of node pointing to 37(i.e key=25) and initialize the NEXT field of key=37 as NULL again and erase the key=37.

Hash valueDataNext
0209
155NULL
2323
322NULL
4NULL
5358
616NULL
7251
8457
940NULL

Case 2: If key to be deleted is 35 which is not pointed by any other node then we have to pull the chain attached to the node to be deleted i.e 35 one step back and mark last value of chain to NULL again.

Hash valueDataNext
0209
1NULL
2323
322NULL
4NULL
5458
616NULL
755NULL
8257
940NULL

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :