Starting from the hash function, this article tells you exactly what hash ideas and hash table construction are.

Author: Code [K]

Source: CSDN Blog

Editor's Note: The original title was "Listing from Hash Functions, Hash Conflicts, and Open Dispersions. This article tells you exactly what hash ideas and hash table structures are."

Hash, generally translated as hash, hash, or transliteration to hash, is to transform an input of any length (also known as a pre-image) into a fixed-length output through a hash algorithm, and the output is the hash value.

Today we will explore together the mystery of the lowest level of hashing.

Hashing concept

A storage structure is constructed, and a certain function is used to establish a one-to-one mapping relationship between the storage position of its elements and his key code, and then the corresponding element is quickly found through this function when searching.

In short, it is to set a fixed function (hashFunc), and use this function to make the value of the inserted element correspond to the position of the element. When we need to find this element, we can find the value through this function (hashFunc).

Hash function

Hash function (English: Hash function), also known as hash algorithm, hash function, is a method to create a small digital "fingerprint" from any kind of data. The hash function compresses the message or data into a digest, making the amount of data smaller and fixing the format of the data.

This function scrambles the data and recreates a fingerprint called hash values (hash codes, hash sums, or hashes). The hash value is usually represented by a short string of random letters and numbers.

The hash function makes the calculated addresses evenly distributed throughout the space.

Insert and search elements

According to the key code of the element to be inserted, its storage location is calculated according to the hash function.

We use the hash function of the remainder division method to introduce: Example: The existing numbers of 1, 3, 4, 5, 6, and 9 are stored, and the result of the modulo operation of n% 10 is used as the hash address for element insertion. .

If you want to find an element, you only need to perform a hash function operation on the search element to get its storage address, and you can find the element.

Hash collision

When an element is inserted, the address calculated by the hash function is already occupied by other elements, which is called a hash collision.

Such as:

In order to better identify whether the current position is occupied, we need to mark each position

  enum state {EMPTY, FULL, DELETE}; 

Note: If we want to delete an element, we cannot delete it directly. If we delete directly, it will affect the current structure and cause the search of other elements to be wrong. So when we want to delete an element, we need to mark it as deleted. , Not empty.

Open hash

Open hashing is also called the chain address method . First, a hash function is used to calculate the hash address for a set of key codes. When there are key codes with the same address, all elements of the same address are linked by a single linked list. The head node is stored in the hash table.

Now, you should understand the idea of ​​hash and the structure of hash table, right? Welcome to share your thoughts with us in the comment area!

We will continue to update Blocking; if you have any questions or suggestions, please contact us!

Share:

Was this article helpful?

93 out of 132 found this helpful

Discover more

Blockchain

The coin was stolen for the first time, and the 7000BTC was missing.

On the morning of May 8, the world-renowned cryptocurrency exchange currency announced that the currency security was...

Blockchain

A new attempt at traditional finance, the technology of the Stock Exchange enters the currency circle

In January 2019, the London Stock Exchange Group announced a partnership with the digital asset trading platform AAX,...

Blockchain

Is the 'big boss' of the cryptocurrency world, Binance, starting to decline?

LianGuaiBitpushNews Mary Liu As 2022 comes to a close, it seems that Zhao Changpeng, co-founder and CEO of Binance, ...

Blockchain

Look at IEO, the dilemma of markets, exchanges, project parties and investors

"IEO's projects are flying, do you want to follow?" Wei Dong entered the currency circle for more than...

Blockchain

PAData: FCoin potential victims or more than 2000 people, per capita loss or more than 25 BTC

Analyst | Carol Editor | Bi Tongtong Production | PANews Data Partner | Chain.info On February 17, the FCoin, which w...

Blockchain

Simultaneously addressing the pressing issues of DEX and CEX, the company received a $ 40 million investment from Sequoia and Intel

Starkware, an Israeli startup, believes that it can solve two of the most pressing issues in crypto trading-the inher...