Data structure tutorial 32nd lesson Hash Table (i)

Source: Internet
Author: User

Teaching Purpose: grasping the concept function and significance of hash table, the method of constructing hash table

Teaching Emphases: The method of constructing hash table

Teaching Difficulty: The method of constructing hash table

Teaching Content:

The concept and function of a hash table

In a general linear table, in a tree, the relative position of the record in the structure is random, that is, there is no definite relationship between the key words and the record, so it is necessary to make a series of comparisons with the keywords when searching the records in the structure. This type of lookup method is based on the comparison, and the efficiency of the lookup depends on the number of comparisons made during the lookup.

Ideally, you can directly find the records you need, so you have to establish a definite correspondence f between the storage location of the record and its keywords, so that each keyword and a unique storage location in the structure corresponds.

The most common example of a hash table is the student number of the key to the score sheet, 1th students in the record position in the first, number 10th students record position in the 10th ...

If we take the student name as the keyword, how to establish the lookup table, so that according to the name can directly find the corresponding records?

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st 22 23 24 25 26

Liu Li Liu Hongying Wu Wu Xiaoyan Li Qiumei Mr albert ...
First letter of Pinyin in the name ll Lhy Wj Wxy Lqm cw ...
Add sum with all first letter number values 24 46 33 72 42 26 ...

The minimum value may be 3 maximum may be 78 for 75 students

Using the above obtained values as corresponding records in the table position, get the following table:

Score a Score two ...
3 ...
... ...
24 Liu Li 82 95
25 ...
26 Mr albert
... ...
33 Wu
... ...
42 Li Qiumei
... ...
46 Liu Hongying
... ...
72 Wu Xiaoyan
... ...
78 ...

The table above is a hash.

If you want to check the results of Li Qiumei in the future, you can use the above method to find the location of the record:

Li Qiumei: LQM 12+17+13=42 take the 42nd record in the table.

Question: If two classmates call Liu Liliulan How to deal with these two records separately?

The problem is that a hash table is unavoidable, a conflict phenomenon: the same hash address can be obtained for different keywords.

Two, the construction method of hash table

1. Direct Addressing method

For example: There is a demographic table from 1-100 years old, in which, the age as a keyword, the hash function takes the keyword itself.

Address 01 02 ... 25 26 27 ... 100
Age 1 2 ... 25 26 27 ... ...
Number 3000 2000 ... 1050 ... ... ... ...
...

2. Digital Analysis method

The date of the student's birthday is as follows:

Month, day

75.10.03
75.11.23
76.03.02
76.07.12
75.04.21
76.02.15
...

After analysis, the first, second, third repeat the possibility of large, take these three to create the chance of conflict, so try not to take the first three digits, after three-bit better.

3, Square to take the middle method

The middle number after the keyword squared is the hash address.

4. Folding method

Divide the keywords into the same number of bits (the last part can be different), and then take the superposition of these parts and (the rounding) as a hash address, this method is called folding method.

For example: Every Western book has an international standard book number, it is a 10-bit decimal digit, to use it as a keyword to create a hash table, when the collection of less than 10,000 books, this method can be used to construct a four-digit hash function. If the number of a book is 0-442-20586-4, then:

5864 5864
4220 0224
+) 04 +) 04
----------- -----------
10088 6092
H (Key) =0088 H (Key) =6092
(a) Shift overlay (b) Inter-boundary superposition

5, except the residue method

The remainder is a hash address when the keyword is removed by a number of p that is not more than the hash table long m.

H (key) =key MOD p (p<=m)

6. Random number method

Select a random function, take the keyword's random function value to its hash address, that is,

H (Key) =random (key), where random is a random function. This method is usually used when the keyword length is unequal.

Third, summary

Advantages and disadvantages of hash table

Four, homework

Preview how to deal with conflicts and hash table lookup.

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.