Hash table (hash list) Conflict resolution method

Source: Internet
Author: User

The methods of dealing with conflicts can be divided into two main categories: Open Address method and chain address method.

Develop Address method

The basic idea of the open address method is that the records are stored in a hash array, when the initial hash address h0=h (key) of a record keyword key conflicts, based on H0, take the appropriate method to calculate the other address H1, if the H1 still conflict, H1 as the basis for the next address H2 , if the H2 still conflict, then seek H3. And so on, until HK does not conflict, then HK is the hashed address recorded in the table.

This method, when looking for "next" empty hash address, the original array space for all the elements are open, so called open address law. The process of finding the "next" vacancy is often referred to as probing, which can be represented by the following formula

Hi= (H (key) +di)%m i=1,2,..., K (k<=m-1)

where H (key) is the hash function, M is the hash table length, and di is the increment sequence. Depending on the value of Di, the following three detection methods can be divided into:

(1) Linear detection method:

di=1,2,3, ... , m-1

This detection method can be imagined as a circular table, in the event of a conflict, the next cell in the order of the conflict address to find an empty cell, if no empty cell to the last position, then go back to the table header to continue to find, until a space is found, put this element into the empty space, if not find the empty space, The hash list is full and overflow processing is required.

(2) Two-time detection method

Di=1^2, -1^2, 2^2, -2^2,..., +k^2,-k^2 (K<=M/2)

(3) Pseudo-random detection method

di= pseudo-random number sequence

For example, the hash list length is 11, hash function h (key) =key%11, assuming that the table has been filled with keywords 17, 60, 29 records, now has a fourth record, its keyword is 38, by the hash function to obtain a hash address of 5, resulting in a conflict.

If the linear detection method is processed, the next address 6 is still in conflict, and the next address 7 is still in conflict until the location of the hash address 8 is "empty", the process of handling the conflict ends, and 38 fills in the location of the hash list with the ordinal 8.

If the two-time detection method, hash address 5 conflict, get the next address 6, still conflict; to get the next address 4, no conflict, 38 fill in the position of the ordinal 4.

If the pseudo-random detection method is assumed to produce a pseudo-random number of 9, then the next hash address is computed (5+9)%11=3, so 38 fills in the position of the ordinal 3.

A phenomenon can be seen from the process of linear detection: When a record is filled in the i,i+1,i+2 position of the table, the next hash address of I, i+1, i+2, and i+3 will be populated with the i+3 location. This record of two first hash addresses that occur during a conflict process is competing for the same subsequent hash address as "two aggregates (or" stacking "), which adds non-synonym conflicts in the process of dealing with synonyms.

It can be seen that the above three kinds of treatment methods have advantages and disadvantages. The advantage of the linear detection method is that as long as the hash list is not filled, there is always an address that does not conflict: The disadvantage is that there is a "two aggregation" phenomenon. The advantages of two-time aggregation detection method and pseudo-random detection method are as follows: The phenomenon of "two aggregation" can be avoided; The disadvantage is also obvious: there is no guarantee that no conflicting addresses will be found.

Chain Address method

The basic idea of a chain address is to put records with the same hash address in the same single linked list, called the synonym list. There is m hash address there is m single linked list, while using the array ht[0...m-1] to store the head of each linked list of pointers, all the hash address of I records are nodes inserted into the ht[i] as the head node of the single linked list.

  

Hash table (hash list) Conflict resolution method

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.