Hash Tabel

During the interview, I was asked: Do you know what the underlying data structure of the dictionary is?

  So what we are mainly talking about today is what the data structure of the hash table looks like; how the hash collision is caused and how to solve the hash collision.

1. Definition

  Hash table (Hash table, also called hash table) is a data structure that is directly accessed based on the key value. In other words, it accesses the record by mapping the key code value to a location in the table to speed up the search. This mapping function is called a hash function, and the array storing records is called a hash table.

  The dictionary storage value case is as follows. In order to understand the data structure of the hash table in a more popular way, let's first look at the following figure:

put("jack","666")
put("Rose","777")
put("Evan","888")

image

image

  Hash Table is mainly composed of 2 parts:

  1. Hash function

  2. Table "The bottom layer is an array, and the general array size is 2n." As for why this is the case, it is for the convenience of bit operations

The role of Hash function and Table

  The main function of the hash function is to manipulate the key to generate an integer index value index. Corresponding to our case:

  • hash("Jack") --> index = 14

  • hash("Rose") --> index = 01

  • hash("Evan") --> index = 03

  Then according to the index, the corresponding value is stored in the Table array. So as to complete the key and value mapping.

2.Hash collision

  Let's add another set of data to the dictionary (Jeffery,999). Suppose hash("Jeffery") --> index = 03, at this time, we will find that the indexes of Jeffery and Rose are both 3 and conflict. This is Hash collision.

image

 

3. Solve Hash collision

  Common methods to resolve Hash conflicts:

  • 1. Open Addressing: Probing to other addresses in accordance with certain rules, until an empty location is encountered

  • 2. Re-Hashing (Rr-Hashing) "design multiple hash functions

  • 3. Separate Chaining, such as stringing elements of the same index through a linked list

  Today I will mainly introduce the chain address method. When a hash collision is found, you can use a singly linked list to string together elements of the same index, as shown in the following figure:

image

image

4. How to survive the hash value of the key?

  Common types of keys may include integers, floating-point numbers, strings, and definition objects.
  Different types of keys have different ways of generating hash values, but the goal is the same:

  • 1. Try to make the hash value of each key unique

  • 2. Try to let all the key information participate in the calculation

  The keys in this article are all strings. Take jack as an example: The hash value of jack can be expressed as: All j * n^3 + a * n^2 + c * n^1 + k * n^0  jackASCII can be checked, so an integer T can be obtained through the above calculation, and then by the size of the Table array, &位运算or %去余运算, You can get the inedx subscript value.

 

image

  Regarding the calculation methods of integers, floating-point numbers, and hash values ​​of defined objects, if you are interested, you can search for it, and there is a more systematic explanation.

to sum up

  Today, I mainly introduced how the data structure of the hash table stores the value, then explained how the hash collision is generated and how to solve it, and finally introduced the calculation method of the hash value of the string key. I believe that after reading this article carefully, you will have a clearer understanding of what a hash table is.

Welcome to pay attention to [The Way of Infinite Testing] public account , reply [receive resources],
Python programming learning resources dry goods,
Python+Appium framework APP UI automation,
Python+Selenium framework Web UI automation,
Python+Unittest framework API automation,

Resources and codes are sent for free~
There is a QR code of the official account at the bottom of the article, you can just scan it on WeChat and follow it.

Remarks: My personal public account has been officially opened, dedicated to the sharing of test technology, including: big data testing, functional testing, test development, API interface automation, test operation and maintenance, UI automation testing, etc., WeChat search public account: "Infinite The Way of Testing", or scan the QR code below:

 Add attention and let us grow together!

Guess you like

Origin blog.csdn.net/weixin_41754309/article/details/112100654