Detailed Hash Table lookup

Source: Internet
Author: User
Hash Table Lookup definition Basic Concepts Implementation Methods 1. Definition

Hash table lookup is also called a hash lookup, by looking for a keyword does not need to compare to obtain a record of the storage location, it is through the storage location of the record and its keywords between the establishment of a definite corresponding relationship F, so that each keyword key corresponding to a storage location F (key). That
-Storage location =f (keywords), where f is a hash function.

1. The best solution for a hash table is to find a record that is equal to the given value.

2, Hachicha find not suitable for the same keyword corresponding to a number of records, such as the use of the keyword "male" to find a classmate.

3, not suitable for the scope of search, such as looking for class 18~22岁 students. 2. Basic Concepts 1, the method of constructing the hash function

What kind of a good hash function.

1, simple calculation. The calculated time of the hash function, which refers to the time the address was generated, should not exceed the time that other lookup techniques compared to the keyword.
2, the address distribution uniformity. Try to distribute the hash address evenly in the storage space so that the space can be used efficiently.

(1) Direct addressable method

We can go to the value of some linear function of the keyword as the hash address, as follows:

F (Key) = A*key + B (where a,b is constant)

The advantage of this hash function is relatively simple, uniform, and does not create conflicts, but need to know the distribution of the keyword in advance, suitable for the lookup table relatively small and continuous situation. is not commonly used in practice.

(2) Digital analysis method

You can use a portion of the keyword to calculate the location of the hash store, such as the back of the phone number (or reverse, move right, and so on).

It is usually suitable for handling the case of a large number of key bits, if you know the distribution of keywords in advance and the number of keywords in the distribution of several bits evenly, you can consider this method.

(3) Excluding residue method

This is the most common method of constructing a hash function, and the hash function for a hash table length of M is:

F (key) =key mod p (p<=m)

MoD is the meaning of modulo (remainder). In fact, if p is not chosen well, it is easy to have a synonym conflict:

As shown in the figure above, select p=11, there will be key=12, 144 of the conflict.

As a rule of thumb, the Johasi table is m, usually p is the smallest prime number less than or equal to the table length, or a composite number that does not contain less than 20 quality factors.
2. Methods of dealing with conflicts

It can be seen from the addition of the remainder method that the design of a good hash function cannot completely avoid conflict (key1). =key2, but F (key1) =f (Key2), here are a few common ways to avoid conflicts.

(1) Open addressable method

The method is to look for the next empty hash address in the event of a conflict, as long as the hash table is large enough, the empty hash address is always found, and its records are stored.

Fi (key) = (f (key) +di) mod m (di=1,2,3......m-1)

Two-time detection method:

Fi (key) = (f (key) +di) mod m (di=1^2, -1^2, 2^2, -2^2......q^2,-q^2, Q<=M/2)

Add square entries, mainly in order not to let the keywords are concentrated in a certain area, to avoid different keywords competing for an address situation.

Random detection method:

Fi (key) = (f (key) +di) mod m (di is a random sequence)

DI is computed using random functions and is an improvement to the previous method.

(2) Chain address method

Stores records with synonyms for all keywords in a single linked list, with pointers to all the synonym child tables in the hash table.

As shown in the figure above, take 12 as a divisor, the addition of residual method, no matter how many conflicts, are only in the current position to add nodes to a single list. 3. Implementation Method 1, first define the structure of some hash table, as well as some related constants.

#define SUCCESS 1
#define UNSUCCESS 0
#define HASHSIZE  //define the length of the hash table as a long array
#define NULLKEY-32768
typedef struct
{
    int *elem;  The base address that the data element stores, dynamically allocating array
    int count;

HashTable;
int m = 0;
2. Initialize the hash table
Status inithashtable (HashTable *h)
{
    int i;
    m = hashsize;
    H->count = m;
    H->elem = (int *) malloc (m*sizeof (int));
    for (i = 0; i < m i++)
        h->elem[i] = Nullkey;
    return OK;

}
3. Define the hash function (address when inserting), where the algorithm can be replaced according to different conditions
int Hash (int key)
{return
    key% m//The remainder method is used here
}
4. Insert operation on hash table
void Inserthash (HashTable *h, int key)
{
    int addr = Hash (key);///////per keyword
    (h->elem[addr)!= NULL Key)//If not NULL, there is a conflict
        addr = (addr + 1)% m//here uses the Open addressing method for linear probing

    h->elem[addr] = key;//Until there is a vacancy and insert
}
5, through the hash table for keyword search process, as shown below
status Searchhash (HashTable H, int key, int *addr) {*addr = Hash (key);  Hash address while (H.ELEM[*ADDR]!= key)//If not NULL, there is a conflict {*addr = (*addr + 1)% m; Development of a addressable linear detection if (h.elem[*addr] = = Nullkey | | | *addr = = Hash (key)) return unsuccess; Keyword does not exist} return SUCCESS; Find successful return} 

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.