The fifth chapter of the hash
The implementation of a hash table is often called a hash (hashing). Hashing is a technique used to perform insertions, deletions, and lookups with constant average time.
There is a very important concept about hashing: hash function. The hash function is one of the key points of the hash , and the hash function is a correspondence relationship based on the mapping mechanism (which is usually a many-to-one relationship).
This chapter can be divided into 5 parts: general idea, hash function, separated link method, open addressing method (can be divided into linear detection, square detection, double hash), re-hash, the proliferation of columns.
This article is written only in the first four sections. Namely: general idea, hash function, separated link method, open addressing method (can be divided into linear detection, square detection, double hash) ()
Chapter Fifth Section I: General ideas
Consider the hash table as a simple array (in fact, we will know that the essence of the hash table is an array), the size of the array is recorded as tablesize. As a result, we can
The array corresponding subscript [0, TableSize-1] to allocate the storage location for the element we are going to deposit. is a simple and ideal hash table:
Ycx 2011716001 was hashed to 1;
Leilei 2011716037 was hashed to 4;
Tracy 2011716049 was hashed to 6;
This is the basic idea of hashing. If you have a lot of elements (more than 10) and do not change the size of the array, then there must be a number of hashes that are duplicated, right? So how do you store these repeating elements?
This is one of the most intractable problems in hashing, "collision" (translated into conflict). In the process of studying how to solve the conflict, several solutions have been produced: the separation link method and the open addressing method.
Let's take a look at what a hash function is before we introduce some of these conflict resolution methods.
Fifth Chapter II: hash function
Set hash (x) as hash function, like these: hash (x) = x tablesize or hash (x) = (Add the acsii code of all characters)
Or a more complex Hash (X) = key[0] + key[* 1] + 729 * key[2]. These functions raise the "utilization" of the array in turn.
Fifth Chapter III: Separate link method
This section is relatively simple, as is the list nature. ()
Fifth Chapter III: Open and definite address law
Divided into linear detection method, square detection method, double hash.
The difference between the separation and linking method is that the open-address method is not a mechanism for separating links, but rather adds a function that I call the probing function . Are you sure the name is image-sufficient? ()
The detection function of the linear detection method ( green ) is:
h I (x) = (Hash (x) + F (i))% tablesize, f (i) = i; i=1,2,3,4 ... , Hash (x) = x% tablesize (hash function)
How does the probing function work? For example: We want to put the keyword {89,18,49,58,69} into the hash list, then the process is this:
The mechanism is this: before each element enters the hash table, the hash function (hash (x) = x% tablesize) is called to handle the number, and the detection function comes to the rescue when a conflict is encountered.
89 is sent to hash (X), processing .... Hash (89) = 89 10 = 9; View Hash number 9 is available, so put it in 9,
Tablesize = 10
0 |
|
1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
89 |
18 is sent to hash (X), processing .... Hash (18) = 18 10 = 8; View Hash number 8 is available, so put it in 8,
0 |
|
1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
18 |
9 |
89 |
49be sent to hash (X), deal with .... Hash (49) = 49 10 = 9; View Hash number 9 not available, so request probe June come, [], let go of that number, let me!!!
49 is sent to the probe function, processing .... H 1 ( +) = (9 + 1)% = 0; View hash number 0, found available, 49 was sent to 0 places,
0 |
49 |
1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
18 |
9 |
89 |
58be sent to hash (X), deal with .... Hash (58) = 58 10 = 8; View Hash number 8Noavailable, so request probe June come, [], let go of that number, let me!!!
< Span style= "font-family: ' Comic Sans MS '" >58 is sent to the probe function, processing .... h 1 &NBSP; ( 58 ) = (8 +1 )% = 9; View hash number 9, discovery not available, probe June continue probing:
58 is sent to the probe function, processing .... h 2 &NBSP; ( 58 2 )% = 0; View hash number 0, found unavailable, probe June continue probing:
58 is sent to the probe function, processing .... H 3 ( +) = (8 +3 )% = 1; View hash number 1, found available, 58 after several and finally landed 1 places,
0 |
49 |
1 |
58 |
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
18 |
9 |
89 |
69 by .... You're not going to say that, are you? If everything is OK then:
0 |
49 |
1 |
58 |
2 |
69 |
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
18 |
9 |
89 |
What about the square-probing method? Similarly, except F (i), the rest of the analysis process is the same. Don't dwell on it.
[BS] Hash table hash Table hash