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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Implementing our Own Hash Table with Separate Chaining in Java
- Hashing | Set 3 (Open Addressing)
- Quadratic Probing in Hashing
- What are Hash Functions and How to choose a good Hash Function?
- Advantages of BST over Hash Table
- Hash Table vs STL Map
- Implement Your Own sizeof
- When should we write our own copy constructor?
- Does C++ compiler create default constructor when we write our own?
- When should we write our own assignment operator in C++?
- Implement your own itoa()
- Write your own memcpy() and memmove()
- Implement your own tail (Read last n lines of a huge file)
- How to implement our own Vector Class in C++?
- Implementing Iterator pattern of a single Linked List
- Implementing ternary operator without any conditional statement
- Implementing Forward Iterator in BST
- Implementing upper_bound() and lower_bound() for Ordered Set in C++
- Python | Implementing Dynamic programming using Dictionary
- Understanding ShellExecute function and it's application to open a list of URLs present in a file using C++ code
Improved By : howdyganesh