To put it simply, hash is to find a ing between the data content and the data storage address. For example, if you want to store several strings in a Hasse table and want to locate a specific string in the table at O (1) time, we can use arrays to implement the Hasse table, find a function, Sting-> integer, as P = f (s), where p is an integer, S is a string, and P is the subscript of string s in the array. In this way, if you need to set the s position in the array, you only need to directly calculate the s position based on the function P = f (s. Adding a string to the Hasse table is similar. Calculate the location where the string should be stored in the array based on the value of the string, and then place the string in. However, it is difficult to find such a function (as a Hasse function). It is almost impossible to find a one-to-one function. Therefore, we often use a non-. For example, for the above example, we can design a simple Hasse function. We set F (s) to define the ASCII code of each character of S and divide by the remainder of N, here N is the length of our array. We assume that a maximum of n elements need to be stored in the Hasse table. However, this Hasse function has an obvious disadvantage. For example, for strings S1 = "ABC" and S2 = "ACB", the value of the Hasse function is obviously the same, however, only one element can be stored in one location. If S1 is first placed in position P1 Of The Hasse table, and S2 is then placed in the Hasse table, this is because P2 = f (S2) = p1, so the position where S2 should be placed has been occupied by S1, so there is a problem. This is called "conflict ". A simple solution to this conflict is that, because P1 has been occupied by S1, we can see P1 + 1. If this position is null, It will be placed in S2, otherwise, you can continue watching P1 + 2 ,...... Always find a vacant space. Suppose we put S2 in P1 + 1, but we need to add S3 at this time, and F (S3) is exactly equal to p1 + 1, and S3 is occupied by S2 again, we can continue to look at P1 + 2, P1 + 3 ...... Whether it is empty until a vacant space is found and put into S3. When looking for S2, we first calculate that S2 should be at the P1 position based on F (S2). Then we can see the elements at the P1 position, we found that it was not S2 (S1 at this position), so we continue to look at P1 + 1, P1 + 2 ,...... Until S2 is found, or at the end of the table, or a vacant space is found, the end can be aborted. The last two cases indicate that S2 is not in the table. Obviously, if there are too many conflicts, the efficiency of the Hasse table will decrease. In fact, the Hasse function in the example I just mentioned is very bad, so there is a high possibility of conflict. If you find a better Hasse function, the efficiency of the table is still very high. As for the method for finding the Hasse function, we need to analyze it based on the specific data type and application scenarios. There are also some principles. Here we will not describe them one by one ~~
--- From csdn Documentation Center
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service