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 firstname.lastname@example.org. 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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- 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
- Quadratic Probing in Hashing
- What are Hash Functions and How to choose a good Hash Function?
- Implementing Forward Iterator in BST
- Implementing upper_bound() and lower_bound() for Ordered Set in C++
- Implementing ternary operator without any conditional statement
- Python | Implementing Dynamic programming using Dictionary
- Implementing Iterator pattern of a single Linked List
- std::hash class in C++ STL
- MD5 hash in Java
- Graph representations using set and hash
- Hash Function for String data in C#
- Sorting using trivial hash function
- Understanding ShellExecute function and it's application to open a list of URLs present in a file using C++ code
- Sort elements by frequency | Set 4 (Efficient approach using hash)
- Find k most frequent in linear time
- Overview of Data Structures | Set 2 (Binary Tree, BST, Heap and Hash)
Improved By : howdyganesh