Hash conflict linear probing opening address method

In practical applications, however construct a hash function, the conflict can not be completely avoided.

Opening address method

The basic idea of this method is: When an address conflict, according to some method continues to detect other storage units in the hash table until you find an empty position so far. This process can be described by the formula: 
H I (Key) = (H (Key) + DI) MOD m (I = 1,2, ......, K (K ≤ m -. 1)) 
where: H (key) of the key direct address hash key words, m is the length of the hash table, di address increment each time the re-detection. 
With this method, the first element directly calculated hash address H (key), if the memory cell has been occupied by other elements, it continues to view the address H (key) + d 2 of the memory cell, and repeat until Found a storage unit is empty, the keywords stored in the data element of the key unit. 
Delta d may have different emulated, and have different names according to their 
emulated: (. 1) DI =. 1, 2,. 3, ...... linear probing 
re-hash; (2) DI = 2. 1 ^, - ^ 2. 1 , 2 ^ 2, - 2 ^ 2, k ^ 2, -k ^ 2 ...... detecting secondary re-hash; 
(. 3) DI = pseudo-random sequence of pseudorandom re-hash;

Example 1 is provided with the hash function H (key) = key mod 7 , hash table address space from 0 to 6, a key string (32, 13, 49, 55, 22, 38, 21) linear probing again by the method of detecting the secondary re-hash and the hash of the hash table are configured. 
Solutions: 
(1) linear probing re-hash: 
32% 7 = 4; 7 = 6 13%; 49% 7 = 0; 
55% = 6 7 conflict, the next memory address (6 + 1) = 7% 0 still conflict, then the next memory address :( 6 + 2) 1 = 7% No collision, can be stored. 
7 = 22% 1 conflict, the next memory address :( 1 + 1) = 7% 2 No collision; 
38% 3 = 7; 
21% 7 = 0 conflict, according to the above process continues until the detecting space 5, do not collide, the resulting hash table corresponding to the storage location: 
index:. 5. 4. 3 0. 1. 6 2 
49 55 32 22 is 38 is 21 is 13 is 
(2) a secondary re-hash detection: 
index: 012345 . 6 
49 32 55 22 is 21 is 38 is 13 is 
NOTE: the need for caution when hash table method using an open address conflict arising remove an element can not be directly deleted, because other elements are truncated with the same hash addresses Find the address, so usually it sets a special flag to indicate that the element has been deleted ( the conditions for various open address method, the empty address location (i.e., open address) are lookup failure ).

example

Given a linear table (38,25,74,63,52,48), assuming h (k) = k% 6 calculates a hash hash memory address, if the conflict detection open linear addressing method, the to find in the average length of the hash table is (). 
A. 1.5 B. 1.7 C. 2 D. 2.3  2,
problem solving process: 
(1) Calculate h (k): 38% 6 = 2 25% 6 = 1 74% 6 = 2 63% 6 = 3 52% 6 = 448% 6 = 0 
(2) addressed: the conflict and no conflict can address all listed: 1 2. 3. 4. 5 0 
1 1 table linear element (38): 38 (1st not conflicts) 
2, the linear form of the second element (25): 25 (1st not in conflict) 
3, linear form the third element (74): 74 (1st conflict, address + 1) 
4, the linear form of 3 elements (74): 74 (2nd not in conflict) 
5, the linear form of four elements (63): 63 (1st conflict, address + 1) 
6, the linear form of four elements (63): 63 (2nd not in conflict) 
7, the linear form of the five elements (52): 52 (1st conflict, address + 1) 
8, the linear form of the five elements (52): 52 (2nd no conflict ) 
9, table 6 linear element (48): 48 (1st not in conflict) 
through the above-described addressing process, each element in the linear table has a unique address. 
2.3 The results in Table 6 linear elements, addressed through 9, in the hash table lookup to find the average length: 9/6 = 1.5, answer selected: A

Guess you like

Origin www.cnblogs.com/zhjh256/p/11704463.html