[Python study notes, python Study Notes
The built-in Dictionary of Python is implemented using hash table. Here we just implement our own hash table to deepen our understanding of hash table and hash functions.
[Concept 1: Mapping )]
The dictionary is indexed by the Key. A key corresponds to a stored value. Any unchangeable data type can be used as a key.
[Concept 2: Hash Table )]
The Hash Table directly accesses the data structure in the memory storage location based on the key, thus accelerating the search speed (O (1 )).
It is an empty Hash Table with size of 11. Each element is initialized to None:
[Concept 3: Hash function (Hash function )]
The value corresponding to the key is stored in the storage location of the f (key). The corresponding relationship f is called the Hash Function:
There are many methods to construct a Hash Function:
For example, (1) Remainder Method (except the Remainder ):
Suppose we have an empty Hash Table with the size (Table length) of 11. Now we want to store a series of integers in the Hash Table: 54, 26, 93, 17, 77, 31; then the remainder hash function is h (key) = key % 11.
The result is as follows:
Now our hash table is changed:
[Concept 4: load factor] λ = number_of_keys/table_size. in the above example, we store 6 items and the hash table length is 11, and the corresponding load factor is 6/11.
When we want to find a key, we only need to use the hash function to calculate the corresponding storage location (slot name) and check whether the location stores the key. The time complexity of this search operation is O (1 ).
[CONCEPT 5: collision or clash]: two items may correspond to the same hash function address, which is called a conflict. For example, when we use the remainder hash function, both 44% 11 and 77% 11 are equal to 0.
In this case, you need to select another hash function or process the conflicting result.
Let's first introduce several other hash functions. We hope to build a hash function to minimize the number of conflicts, facilitate calculation, and achieve even distribution in the hash table:
(2) Folding Method (Folding Method ):
Divide the key into several parts with the same number of digits (the last part may have different digits), sum these parts, and then divide the remaining parts.
For example, if our key is a telephone number 4365554601 and two numbers are in A group (210, 01), sum these parts into 43 + 65 + 55 + 46 + 01 =, the final result is 210% 11 = 1. Therefore, this phone number is
Hash table is stored in slot 1.
(3) Mid-square Method (the square is set to China and France ):
After the keyword is obtained by Square, the remainder of the digits in the middle is obtained as the hash address.
For example, if our key is 44, first take the square 44 ^ 2 = 1936, then take the two digits in the middle of 93, and then take the remainder 93% 11 = 5 for its hash address.
For non-integer keys, such as string, we can use ordinal values to calculate:
For example, the string 'cat', ord ('C') = 99, ord ('A') = 97, ord ('T') = 116, so the corresponding hash address can be (99 + 97 + 116) % 11 = 4
Next, we will discuss the [conflict handling] method:
(1) open addressing: in case of a conflict, we can find the next empty location in the hash table to store the conflicting key. Here we will introduce two methods of searching:
(I) Linear Probing: It is equivalent to Probing hash tables one by one until an empty slot is found and storing the key in this location. For example, the conflicting hash value is h, and the search sequence is h + 1, h + 2, h + 3...
To prevent clustering, you can use the skip slots method.
(Ii) Quadratic Probing (square test): that is, if the conflicting hash value is h, the next query location is h + 1, and the next query location is h + 4, h + 9, h + 16...
(2) chaining: stores all elements with the same hash value in a linked list. However, the more elements that are linked at the same location, the more difficult the search is. The linked list is as follows:
Finally, we implement our own Hash Table, which includes the following methods:
HashTable (size): Creates a new, empty ing. It returns an empty ing set with the size of the initialization table.
Put (key, val): Add a new pair of key-value to the table. If the key already exists in the table, update the value corresponding to the key.
Get (key): returns the value of a given key. If the key does not exist in the table, None is returned.
Del: Use del map [key] to delete the corresponding key-value.
Len (): returns the number of key-value pairs in the table.
In: key in map is used to determine whether the key is in map. True is returned if the key is not in map. False is returned if the key is not in map.