In Open Addressing, all elements are stored in the hash table itself. So at any point, size of table must be greater than or equal to total number of keys (Note that we can increase table size by copying old data if needed).
- Insert(k) – Keep probing until an empty slot is found. Once an empty slot is found, insert k.
- Search(k) – Keep probing until slot’s key doesn’t become equal to k or an empty slot is reached.
- Delete(k) – Delete operation is interesting. If we simply delete a key, then search may fail. So slots of deleted keys are marked specially as “deleted”.
Here, to mark a node deleted we have used dummy node with key and value -1.
Insert can insert an item in a deleted slot, but search doesn’t stop at a deleted slot.
The entire process ensures that for any key, we get an integer position within the size of the Hash Table to insert the corresponding value.
So the process is simple, user gives a (key, value) pair set as input and based on the value generated by hash function an index is generated to where the value corresponding to the particular key is stored. So whenever we need to fetch a value corresponding to a key that is just O(1).
key = 1 value = 1 key = 2 value = 3 2 3 1 0 0
This article is contributed by Chhavi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Implementing our Own Hash Table with Separate Chaining in Java
- Hashing | Set 3 (Open Addressing)
- Hash Table vs STL Map
- Advantages of BST over Hash Table
- What are Hash Functions and How to choose a good Hash Function?
- Implementing Forward Iterator in BST
- Implementing ternary operator without any conditional statement
- Implementing Iterator pattern of a single Linked List
- MD5 hash in Java
- std::hash class in C++ STL
- Understanding ShellExecute function and it's application to open a list of URLs present in a file using C++ code
- Graph representations using set and hash
- Sorting using trivial hash function
- Hash Function for String data in C#
- Sort elements by frequency | Set 4 (Efficient approach using hash)
Improved By : howdyganesh