前面的文章分析了開地址法的其中一種:線性探測再散列,這篇文章來講開地址法的第二種:二次探測再散列
(二)、二次探測再散列
為改善“堆積”問題,減少為完成搜索所需的平均探查次數,可使用二次探測法。
通過某一個散列函數對表項的關鍵碼 x 進行計算,得到桶號,它是一個非負整數。

若設表的長度為TableSize = 23,則在線性探測再散列 舉的例子中利用二次探查法所得到的散列結果如圖所示。

比如輪到放置Blum 的時候,本來應該是位置1,已經被Burke 占據,接着探測 H0 + 1 = 2,,發現被Broad 占據,接着探測 H0 - 1 =
0,發現空位於是放進去,探測次數為3。
下面來看具體代碼實現,跟前面講過的線性探測再散列 差不多,只是探測的方法不同,但使用的數據結構也有點不一樣,此外還實
現了開裂,如果裝載因子 a > 1/2; 則建立新表,將舊表內容拷貝過去,所以hash_t 結構體需要再保存一個size 成員,同樣的原因,
為了將舊表內容拷貝過去,hash_node_t 結構體需要再保存 *key 和 *value 的size。

common.h:
|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#ifndef _COMMON_H_
#define _COMMON_H_ #include <unistd.h> #include <sys/types.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #define ERR_EXIT(m) \ do \ { \ perror(m); \ exit(EXIT_FAILURE); \ } \ while (0) #endif |
hash.h:
|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#ifndef _HASH_H_ #define _HASH_H_ typedef struct hash hash_t; typedef unsigned int (*hashfunc_t)(unsigned int, void *); hash_t *hash_alloc(unsigned int buckets, hashfunc_t hash_func); void hash_free(hash_t *hash); void *hash_lookup_entry(hash_t *hash, void *key, unsigned int key_size); void hash_add_entry(hash_t *hash, void *key, unsigned int key_size, void *value, unsigned int value_size); void hash_free_entry(hash_t *hash, void *key, unsigned int key_size); #endif /* _HASH_H_ */ |
hash.c:
|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
#include "hash.h"
#include "common.h" #include <assert.h> typedef enum entry_status { EMPTY, ACTIVE, DELETED } entry_status_t; typedef struct hash_node { enum entry_status status; void *key; unsigned int key_size; //在拷貝進新的哈希表時有用 void *value; unsigned int value_size; //在拷貝進新的哈希表時有用 } hash_node_t; struct hash { unsigned int buckets; unsigned int size; //累加,如果size > buckets / 2 ,則需要開裂建立新表 hashfunc_t hash_func; hash_node_t *nodes; }; unsigned int next_prime(unsigned int n); int is_prime(unsigned int n); unsigned int hash_get_bucket(hash_t *hash, void *key); hash_node_t *hash_get_node_by_key(hash_t *hash, void *key, unsigned int key_size); hash_t *hash_alloc(unsigned int buckets, hashfunc_t hash_func) { hash_t *hash = (hash_t *)malloc(sizeof(hash_t)); //assert(hash != NULL); hash->buckets = buckets; hash->hash_func = hash_func; int size = buckets * sizeof(hash_node_t); hash->nodes = (hash_node_t *)malloc(size); memset(hash->nodes, 0, size); printf("The hash table has allocate.\n"); return hash; } void hash_free(hash_t *hash) { unsigned int buckets = hash->buckets; int i; for (i = 0; i < buckets; i++) { if (hash->nodes[i].status != EMPTY) { free(hash->nodes[i].key); free(hash->nodes[i].value); } } free(hash->nodes); printf("The hash table has free.\n"); } void *hash_lookup_entry(hash_t *hash, void *key, unsigned int key_size) { hash_node_t *node = hash_get_node_by_key(hash, key, key_size); if (node == NULL) { return NULL; } return node->value; } void hash_add_entry(hash_t *hash, void *key, unsigned int key_size, void *value, unsigned int value_size) { if (hash_lookup_entry(hash, key, key_size)) { fprintf(stderr, "duplicate hash key\n"); return; } unsigned int bucket = hash_get_bucket(hash, key); unsigned int i = bucket; unsigned int j = i; int k = 1; int odd = 1; while (hash->nodes[i].status == ACTIVE) { if (odd) { i = j + k * k; odd = 0; // i % hash->buckets; while (i >= hash->buckets) { i -= hash->buckets; } } else { i = j - k * k; odd = 1; while (i < 0) { i += hash->buckets; } ++k; } } hash->nodes[i].status = ACTIVE; if (hash->nodes[i].key) ////釋放原來被邏輯刪除的項的內存 { free(hash->nodes[i].key); } hash->nodes[i].key = malloc(key_size); hash->nodes[i].key_size = key_size; //保存key_size; memcpy(hash->nodes[i].key, key, key_size); if (hash->nodes[i].value) //釋放原來被邏輯刪除的項的內存 { free(hash->nodes[i].value); } hash->nodes[i].value = malloc(value_size); hash->nodes[i].value_size = value_size; //保存value_size; memcpy(hash->nodes[i].value, value, value_size); if (++(hash->size) < hash->buckets / 2) return; //在搜索時可以不考慮表裝滿的情況; //但在插入時必須確保表的裝填因子不超過0.5。 //如果超出,必須將表長度擴充一倍,進行表的分裂。 unsigned int old_buckets = hash->buckets; hash->buckets = next_prime(2 * old_buckets); hash_node_t *p = hash->nodes; unsigned int size; hash->size = 0; //從0 開始計算 size = sizeof(hash_node_t) * hash->buckets; hash->nodes = (hash_node_t *)malloc(size); memset(hash->nodes, 0, size); for (i = 0; i < old_buckets; i++) { if (p[i].status == ACTIVE) { hash_add_entry(hash, p[i].key, p[i].key_size, p[i].value, p[i].value_size); } } for (i = 0; i < old_buckets; i++) {
// active or deleted if (p[i].key) { free(p[i].key); } if (p[i].value) { free(p[i].value); } } free(p); //釋放舊表 } void hash_free_entry(hash_t *hash, void *key, unsigned int key_size) { hash_node_t *node = hash_get_node_by_key(hash, key, key_size); if (node == NULL) return; // 邏輯刪除 node->status = DELETED; } unsigned int hash_get_bucket(hash_t *hash, void *key) { unsigned int bucket = hash->hash_func(hash->buckets, key); if (bucket >= hash->buckets) { fprintf(stderr, "bad bucket lookup\n"); exit(EXIT_FAILURE); } return bucket; } hash_node_t *hash_get_node_by_key(hash_t *hash, void *key, unsigned int key_size) { unsigned int bucket = hash_get_bucket(hash, key); unsigned int i = 1; unsigned int pos = bucket; int odd = 1; unsigned int tmp = pos; while (hash->nodes[pos].status != EMPTY && memcmp(key, hash->nodes[pos].key, key_size) != 0) { if (odd) { pos = tmp + i * i; odd = 0; // pos % hash->buckets; while (pos >= hash->buckets) { pos -= hash->buckets; } } else { pos = tmp - i * i; odd = 1; while (pos < 0) { pos += hash->buckets; } i++; } } if (hash->nodes[pos].status == ACTIVE) { return &(hash->nodes[pos]); } // 如果運行到這里,說明pos為空位或者被邏輯刪除 // 可以證明,當表的長度hash->buckets為質數且表的裝填因子不超過0.5時, // 新的表項 x 一定能夠插入,而且任何一個位置不會被探查兩次。 // 因此,只要表中至少有一半空的,就不會有表滿問題。 return NULL; } unsigned int next_prime(unsigned int n) { // 偶數不是質數 if (n % 2 == 0) { n++; } for (; !is_prime(n); n += 2); // 不是質數,繼續求 return n; } int is_prime(unsigned int n) { unsigned int i; for (i = 3; i * i <= n; i += 2) { if (n % i == 0) { // 不是,返回0 return 0; } } // 是,返回1 return 1; } |
main.c:
|
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
#include "hash.h" #include "common.h" typedef struct stu { char sno[5]; char name[32]; int age; } stu_t; typedef struct stu2 { int sno; char name[32]; int age; } stu2_t; unsigned int hash_str(unsigned int buckets, void *key) { char *sno = (char *)key; unsigned int index = 0; while (*sno) { index = *sno + 4 * index; sno++; } return index % buckets; } unsigned int hash_int(unsigned int buckets, void *key) { int *sno = (int *)key; return (*sno) % buckets; } int main(void) { stu2_t stu_arr[] = { { 1234, "AAAA", 20 }, { 4568, "BBBB", 23 }, { 6729, "AAAA", 19 } }; hash_t *hash = hash_alloc(256, hash_int); int size = sizeof(stu_arr) / sizeof(stu_arr[0]); int i; for (i = 0; i < size; i++) { hash_add_entry(hash, &(stu_arr[i].sno), sizeof(stu_arr[i].sno), &stu_arr[i], sizeof(stu_arr[i])); } int sno = 4568; stu2_t *s = (stu2_t *)hash_lookup_entry(hash, &sno, sizeof(sno)); if (s) { printf("%d %s %d\n", s->sno, s->name, s->age); } else { printf("not found\n"); } sno = 1234; hash_free_entry(hash, &sno, sizeof(sno)); s = (stu2_t *)hash_lookup_entry(hash, &sno, sizeof(sno)); if (s) { printf("%d %s %d\n", s->sno, s->name, s->age); } else { printf("not found\n"); } hash_free(hash); return 0; } |
simba@ubuntu:~/Documents/code/struct_algorithm/search/hash_table/quardratic_probing$ ./main
The hash table has allocate.
4568 BBBB 23
not found
The hash table has free.
