Coalesced hashing - GeeksforGeeks

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:
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).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 deletedSEARCH(key): Returns True if key is present, otherwise return False.
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).
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
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:
Initially empty hash table is created with all NEXT field initialised with NULL and h(key) values ranging from 0-9.Hash valueDataNext0–NULL1–NULL2–NULL3–NULL4–NULL5–NULL6–NULL7–NULL8–NULL9–NULLLet’s start with inserting 20, as h(20)=0 and 0 index is empty so we insert 20 at 0 index.Hash valueDataNext020NULL1–NULL2–NULL3–NULL4–NULL5–NULL6–NULL7–NULL8–NULL9–NULLNext element to be inserted is 35, h(35)=5 and 5th index empty so we insert 35 there.Hash valueDataNext020NULL1–NULL2–NULL3–NULL4–NULL535NULL6–NULL7–NULL8–NULL9–NULLNext we have 16, h(16)=6 which is empty so 16 is inserted at 6 index value.Hash valueDataNext020NULL1–NULL2–NULL3–NULL4–NULL535NULL616NULL7–NULL8–NULL9–NULLNow 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 valueDataNext02091–NULL2–NULL3–NULL4–NULL535NULL616NULL7–NULL8–NULL940NULLTo 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 valueDataNext02091–NULL2–NULL3–NULL4–NULL5358616NULL7–NULL845NULL940NULLNext 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 valueDataNext02091–NULL2–NULL3–NULL4–NULL5358616NULL725NULL8457940NULLTo insert 32, h(32)=2, which is empty so insert 32 at 2nd index value.Hash valueDataNext02091–NULL232NULL3–NULL4–NULL5358616NULL725NULL8457940NULLTo 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 valueDataNext02091–NULL232NULL3–NULL437NULL5358616NULL72548457940NULLTo 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 valueDataNext02091–NULL2323322NULL437NULL5358616NULL72548457940NULLFinally, 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.
Initially empty hash table is created with all NEXT field initialised with NULL and h(key) values ranging from 0-9.Hash valueDataNext0–NULL1–NULL2–NULL3–NULL4–NULL5–NULL6–NULL7–NULL8–NULL9–NULL
Let’s start with inserting 20, as h(20)=0 and 0 index is empty so we insert 20 at 0 index.Hash valueDataNext020NULL1–NULL2–NULL3–NULL4–NULL5–NULL6–NULL7–NULL8–NULL9–NULL
Next element to be inserted is 35, h(35)=5 and 5th index empty so we insert 35 there.Hash valueDataNext020NULL1–NULL2–NULL3–NULL4–NULL535NULL6–NULL7–NULL8–NULL9–NULL
Next we have 16, h(16)=6 which is empty so 16 is inserted at 6 index value.Hash valueDataNext020NULL1–NULL2–NULL3–NULL4–NULL535NULL616NULL7–NULL8–NULL9–NULL
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 valueDataNext02091–NULL2–NULL3–NULL4–NULL535NULL616NULL7–NULL8–NULL940NULL
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 valueDataNext02091–NULL2–NULL3–NULL4–NULL5358616NULL7–NULL845NULL940NULL
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 valueDataNext02091–NULL2–NULL3–NULL4–NULL5358616NULL725NULL8457940NULL
To insert 32, h(32)=2, which is empty so insert 32 at 2nd index value.Hash valueDataNext02091–NULL232NULL3–NULL4–NULL5358616NULL725NULL8457940NULL
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 valueDataNext02091–NULL232NULL3–NULL437NULL5358616NULL72548457940NULL
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 valueDataNext02091–NULL2323322NULL437NULL5358616NULL72548457940NULL
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.
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.
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.
shubham_singh
Advanced Data Structure
Hash
Hash
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Extendible Hashing (Dynamic approach to DBMS)
Ternary Search Tree
Proof that Dominant Set of a Graph is NP-Complete
2-3 Trees | (Search, Insert and Deletion)
Advantages of Trie Data Structure
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Internal Working of HashMap in Java
Hashing | Set 1 (Introduction)
Count pairs with given sum
Hashing | Set 3 (Open Addressing)