Data structure (Java language)-HashTable (open address method) Simple implementation
The disadvantage of the separated link hashing algorithm is that some linked lists are used. Because it takes time to allocate addresses to new units, this slows down the algorithm speed. At the same time, the algorithm actually needs to implement the second data structure. Another way to resolve conflicts without using a linked list is to try other units until an empty unit is found. More commonly, the h0 (x), h1 (x), h2 (x ),... one by one, Where hi (x) = (hash (x) + f (I) mod TableSize, and f (0) = 0. Function f is a solution to conflicts. Because all data must be placed in a table, the tables required for this solution are larger than the tables with separated links and hashes. Generally, for a hash list without separated links, its fill factor λ should be less than 0.5. We call this table probing hash table ). Now let's look at the general conflict solution in November 3.
In linear probing, function f is a linear function of I, typically f (I) = I. This is equivalent to one-by-one detection unit (which can be bypassed if necessary) to find an empty unit. As long as the table is large enough, you can always find a free unit, but it takes a lot of time. Even if the table is relatively empty, the occupied units will begin to form some blocks. The result is called a aggregation, that is, any keywords hashed into the block must be checked multiple times to resolve the conflict. Then, the keyword is added to the corresponding block. It can be proved that the expected number of probes using linear probes is about 0.5 (1 + 1/(1-λ) ²) for inserted and unsuccessful searches ), for a successful search, the value is 0.5 (1 + 1/(1-λ )). It is easy to see from the program that the inserted and unsuccessful searches require the same number of probes, and the average time spent on successful searches should be less than the unsuccessful searches. Square detection is a solution to resolve a clustering problem in linear detection. Square detection is a quadratic Detection Method for conflicting functions. The popular choice is f (I) = i². For linear detection, it is not a good idea to make the hash list almost fill up the elements, because the table performance will decrease. For square detection, even worse: Once the table is filled in more than half, when the table size is not a prime number or even before the table is filled in half, it cannot be guaranteed to find an empty unit at a time. This is because up to half of the tables can be used as alternative positions for resolving conflicts. It can be proved that insertion may fail even if the table is filled with only one half more. In addition, the table size is also very important as a prime number. If the table size is not a prime number, the number of alternative units may decrease sharply. Standard delete operations cannot be performed independently in the probe hash, because the corresponding unit may have caused a conflict and the element exists elsewhere. Therefore, you need to delete the scattered list. Although a clustering is excluded from the square hash, elements hashed to the same position will detect the same alternative unit, which is called secondary clustering. Secondary aggregation is a small shortcoming in theory. The simulation results indicate that for each query, it usually causes less than half of the other detection. The following technology will eliminate this defect, but it will cost the calculation of an additional hash function. For dual hash columns, f (I) = I * hash? (X ). This formula is used to apply the second hash function to x and separate it from hash? (X), 2 hash? (X),... and so on. Hash? (X) poor selection will be disastrous. The function must not calculate 0. In addition, it is important to ensure that all units can be detected. Such as hash? (X) = R-(x mod R) functions will play a good role, where R is a prime number smaller than TableSize. It is important to ensure that the table size is a prime number when double hash is used. If the table size is not a prime number, the alternative unit may be used up in advance. However, if the dual hash is correctly implemented, the simulation shows that the expected number of probes is almost the same as the solution to the random conflict, which makes the dual hash theoretically attractive. However, the second hash function is not required for square detection, which may be simpler and faster in practice, especially for keywords such as strings, their hash functions are quite time-consuming.
The following is an implementation of a scattered list of square probes:
Import java. util. random; public class QuadraticProbingHashTable {private static final int DEFAULT_TBALE_SIZE = 11; private HashEntry [] array; private int currentSize;/** in three cases: null, non-null, and active, non-null and this item is marked to be deleted */private static class HashEntry {public AnyType element; public boolean isActive; public HashEntry (AnyType e) {this (e, true );} public HashEntry (AnyType e, boolean I) {element = e; isActive = I ;}} publ Ic QuadraticProbingHashTable () {this (DEFAULT_TBALE_SIZE);} public QuadraticProbingHashTable (int size) {array = new HashEntry [size]; makeEmpty ();} public void makeEmpty () {for (int I = 0; I <array. length; I ++) {array [I] = null;} currentSize = 0 ;} /*** whether the hash table contains an element ** @ param x * query element * @ return query result */public boolean contains (AnyType x) {int currentPos = findPos (x); return isActive (currentPos);}/*** to the hash table Insert an element. If yes, do not insert the ** @ param x * element */public void insert (AnyType x) {int currentPos = findPos (x); System. out. println (x + hash-> + currentPos); if (isActive (currentPos) {return;} array [currentPos] = new HashEntry (x, true ); if (++ currentSize> array. length/2) {rehash () ;}/ *** delete an element from the hash table ** @ param x * delete an element */public void remove (AnyType x) {int currentPos = findPos (x); if (isActive (currentPos) {array [CurrentPos]. isActive = false;}/*** find an element subscript by hashing (square probing): f (I) = f (I-1) + 2i-1 ** @ param x * search element * @ return subscripts of this element in the array */private int findPos (AnyType x) {int offset = 1; int currentPos = myhash (x); while (array [currentPos]! = Null &&! Array [currentPos]. element. equals (x) {currentPos + = offset; offset + = 2; if (currentPos> = array. length) {currentPos-= array. length ;}} return currentPos ;} /*** check whether the specified subscript has an activity item ** @ param currentPos * subscript * @ return whether there is an activity item */private boolean isActive (int currentPos) {return array [currentPos]! = Null & array [currentPos]. isActive;}/*** simple hash algorithm ** @ param x * element * @ return hash value */private int myhash (AnyType x) {int hashVal = x. hashCode (); hashVal % = array. length; if (hashVal <0) {hashVal + = array. length;} return hashVal;}/*** re-hash function, run */@ SuppressWarnings (unchecked) private void rehash () when the inserted space is over half () {HashEntry [] oldArray = array; // create an empty table array = new HashEntry [nextPrime (2 * array. length)]; curre NtSize = 0; // copy data to the new table for (int I = 0; I <oldArray. length; I ++) {if (oldArray [I]! = Null & oldArray [I]. isActive) {insert (oldArray [I]. element) ;}} System. out. println (rehash by length of: + array. length);}/*** check whether an integer is a prime number ** @ param num * Check the integer * @ return check result */private static boolean isPrime (int num) {if (num = 2 | num = 3) {return true;} if (num = 1 | num % 2 = 0) {return false ;} for (int I = 3; I * I <= num; I + = 2) {if (num % I = 0) {return false ;}} return true ;} /*** the return value is no less than a certain value. Prime Number of the integer ** @ param num * integer * @ return next prime number (which can be equal) */private static int nextPrime (int num) {if (num = 0 | num = 1 | num = 2) {return 2;} if (num % 2 = 0) {num ++ ;} while (! IsPrime (num) {num + = 2;} return num;} public void printTable () {for (int I = 0; I <array. length; I ++) {System. out. println (-----); if (array [I]! = Null) {System. out. println (array [I]. element +);} else {System. out. println () ;}} public static void main (String [] args) {QuadraticProbingHashTable
HashTable = new QuadraticProbingHashTable
(); Random random = new Random (); for (int I = 0; I <20; I ++) {hashTable. insert (random. nextInt (60);} hashTable. printTable ();}}
Execution result:
22 hash-> 0
15 hash-> 4
45 hash-> 1
33 hash-> 9
45 hash-> 1
17 hash-> 6
32 hash-> 10
22 hash-> 22
45 hash-> 0
15 hash-> 15
17 hash-> 17
33 hash-> 10
32 hash-> 9
Rehash by length of: 23
19 hash-> 19
3 hash-> 3
9 hash-> 13
17 hash-> 17
12 hash-> 12
48 hash-> 2
45 hash-> 0
58 hash-> 16
45 hash-> 45
48 hash-> 1
3 hash-> 3
32 hash-> 32
33 hash-> 33
12 hash-> 12
9 hash-> 9
15 hash-> 15
58 hash-> 11
17 hash-> 17
19 hash-> 19
22 hash-> 22
Rehash by length of: 47
3 hash-> 3
44 hash-> 44
27 hash-> 27
57 hash-> 10
44 hash-> 44
-----
-----
48
-----
-----
3
-----
-----
-----
-----
-----
-----
9
-----
57
-----
58
-----
12
-----
-----
-----
15
-----
-----
17
-----
-----
19
-----
-----
-----
22
-----
-----
-----
-----
-----
27
-----
-----
-----
-----
-----
32
-----
33
-----
-----
-----
-----
-----
-----
-----
-----
-----
-----
-----
44
-----
45
-----