In the open addressing method, all elements are stored in the slot. In the hash list of the linked list method, each slot stores the pointer of the corresponding linked list. To maintain a linked list, each node of the linked list must have an additional field to store its foreplay and subsequent nodes. The open addressing method does not store elements outside the slot, does not use pointers, and does not have to use additional domains to maintain a data structure. All the space saved without storing pointers can be used, this allows you to use the same space to provide more slots, potentially reducing conflicts and improving the search speed.
To insert an element using the open addressing method, you need to continuously check the hash, or probe, until an empty slot is found to hold the keywords to be inserted.
There are three common techniques used to calculate the probe sequence in the open addressing method: Linear probe, binary probe, and dual probe.
1. linear profiling: A common hash function H': U-> {0, 1 ,..., this is called an auxiliary hash function. The linear exploration method uses the following hash functions:
H (K, I) = (H '(k) + 1) Mode N, I = 0, 1,... m-1
Given a keyword K, we first probe T [H '(k)], that is, the slot given by the auxiliary hash function, and then explore the slot [H' (k) + 1], and so on until T [m-1], and then t [0], t [1],..., until the slot [H '(k)-1] is finally explored, in the linear probe method, the initial probe position determines the entire sequence, so there are only m different probe sequences.
2. secondary exploration: Use a hash function like the following: H (K, I) = (H '(k) + c1i + c2i ^ 2) mod m
H is an auxiliary hashed wave function. C1 and C2 are positive auxiliary constants, I = 0, 1,..., m-1.
Like a linear probe, the initial probe position of a secondary probe determines the entire sequence.
3. double hash: double hash is one of the best ways to use the open addressing method, because the arrangement it generates has so many features of random selection and arrangement. Double hash uses the following form of hash function: H (K, I) = (H1 (k) + ih2 (k) mod m
Both H1 and H2 are auxiliary hash functions. The initial probe position is t [H1 (k)], and the subsequent probe position is the offset H2 (k) m added to the previous position. To search for the entire hash, the value H2 (k) must be m-Prime to the table size.
The following is an example of an open addressing class definition:
# Ifndef _ open_addressing_hash_h _ # DEFINE _ open_addressing_hash_h _/***************************** **************************************** * ** introduction to algorithms: Open addressing hash, this course uses a double hash function, where H1 (K) = K % m, H2 (K) = 1 + K % (m-1) **************************************** * ******************************/# include <stdexcept> template <class T> class openaddressinghash {public: // define a hash element type struct node {friend class openaddres Singhash <t>; // key value of the hashed element. The key must be greater than or equal to 0. If the key is-1, the slot is empty, // If key =-2, the int key; t value; private: node (): Key (-1) {} node (int K, const T & V): Key (K), value (v) {}}; // insert an element node * insert (size_t key, const T & value ); // search for an element node * search (size_t key); // delete a hash element void remove (size_t key); Private: // hash size static const size_t _ table_size = 11; // hash node _ TABLE [_ table_size]; // hash function size_t Hash (size_t K, Size_t); // auxiliary hash function H1 h2inline size_t hash1 (size_t K); inline size_t hash2 (size_t K) ;}; template <class T> typename openaddressinghash <t>:: node * openaddressinghash <t>: insert (size_t key, const T & Value) {size_t I = 0; while (I! = _ Table_size) {auto hashcode = hash (key, I); Auto node = & _ TABLE [hashcode]; // If the keywords in the slot are the same as those to be inserted, modify the element value if (node-> key = Key | node-> key =-2 | node-> key =-1) {node-> key = static_cast <int> (key); node-> value = value; return node ;}++ I ;} throw STD :: overflow_error ("hash table overflow");} template <class T> typename openaddressinghash <t >:: node * openaddressinghash <t >:search (size_t key) {size_t I = 0; while (I! = _ Table_size) {auto hashcode = hash (key, I ++); If (_ TABLE [hashcode]. key = Key) Return & _ TABLE [hashcode];} return nullptr;} template <class T> void openaddressinghash <t >:: remove (size_t key) {auto node = search (key); If (node) // set the key to-2, indicating that the current slot element has been deleted // do not set the key to-1, in this case, elements with the same hash value cannot access node-> key =-2;} template <class T> size_t openaddressinghash <t >:: Hash (size_t key, size_t I) {return (hash1 (key) + I * hash2 (key) % _ table_size;} template <class T> size_t openaddressinghash <t>: hash1 (size_t key) {return key % _ table_size;} template <class T> size_t openaddressinghash <t >:: hash2 (size_t key) {return key % (_ table_size-1) + 1 ;} # endif
Open addressing of hash