Hash Table Theoretical Basis

The study arrangement is based on "Code Caprice"~

What is a hash table: a data structure accessed according to the value of the key [official explanation]

Personal summary: The corresponding value can be found according to the index. Example: An array is a hash table.

What is the use of a hash table: It is generally used to quickly determine whether an element appears in a set.

Time complexity of finding an element in a hash table : O(1).

What is a hash function: a function that maps a stored hash value to an index of a hash table by a specific encoding

which is

What is hash collision: Since the number of elements to be stored is greater than the size of the hash table, different hash values ​​correspond to the same index, which is called hash collision. There are two ways to solve hash collisions: zipper method and linear detection method.

 (a) Zipper method (b) Linear detection method

 Zipper method: Choose the appropriate hash table size, so that memory will not be wasted because the array null value is too large, and search time will not be wasted because the linked list is too long.

Linear detection method: The size of the hash table needs to be larger than the length of the data element, and it is necessary to find vacancies for the conflicting elements.

Common hash structures:

1. array 2. set collection 3. map (map)

The underlying implementation principle:

PS: The underlying implementation of std::set and std::multiset is a red-black tree. The red-black tree is a balanced binary search tree , so the key value is ordered , but the key cannot be modified . Changing the key value will cause The whole tree is messed up, so it can only be deleted and added.

  PS: The underlying implementation of std::unordered_map is a hash table, and the underlying implementation of std::map and std::multimap is a red-black tree. Similarly, the keys of std::map and std::multimap are also ordered.

Recommendations:

1. To solve the hash problem, use unordered_set first, because its query and addition and deletion efficiency are optimal .

2. If you need to insert in order , use set; in addition, if you want the data to be repeated, use multiset .

3. Map is a data structure of <key-value> pairs. There are restrictions on the storage of keys in map, but not for values.

4. The underlying implementation of the red-black tree can still solve the mapping problem , so it is also called hashing.

5.hash_map and hash_set have the same functions as unordered_set and unordered_map, but the latter has been introduced into the C++11 standard library, and the former has not .

Summary: Hash is used to determine whether an element exists in a collection, but sacrifices space for query time.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324155625&siteId=291194637