Hashtable Hashtable is one of the most common and most basic data structures in computers, but some learners with weak CS foundation actually know little about this structure and are always confused. For these friends, try a new way - from the creator's point of view, reinvent the structure yourself. The idea sounds strange, isn't it harder? Not really, it's all about rhythm. As long as you sort out your thinking clearly and break down the problem step by step, you will see a natural and clear path.
From the point of view of invention, we only need to ask two things:
(1) What problem do we want to solve?
(2) How can this problem be best solved?
1
Motivation: Ubiquitous queries
In the program, when it is often necessary to "look up the table", it is to use some form of identification (key) to query the relevant information (value) corresponding to the identification:
Save a user name to the lookup table of user information for future reference
Count the word frequency of a piece of text: Then you have to establish a correspondence from each word to the number of occurrences, and when you scan the text, if you see a word, add its frequency +1
For a function f(n) with high computational cost, if you want to avoid duplication of work for the already calculated results, you need a table to store all the calculated n and their corresponding calculation results.
Such "keyword query" requirements are ubiquitous in the process of program design, so we need an efficient mechanism to store the correspondence between keys and values.
2
problem
Task: Design an efficient method to query the corresponding value from the key.
Raw material: Since the hashtable is too basic, each language will have its own structure to provide this function, but since we are redesigning it, we can only assume that the hashtable that comes with these languages ​​does not exist. Your raw material is the most basic, the array array.
3
stupid way
Let’s start with the most basic solution, first to help understand the problem to be solved, and second to compare the advantages of the final hashtable.
For example, a stupid way is to directly stack the pair of (key, value) to be stored in the array
Every time a key is queried, the array is scanned from the beginning to the end, and the key is found, and then it stops. This method at least satisfies the correctness, but it is relatively slow; because the entire array is scanned linearly, the time cost will be very high if the total number of keys in it exceeds.
4
The simplest problem: Arrays are queries
Back to our design question. But if you think about it, the array can be regarded as a kind of query - the key is a query of 0..N-1 - enter the subscript i, and the array a[i] tells you the value corresponding to the subscript.
And this also fits our efficiency needs: queries and modifications are very fast. Of course, this is because the array is contiguous memory, and the subscript is the memory address, so 0..N-1 is particularly simple and direct to query compared to other keys.
Then, what we need to ask is, how to make any form of key can enjoy almost the same efficiency as the special 0..N-1 subscript query? One idea is to find a way to efficiently convert the key to a subscript of 0..N-1.
5
Subscript conversion: key='a'..'z'
Example/advanced question: For example, if we want to do a simple text encryption, we pre-specify the encryption relationship a -> x, b -> o, c -> p, ..., write this relationship into a lookup table, and later When encrypting, just read the original text letter by letter, find out the ciphertext, and the letter will be converted. For example the word "cab" becomes "pxo".
Analysis: 'a'..'z' is not an integer 0..N-1, but it's almost the same...
One solution: of course, 'a'..'z' correspond to 0..25 in order
When querying any lowercase letter ch, first calculate the subscript i = ch - 'a', then you can access a[i], and find the ciphertext corresponding to the plaintext letter in the array.
6
General transformation: hash function
Following the example of the above letter, we can think in general terms. The process of 'a'..'z' corresponding to 0..N-1 just now, if it can be done for any key. And this corresponding mechanism is called the hash function hash function: if we can provide a hash function hash for a certain key type (such as a string), and bring the key in to output an integer hash(key), then we can Use it as a raw material to generate array subscripts.
Usually we make hash(key) larger, of course, the array space is actually only N, so take a remainder and use hash(key) % N as the final array subscript. For example, hash(key0) % N = 3, then save it as
Of course this is only an ideal situation. If we observe the correspondence of 'a'..'z' → 0..25, we will find that it has a particularity, which is "one radish and one pit": letters and subscripts correspond one-to-one. However, in a more general case, under the double operation of hash function and remainder taking, sometimes it is inevitable that "many radishes want to go to the same pit" situation. How to solve it? People usually think of two ways:
"Dig the pit deep": If several keys correspond to the same array subscript, then pile these radishes into the pit. Specifically, the subscript of each array is not a pair of (key, value), but a whole list with several (key, value) - this list is actually similar to the "stupid method" we first proposed. ". First use the key and hash function to find the pit, and then query in the pit degenerates into our "stupid way".
"First come, first arrive, and the radish that arrives later will find another pit to occupy": each pit still only accommodates one radish; if a radish finds that the pit it should go to has been occupied, it can define a set of rules to help it find the next pit , again and again until an empty pit is found.
7
retrospect and imagination
Above, we have finally completed the design of the general framework of hashtable. Having figured out these internal principles, we can start to have some imagination about its operation, also as a kind of retrospective.
First, the basic process of the query is shown in the figure below. Enter the key, calculate the array subscript through the hash function, and then search for the specific key in the pit corresponding to the array subscript.
In an ideal situation, the hash function can successfully disperse the keys into the array subscripts evenly, and the array space is relatively abundant, so the subscript conflicts of the keys are not much, and adding, modifying, and querying elements can be done through the hash function. O(1) time; Occasionally encounter some subscript conflicts, but the frequency is low, and the desired element can still be found after two more steps, so the overall average is roughly considered to be constant time.
On the other hand, if the hash function is not well designed, resulting in a lot of subscript conflicts, in the extreme case, all the radishes may fall into a pit, and after a long time, you may still degenerate into the "stupid method" we first proposed. Or if the array space is insufficient, and there are too many (key, value) stored in it, there may be a lot of subscript conflicts, which is very bloated and cannot achieve the expected efficiency.
The stadium LED screen is ideal for places of with huge crowd. Its high resolution picture and wide viewing angle ensure best quality pictures and videos to every corner of the stadium. Stable body protects the display from any damage, even high speed ball.
The LED Display is manufactured by using best quality material, purchased from most reputed vendors. The final products feature supreme quality and offers best outputs. High refresh rate and gray scale ensures the picture quality remains seamless.
The display features a wide viewing angle of 120° horizontal and 120° in vertical which reaches more viewers. The picture quality remains seamless in all direction and at a distance, presenting all viewers the same highly quality outputs. This makes it suitable for large gathering. -
Stadium LED Display,Football Led Display,Staduim Led Display,Outdoor Football Led Display
Guangzhou Chengwen Photoelectric Technology co.,ltd , https://www.cwstagelight.com