Hash table of C++ data structure

Definition of Hash Table: Hash table is a data mapping structure that finds the value according to the key code. The structure finds the place to store the value by mapping the location of the key code. A key can correspond to multiple values ​​(that is, hash collision), and the value is stored in the corresponding key according to the corresponding hash formula.

 The construction requirements of the hash function:

  1. The operation process should be as simple and efficient as possible to improve the insertion and retrieval efficiency of the hash table;
  2. The hash function should have a good hash type to reduce the probability of hash collision, that is, try to make the records corresponding to the keywords evenly distributed in the hash table
  3. Hash functions should have greater compressibility to save memory
  4. A very small change in the keyword can cause a very large change in the hash value.

Hash conflict resolution: 1. Chain address method

If there is a conflict in the principle of the chain address method, he will create a new space at the original address , and then insert it into the space in the form of a linked list node .

 

Below is a picture taken from Baidu, which can clearly reflect the structure below. For example, I have a bunch of data {1,12,26,337,353...}, and my hash algorithm is H(key)=key mod 16, the hash value of the first data 1 is f(1)=1, insert After the 1 node, the hash value f(12)=12 of the second data 12 is inserted into the 12 node, and the hash value f(26)=10 of the third data 26 is inserted into the 10 node Later, for the fourth data 337, the calculated hash value is 1, and a conflict is encountered, but it is still only necessary to find the last link point of the 1 node and insert it, and the same is true for 353.

 

 2. Open address method

 

Reference article:

https://www.cnblogs.com/s-b-b/p/6208565.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325207379&siteId=291194637