[Talking about data structure] The most detailed hash table (hash table) explanation! ! ! (One)

There are three articles in the hash table series

1. Overview
of the hash table 2. The role and construction of the hash function
3. Code implementation of the hash table search

Because the content of the hash table is too much, I plan to divide it into three articles to explain the hash table.

The focus of this article is an overview of the hash table


1. What is a hash table?

Hash table , also called a hash table (the Hash Table) , using the hashing technique recorded in a contiguous storage space in the storage.

In the hash table, we use a certain function f to make the storage location = f (keyword) , so that we can get the storage location of the required record without comparing keywords.

There is no logical relationship between the records of the hash technology, it is only related to the keywords. Therefore, hashing is mainly a search-oriented storage structure .


2. How is the hash table searched?

A certain function mentioned above is called a hash function , which is responsible for recording the corresponding relationship f between the storage location and its keywords .

Hash function, also known as Hash function

  • The correspondence between the storage location of the record and its key f is called a hash function
  • Hashing technology is a new storage technology
  • Hashing technology is to establish a certain correspondence between the storage location of the record and its key f
  • Make each keyword key correspond to a storage location f (key)

When searching:

  • Find the mapping f (key) of the given value key according to this determined correspondence
  • If this record is found in the set, it must be at the position of f (key).

key is the key, f is the hash function, f (key) is the storage location


3. Hash table search steps

During storage, the hash address of the record is calculated through the hash function, and the record is stored according to the hash address.

Suppose a scenario, you have a courier to send out, the courier will help you post the courier number, and then he asks you to put it on the desk in the station.

But as soon as you enter the inn, there are many tables, and you don't know which one he said.

At this time, a person came up to ask if you were going to send the courier, and asked if you had posted the courier number. After you gave him the number, he helped you put it on a designated table.

This is the stored procedure of the hash table. The courier number is the keyword, the person in the station is the hash function, and the table where the courier is placed is the final specified address.


When looking up a record, we calculate the hash address of the record through the same hash function, and access the record according to this hash address.

It's still the scene, but you are not sending a courier at this time, but you are here to get the spicy duck wings sent to you by a good friend.

You went directly to the station where the express was placed, and gave the courier number to that person, who found your courier through your express number, and then gave it to you.

The steps for searching and storing are the same, but the purpose we use is different. We all give keywords to the hash function, and store/search through the storage location calculated by the hash function.

To put it simply, the function of the hash function is to calculate where to store things and where to get them from?


4. Two principles of a good hash function:

1. Simple
calculation The calculation time of the hash function should not exceed the time of other search techniques and keywords comparison.

2. The hash addresses are evenly distributed The
best way to resolve conflicts is to distribute the hash addresses evenly in the storage space.

Ensure the effective use of storage space and reduce the time spent dealing with conflicts.


5. What is the conflict?

In the principle put forward above, we see a new word " conflict ", so what is conflict?

The conflict is that two different keywords , but the addresses obtained by the hash function are the same .
key 1 ≠ key 2 , but f (key 1 ) = f (key 2 )

Synonyms
At this time, key 1 and key 2 are called synonyms of this hash function

That's not okay, how can two people live in a single room?

Don't worry, this problem has naturally been solved by the powerful bosses.
Wait for my next article to come slowly~


6. Summary

Finally, we summarize the advantages and disadvantages of hash tables:

The most suitable solution for the hashing technique is to find a record equal to a given value:

  • Advantages: simplifies the comparison process and improves search efficiency
  • Disadvantages: does not have the ability to many conventional data structures
    • A single keyword cannot record multiple records
    • Not suitable for range search

The above is all the content of this article. If you find it helpful to you,
please use your hands, like, bookmark and forward! ! !
Every time you like is my biggest motivation for updating~

See you next time!

Guess you like

Origin blog.csdn.net/weixin_43776724/article/details/112912006