![]() |
VOOZH | about |
Hash Tables(similar to tables in general) provide a subset of the dynamic set operations. Usually, a set of keys are mapped with some values based on certain relations. However, there might be situations when different keys map to the same position provided by the Hash function, which leads to a collision.
👁 ImageOne of the ways to overcome this situation is Hash Table Chaining.
The chaining approach to resolve collisions deals with it by going ahead and putting all the keys that map to a slot in that slot but representing them as a linked list.
Hash Table chaining in Java is possible with both, Singly Linked List and Doubly Linked List. Though the implementation is same, the only difference is that Doubly Linked List allows two-way traversal i.e., the node contains a pointer to the next as well as the previous node. Thus, the complexity of insertion and deletion at a known position reduces to O(1) as compared to the Singly Linked List (O(n)).
Example :
In Singly Linked List : g -> e -> e -> k -> s Here, each node contains a pointer to the next node only. In Doubly Linked List : g <-> e <-> e <-> k <-> s Each node contains pointer to the next as well as previous node.
Approach :
Below is the implementation of the above approach:
At 0: 80 At 1: 36 At 2: 47 At 3: 23 At 4: 99 At 0: 80 At 1: 36 At 2: 92 47 At 3: 23 At 4: 49 99 At 0: 80 At 1: 36 At 2: 92 47 At 3: 23 At 4: 49 At 0: At 1: At 2: At 3: At 4: true