Hash table concept

hash concept

Hash is a method to achieve fast search. The time complexity is O(1). The hash function is used to establish a one-to-one mapping relationship between the storage location of an element and its key code, so it can be quickly found. element.

For example, to store data sets, {1, 2, 3, 4, 5, 8}

The hash function is set to hash(key) = key%capacity, and capacity is the size of the storage space. (At this time the capacity is 10)

 But we will find that when elements such as 11, 21, and 1 are inserted, they will be mapped to the same hash address, which is called a hash collision . To solve this problem, we need to set a reasonable hash function.

Hash collision resolution

There are two common methods, closed hashing and open hashing.

Closed hash:

It is also called open addressing method. When a hash conflict occurs, if the hash table is not full, it means that there must be an empty position in the hash table,
then the key can be stored in the "next" empty position in the conflict position. go to the location.

 And how to find the next empty position?

A linear exploration is required, starting from the position where the conflict occurs, and then probing backwards until the next empty position is found.

Unhash:

The open hash method is also called the chain address method (open chain method). First, the hash function is used to calculate the hash address for the key code set. The key codes with the same address belong to the same subset. Each subset is called a bucket. The elements in the bucket are linked by a singly linked list, and the head node of each linked list is stored in a hash table.

 

Guess you like

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