![]() |
VOOZH | about |
In Open Addressing, all elements are stored directly in the hash table itself. Therefore, the size of the hash table must be greater than the total number of keys. To maintain good performance, the load factor (number of keys divided by table size) should be kept below a certain limit, usually 0.7. If needed, the table size can be increased by rehashing the existing elements.
Insert(k): The hash function is applied to the key to generate an index. If that slot is occupied, probing continues until an empty or deleted slot is found, and the key is inserted there.
Search(k): The hash function generates the starting index, and probing continues until the key is found or an empty slot is encountered.
Delete(k): Instead of removing an element completely, its slot is marked as "deleted" using a dummy node (key = –1, value = –1). This ensures that searching does not stop prematurely and that future insert operations can reuse deleted slots.
This process ensures that every key is mapped to a valid index within the hash table and that values are stored based on the position generated by the hash function. Searching, insertion, and deletion take O(1) average time, but in the worst case, these operations may take O(n) time if the table becomes too full or has many deleted slots.
Implementation:
Hash Map Elements: 1 10 2 20 3 30 22 220 42 420 Size of map: 5 Deleting key 2 -> 20 Size after deletion: 4 Value of key 3: 30 Value of key 2: -1 Is map empty? No
Complexity analysis for Insertion:
Time Complexity:
Auxiliary Space: O(1)
Complexity analysis for Deletion:
Time Complexity:
Auxiliary Space: O(1)
Complexity analysis for Searching:
Time Complexity:
Auxiliary Space: O(1) for search operation