Data structure tutorial 33rd Lesson Hash Table (ii)

Source: Internet
Author: User
Tags count hash

Teaching Purpose: grasping the method of hash table handling conflict and finding algorithm of hash table

Teaching emphases: a Hash table handling conflict method

Teaching Difficulty: Open addressing method

Teaching Content:

First, review the last lesson content

What is a hash table? How do I construct a hash table?

Ask a question: how do I handle conflicts?

Ii. methods of dealing with conflicts

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 ...

If two students are called Liu Liliulan, when the Liulan, address 24 conflict, we can use a certain pattern of other storage location, if you choose a different location still have a conflict, then select the next, until you find a location where there is no conflict. Methods for selecting a different location are:

1. Open Addressable method

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

where M is the table length and di is the increment sequence

If the DI value is likely to be a 1,2,3,... m-1, it is called a linear probe hash.

If di value may be 1,-1,2,-2,4,-4,9,-9,16,-16,... k*k,-k*k (K<=M/2)

Weigh two times to detect and then hash.

If di can be a pseudo random sequence. Called pseudo random probing and hashing.

Example: in length 11 hash table has filled in the key word is 17,60,29 record, the existing fourth record, its key word is 38, by the hash function obtains the address to be 5, if uses the linear detection again hashes, as follows:

1 2 4 5 7 8 10
60 17 29

(a) before insertion

1 2 4 5 7 8 10
60 17 29 38

(b) linear probing and hashing

1 2 4 5 7 8 10
60 17 29

(c) Two probes and hashes

1 2 4 5 7 8 10
38 60 17 29

(d) pseudo random probing and hashing

Pseudo-random sequence is 9,5,3,8,1 ...

2. Re-hashing method

When a conflict occurs, the second, third, and hash functions are used to compute the address until there is no conflict. Disadvantage: The calculation time increases.

3. Chain Address method

Records all keywords as synonyms are stored in the same linear list.

4. Create a public overflow area

Assuming that the domain of the hash function is [0,m-1], set the vector hashtable[0..m-1] as the base table, and set up a storage space vector OVERTABLE[0..V] to store the conflicting records.

Third, the search for a hash table

Storage structure of open addressable hash table

int hashsize[]={997,...};

typedef struct{

Elemtype *elem;

int count;

int sizeindex;

}hashtable;

#define SUCCESS 1

#define UNSUCCESS 0

#define DUPLICATE-1

Status Searchhash (HashTable h,keytype k,int &p,int &c) {

P=hash (K);

while (H.elem[p].key!=nullkey &&!) EQ (K,h.elem[p].key))

Collision (P,++C);

if (EQ (K,h.elem[p].key)

return SUCCESS;

else return unsuccess;

}

Status Inserthash (HashTable &h,eletype e) {

c=0;

if (Searchhash (h,e.key,p,c))

return DUPLICATE;

else if (C<HASHSIZE[H.SIZEINDEX]/2) {

H.elem[p]=e; ++h.count; return OK;

}

else recreatehashtable (H);

}

Iv. Summary

What are the requirements for dealing with conflicts?

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.