1. Hash table
A hash table, also known as a hash table, is a data structure that is accessed directly from the key code (key), that is, it maps keys to tables
A location to access the record, that is, the key is calculated as hashcode, the hashcode stored in the table. The function that maps the key to Hashcode
Called a hash function, the array that holds the record is called the hash table.
2.Hash table is an array
3. Given table M, there is a function f (key), for any given keyword value key, after substituting the function, if you can get the address of the record in the table containing the key word,
The table m is called a hash (hash) table, and the function f (key) is a hash function.
4. Common methods for hashing functions:
(1) Direct addressing method: Take a keyword or a keyword of a linear function value is a hash address. That is, H (key) =key or H (key) = a key + B,
Where A and B are constants (this hash function is called its own function). If there is already a value in H (key), go to the next one, until H (key)
If there is no value in it, put it in.
(2) Digital analysis: Analyze a set of data, such as the date of birth of a group of employees,
At this point we find that the first few digits of the date of birth are roughly the same, so that the odds of a conflict are great,
But we found that the number of months and days of the month and day is very different, if you use the following numbers to form a hash address,
The odds of the conflict will be significantly reduced. So the digital analysis method is to find the law of numbers,
Use this data whenever possible to construct a hash address with a lower probability of conflict.
(3) Square method: When it is not possible to determine which of the keywords in the distribution is more evenly,
You can first find the square value of the keyword, and then take the middle of the squared value as the hash address as needed.
This is because: after the square after the middle and each of the keywords are related, so the different keywords will be higher probability to produce a different hash address.
(4) Folding method: The keyword is divided into several parts of the same number of bits, the last part of the number can be different,
Then take the overlay of these parts and (remove the carry) as the hash address. Digital superposition can have two methods: shift superposition and boundary superposition.
The shift overlay aligns the lowest bits of each part of the split, then adds, and the bounding overlay is folded back and forth from one end to the other,
The add is then aligned.
(5) Random number method: Select a random function, take the random value of the keyword as a hash address, usually used in the case of different length of the keyword.
5. Collisions
The same hash address may be obtained for different keywords, that is, K1≠K2, and F (K1) =f (K2), a phenomenon called collisions (English: collision).
A keyword with the same function value is called a synonym for the hash function
(6) In addition to the remainder of the method: Take the keyword is not greater than the hash table length m of the number of p except that the remainder is a hash address.
That is, H (key) = key MOD p,p<=m. Not only can the keyword directly modulo,
You can also take a modulus after folding, square-taking, or medium operations. The choice of P is very important, generally take prime or m, if p is not good, easy to produce synonyms
Hash-1.hash table and hash algorithm