I. Hash Table
1. Basic Ideas
With the keyword K of each record in the data table as an independent variable, the function value is calculated by a function H(k). Connect this to an array: the address of a consecutive storage space (array space), i.e. an array subscript, stores records in the cell. Then we call this function hash function (hash function), the value of the function is hash address, and the table created is hash table (hash table).
2. Conflict
Different keywords correspond to a storage address, k1!=k2, but the phenomenon of H(k1) = H(k2) is called conflict.
3. Synonyms
Objects with different keywords and the same hash address are called synonyms.
4. Conflict is unavoidable in most cases, mainly because of the larger set of keywords and the fewer hash addresses.
5. Two problems of hash technology are studied: (1) How to design hash functions to minimize conflicts as much as possible. (2) How to resolve conflicts after they occur.
2. Construction Method of Hash Function
Conflict processing is to find another "empty" hash address for the record of the keyword, then get a new hash address through a new hash function, if there is still a conflict, then get the next one until no conflict occurs.
III. Conflict Resolution
4. Search and Performance Analysis of Hash Table
Factors related to the likelihood of conflict
1. Relating to filling factor
The so-called filling factor is the ratio of the number of elements n stored in the hash table to the length m of the hash table, that is, a = n / m. When a decreases, the possibility of conflict decreases, and vice versa, the possibility of conflict increases (a maximum is 1).
2. Relating to the constructed hash function
If the hash function is selected properly, the hash addresses can be evenly distributed in the hash address space as far as possible, thus reducing the possibility of conflict. Otherwise, if the hash function is not selected properly, the hash addresses may be concentrated in some areas, thus increasing the possibility of conflict.
3. Hash Conflict Function for Conflict Resolution
The choice of hash collision function will also reduce or increase the possibility of collision.
5. Creation of Hash Table
1. Types of Designing Hash Tables
#define MaxSize 100 #Define NULLKEY-1//Define the value of empty keywords typedef int KeyType; //Types of keywords typedef char *InforType; //Other types typedef struct node { KeyType key; //Keyfield InforType data; // Other data domains int count; //Number of explorations }HashType[MaxSize];
2. Insert keywords into hash tables
//Insert keywords into hash tables void InsertHT(HashType ha,int n,KeyType k,int p) { int adr,i; adr = k % p; if(ha[adr].key == NULLKEY) { ha[adr].key = k; //x[i] can be inserted directly ha[adr].count = 1; } else //In case of conflict, use linear probing method to solve it { i = 1; //Record the number of conflicts do{ adr = (adr + 1) % p; i++; }while(ha[adr].key != NULLKEY); ha[adr].key = k; ha[adr].count = i; } n++; }
3. Create hash tables
//Create hash tables void CreatHT(HashType ha, KeyType x[],int n,int m,int p) { int i,n1 = 0; for(i = 0;i < m;i++) { ha[i].key = NULLKEY; //Hash table initialization ha[i].count = 0; } for(i = 0;i < n;i++) { InsertHT(ha,n1,x[i],p); } }
4. Finding keywords
//Find keywords int SearchHT(HashType ha,int p,KeyType k) { int i = 0,adr; adr = k % p; while(ha[adr].key != NULLKEY && ha[adr].key != k) { i++; adr = (adr + 1) % p; //Linear Exploration Search } if(ha[adr].key == k) //Find Success { return adr; } else //Finding Failure { return -1; } }
5. Find successfully and find the average search length
//When the search is successful, find the average search length void CompASL(HashType ha,int m) { int i; int s = 0,n = 0; for(i = 0;i < m;i++) { if(ha[i].key != NULLKEY) { s = s + ha[i].count; n++; } } printf("Find Successful ASL=%.3f\n",s * 1.0 / n); }
6. Output hash table
//Output hash table void DisHT(HashType ha,int n,int m) { float avg = 0; int i; printf("hash address: "); for(i = 0;i < m;i++) { printf("%3d",i); } printf("\n"); printf("Hash table keywords:"); for(i = 0;i < m;i++) { if(ha[i].key == NULLKEY) { printf(" "); } else { printf("%3d",ha[i].key); } } printf("\n"); printf("Search times: "); for(i = 0;i < m;i++) { if(ha[i].key == NULLKEY) { printf(" "); } else { printf("%3d",ha[i].count); } } printf("\n"); for(i = 0;i < m;i++) { if(ha[i].key != NULLKEY) { avg = avg + ha[i].count; } } avg = avg / n; printf("The average search length is ASL(%d)=%.3f\n",n,avg); }
Complete program code:
/***************************************************** copyright (C), 2014-2015, Lighting Studio. Co., Ltd. File name: Author: Jerey_Jobs Version:0.1 Date: Description: Funcion List: *****************************************************/ #include <stdio.h> #define MaxSize 100 #Define NULLKEY-1//Define the value of empty keywords typedef int KeyType; //Types of keywords typedef char *InforType; //Other types typedef struct node { KeyType key; //Keyfield InforType data; // Other data domains int count; //Number of explorations }HashType[MaxSize]; //Insert keywords into hash tables void InsertHT(HashType ha,int n,KeyType k,int p) { int adr,i; adr = k % p; if(ha[adr].key == NULLKEY) { ha[adr].key = k; //x[i] can be inserted directly ha[adr].count = 1; } else //In case of conflict, use linear probing method to solve it { i = 1; //Record the number of conflicts do{ adr = (adr + 1) % p; i++; }while(ha[adr].key != NULLKEY); ha[adr].key = k; ha[adr].count = i; } n++; } //Create hash tables void CreatHT(HashType ha, KeyType x[],int n,int m,int p) { int i,n1 = 0; for(i = 0;i < m;i++) { ha[i].key = NULLKEY; //Hash table initialization ha[i].count = 0; } for(i = 0;i < n;i++) { InsertHT(ha,n1,x[i],p); } } //Find keywords int SearchHT(HashType ha,int p,KeyType k) { int i = 0,adr; adr = k % p; while(ha[adr].key != NULLKEY && ha[adr].key != k) { i++; adr = (adr + 1) % p; //Linear Exploration Search } if(ha[adr].key == k) //Find Success { return adr; } else //Finding Failure { return -1; } } //When the search is successful, find the average search length void CompASL(HashType ha,int m) { int i; int s = 0,n = 0; for(i = 0;i < m;i++) { if(ha[i].key != NULLKEY) { s = s + ha[i].count; n++; } } printf("Find Successful ASL=%.3f\n",s * 1.0 / n); } //Output hash table void DisHT(HashType ha,int n,int m) { float avg = 0; int i; printf("hash address: "); for(i = 0;i < m;i++) { printf("%3d",i); } printf("\n"); printf("Hash table keywords:"); for(i = 0;i < m;i++) { if(ha[i].key == NULLKEY) { printf(" "); } else { printf("%3d",ha[i].key); } } printf("\n"); printf("Search times: "); for(i = 0;i < m;i++) { if(ha[i].key == NULLKEY) { printf(" "); } else { printf("%3d",ha[i].count); } } printf("\n"); for(i = 0;i < m;i++) { if(ha[i].key != NULLKEY) { avg = avg + ha[i].count; } } avg = avg / n; printf("The average search length is ASL(%d)=%.3f\n",n,avg); } int main() { int x[] = {16,74,60,43,54,90,46,31,29,88,77}; int n,m,p,k,i; n = 11; m = 13; k = 29; p = 13; HashType ha; CreatHT(ha,x,n,m,p); printf("\n"); DisHT(ha,n,m); i = SearchHT(ha,p,k); if(i == -1) { printf("To find%d\n",k); } else { printf("ha[%d],key=%d\n",i,k); } printf("\n"); k = 66; InsertHT(ha,n,k,p); DisHT(ha,n,m); printf("\n"); return 0; }