冲突处理方法
- 常见处理冲突的思路:
- 换个位置:开放地址法
- 同一位置的冲突对象组织在一起:链地址法
- 开放定址法(Open Addressing)
- 一旦产生了冲突(该地址已有其他元素),就按某种规则去寻找另一空地址
- h i ( k e y ) = ( h ( k e y ) + d i ) m o d T a b l e S i z e h_i(key)=(h(key)+d_i)mod \space TableSize hi(key)=(h(key)+di)mod TableSize
- d i d_i di决定了不同的解决冲突方案:线性探测( d i = i d_i=i di=i)、平方探测( d i = ± i 2 d_i=\pm i^2 di=±i2)、双散列( d i = i ∗ h 2 ( k e y ) d_i=i*h_2(key) di=i∗h2(key))
1. 线性探测法
- 以增量序列1,2,…,(TableSize-1)循环试探下一个存储地址。
- 注意“聚集”现象
散列表查找性能分析
- 成功平均查找长度(ASLs)
- 不成功平均查找长度(ASLu)
2. 平方探测法
- 以增量序列 1 2 , − 1 2 , 2 2 , − 2 2 , . . . , q 2 , − q 2 1^2,-1^2,2^2,-2^2,...,q^2,-q^2 12,−12,22,−22,...,q2,−q2且 q ≤ ⌊ T a b l e S i z e / 2 ⌋ q\leq \lfloor TableSize/2\rfloor q≤⌊TableSize/2⌋循环试探下一个存储地址。
- 有定理显示:如果散列表长度TableSize是某个4k+3(k是正整数)形式的素数时,平方探测法就可以探测到整个散列表空间。
HashTable InitializeTable( int TableSize )
{
HashTable H;
int i;
if( TableSize < MinTableSize ) {
Error("散列表太小");
return NULL;
}
//分配散列表
H = (HashTable)malloc( sizeof( struct HashTbl ));
if(H == NULL)
FatalError("空间溢出");
H->TableSize = NextPrime( TableSize );
//分配散列表Cells
H->TheCells = (Cell *)malloc(sizeof(Cell)*H->TableSize);
if(H->TheCells == NULL)
FatalError("空间溢出");
for(i=0; i<H->TableSize; i++)
H->TheCells[i].info = Empty;
return H;
}
typedef struct
HashTbl *HashTable;
struct HashTbl{
int TableSize;
Cell *TheCells;
}H;
Position Find( ElementType Key, HashTable H )//平方探测
{
Position CurrentPos, NewPos;
int CNum; //记录冲突次数
CNum = 0;
NewPos = CurrentPos = Hash( Key, H->TableSize );
while(Position Find( HashTable H, ElementType Key )
{
Position CurrentPos, NewPos;
int CNum = 0; /* 记录冲突次数 */
NewPos = CurrentPos = Hash( Key, H->TableSize ); /* 初始散列位置 */
/* 当该位置的单元非空,并且不是要找的元素时,发生冲突 */
while( H->Cells[NewPos].Info!=Empty && H->Cells[NewPos].Data!=Key ) {
/* 字符串类型的关键词需要 strcmp 函数!! */
/* 统计1次冲突,并判断奇偶次 */
if( ++CNum%2 ){ /* 奇数次冲突 */
NewPos = CurrentPos + (CNum+1)*(CNum+1)/4; /* 增量为+[(CNum+1)/2]^2 */
if ( NewPos >= H->TableSize )
NewPos = NewPos % H->TableSize; /* 调整为合法地址 */
}
else { /* 偶数次冲突 */
NewPos = CurrentPos - CNum*CNum/4; /* 增量为-(CNum/2)^2 */
while( NewPos < 0 )
NewPos += H->TableSize; /* 调整为合法地址 */
}
}
return NewPos; /* 此时NewPos或者是Key的位置,或者是一个空单元的位置(表示找不到)*/
}
)
}
bool Insert( HashTable H, ElementType Key )
{
Position Pos = Find( H, Key ); /* 先检查Key是否已经存在 */
if( H->Cells[Pos].Info != Legitimate ) { /* 如果这个单元没有被占,说明Key可以插入在此 */
H->Cells[Pos].Info = Legitimate;
H->Cells[Pos].Data = Key;
/*字符串类型的关键词需要 strcpy 函数!! */
return true;
}
else {
printf("键值已存在");
return false;
}
}
3. 双散列探测法
h 2 ( k e y ) = p − ( k e y m o d p ) h_2(key)= p-(key\space mod \space p) h2(key)=p−(key mod p)
其中 p ≤ T a b l e S i z e , p 、 T a b l e S i z e p\leq TableSize,p、TableSize p≤TableSize,p、TableSize都是素数。
4. 再散列
- 当散列表元素太多时(即装填因子太大),查找效率会下降;
- 实用装填因子一般取 0.5 ≤ α ≤ 0.85 0.5\leq\alpha\leq0.85 0.5≤α≤0.85
- 当装填因子过大时,解决的方法是加倍扩大散列表,这个过程叫做“再散列”
5. 分离链接法
- 将相应位置上冲突的所有关键词存储在同一个单链表中
#define KEYLENGTH 15 /* 关键词字符串的最大长度 */
typedef char ElementType[KEYLENGTH+1]; /* 关键词类型用字符串 */
typedef int Index; /* 散列地址类型 */
/******** 以下是单链表的定义 ********/
typedef struct LNode *PtrToLNode;
struct LNode {
ElementType Data;
PtrToLNode Next;
};
typedef PtrToLNode Position;
typedef PtrToLNode List;
/******** 以上是单链表的定义 ********/
typedef struct TblNode *HashTable; /* 散列表类型 */
struct TblNode { /* 散列表结点定义 */
int TableSize; /* 表的最大长度 */
List Heads; /* 指向链表头结点的数组 */
};
HashTable CreateTable( int TableSize )
{
HashTable H;
int i;
H = (HashTable)malloc(sizeof(struct TblNode));
/* 保证散列表最大长度是素数,具体见代码5.3 */
H->TableSize = NextPrime(TableSize);
/* 以下分配链表头结点数组 */
H->Heads = (List)malloc(H->TableSize*sizeof(struct LNode));
/* 初始化表头结点 */
for( i=0; i<H->TableSize; i++ ) {
H->Heads[i].Data[0] = '\0';
H->Heads[i].Next = NULL;
}
return H;
}
Position Find( HashTable H, ElementType Key )
{
Position P;
Index Pos;
Pos = Hash( Key, H->TableSize ); /* 初始散列位置 */
P = H->Heads[Pos].Next; /* 从该链表的第1个结点开始 */
/* 当未到表尾,并且Key未找到时 */
while( P && strcmp(P->Data, Key) )
P = P->Next;
return P; /* 此时P或者指向找到的结点,或者为NULL */
}
bool Insert( HashTable H, ElementType Key )
{
Position P, NewCell;
Index Pos;
P = Find( H, Key );
if ( !P ) { /* 关键词未找到,可以插入 */
NewCell = (Position)malloc(sizeof(struct LNode));
strcpy(NewCell->Data, Key);
Pos = Hash( Key, H->TableSize ); /* 初始散列位置 */
/* 将NewCell插入为H->Heads[Pos]链表的第1个结点 */
NewCell->Next = H->Heads[Pos].Next;
H->Heads[Pos].Next = NewCell;
return true;
}
else { /* 关键词已存在 */
printf("键值已存在");
return false;
}
}
void DestroyTable( HashTable H )
{
int i;
Position P, Tmp;
/* 释放每个链表的结点 */
for( i=0; i<H->TableSize; i++ ) {
P = H->Heads[i].Next;
while( P ) {
Tmp = P->Next;
free( P );
P = Tmp;
}
}
free( H->Heads ); /* 释放头结点数组 */
free( H ); /* 释放散列表结点 */
}
- 已知散列表元素状态,推测可能的元素输入顺序
- 无法推测元素的输入顺序