Coalesced hashing

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.
  2. Hash value Data Next
    0 NULL
    1 NULL
    2 NULL
    3 NULL
    4 NULL
    5 NULL
    6 NULL
    7 NULL
    8 NULL
    9 NULL
  3. Let’s start with inserting 20, as h(20)=0 and 0 index is empty so we insert 20 at 0 index.
  4. Hash value Data Next
    0 20 NULL
    1 NULL
    2 NULL
    3 NULL
    4 NULL
    5 NULL
    6 NULL
    7 NULL
    8 NULL
    9 NULL
  5. Next element to be inserted is 35, h(35)=5 and 5th index empty so we insert 35 there.
  6. Hash value Data Next
    0 20 NULL
    1 NULL
    2 NULL
    3 NULL
    4 NULL
    5 35 NULL
    6 NULL
    7 NULL
    8 NULL
    9 NULL
  7. Next we have 16, h(16)=6 which is empty so 16 is inserted at 6 index value.
  8. Hash value Data Next
    0 20 NULL
    1 NULL
    2 NULL
    3 NULL
    4 NULL
    5 35 NULL
    6 16 NULL
    7 NULL
    8 NULL
    9 NULL
  9. 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.
  10. Hash value Data Next
    0 20 9
    1 NULL
    2 NULL
    3 NULL
    4 NULL
    5 35 NULL
    6 16 NULL
    7 NULL
    8 NULL
    9 40 NULL
  11. 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.
  12. Hash value Data Next
    0 20 9
    1 NULL
    2 NULL
    3 NULL
    4 NULL
    5 35 8
    6 16 NULL
    7 NULL
    8 45 NULL
    9 40 NULL
  13. 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.
  14. Hash value Data Next
    0 20 9
    1 NULL
    2 NULL
    3 NULL
    4 NULL
    5 35 8
    6 16 NULL
    7 25 NULL
    8 45 7
    9 40 NULL
  15. To insert 32, h(32)=2, which is empty so insert 32 at 2nd index value.
  16. Hash value Data Next
    0 20 9
    1 NULL
    2 32 NULL
    3 NULL
    4 NULL
    5 35 8
    6 16 NULL
    7 25 NULL
    8 45 7
    9 40 NULL
  17. 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.
  18. Hash value Data Next
    0 20 9
    1 NULL
    2 32 NULL
    3 NULL
    4 37 NULL
    5 35 8
    6 16 NULL
    7 25 4
    8 45 7
    9 40 NULL
  19. 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.
  20. Hash value Data Next
    0 20 9
    1 NULL
    2 32 3
    3 22 NULL
    4 37 NULL
    5 35 8
    6 16 NULL
    7 25 4
    8 45 7
    9 40 NULL
  21. 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 fied, and we insert the address of newly inserted node at 4th index value node.
Hash value Data Next
0 20 9
1 55 NULL
2 32 3
3 22 NULL
4 37 1
5 35 8
6 16 NULL
7 25 4
8 45 7
9 40 NULL

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 value Data Next
0 20 9
1 55 NULL
2 32 3
3 22 NULL
4 NULL
5 35 8
6 16 NULL
7 25 1
8 45 7
9 40 NULL

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 value Data Next
0 20 9
1 NULL
2 32 3
3 22 NULL
4 NULL
5 45 8
6 16 NULL
7 55 NULL
8 25 7
9 40 NULL


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.