演算法導論11(雜湊表)

2021-06-26 08:51:41 字數 2216 閱讀 4065

11.1 直接定址表

struct node

;node *directaddresssearch(node **t,int k)

void directaddressinsert(node **t,node *x)

void directaddressdelete(node **t,node *x)

11.2 雜湊表

通過鏈結法解決衝突

(1)單鏈表

struct node

;void chainedhashinsert(node **t,node *x)

node *chainedhashsearch(node **t,int k)

return null;

}void chainedhashdelete(node **t,node *x)

else //x是尾結點

else //x不是頭節點

p->next=q->next;

delete q;

} }}

(2)雙向鍊錶

struct node

;void chainedhashinsert(node **t,node *x)

node *chainedhashsearch(node **t,int k)

return null;

}void chainedhashdelete(node **t,node *x)

else if(!x->prev) //x是頭節點,不是尾結點

else if(!x->next) //x是尾結點,不是頭節點

else //x既不是頭節點,也不是尾結點

}

11.3 雜湊函式

//除法雜湊法

int h(int k,int m)

//乘法雜湊法

int h(int k,int m,double a)

//全域雜湊法

//設計乙個全域雜湊函式類,1<=a<=p-1,0<=b<=p-1

int h(int k,int m,int a,int b,int p)

11.4 開放定址法

//沒有刪除操作

const int nil=-1;

int hashinsert(int *t,int m,int k)

else ++i;

}while(i!=m);

return nil;

} int hashsearch(int *t,int m,int k)

while(t[j]!=nil&&i!=m);

return nil;

} //有刪除操作

const int nil=-1,deleted=-2;

int hashinsert(int *t,int m,int k)

++i;

}while(i!=m);

return nil;

} int hashsearch(int *t,int m,int k)

while(t[j]!=nil&&i!=m);

return nil;

}int hashdelete(int *t,int m,int k)

++i;

}while(t[j]!=nil&&i!=m);

return nil;

}

//線性探查

h(k,i)=(h1(k)+i)%m

//二次探查

h(k,i)=(h1(k)+c1*i+c2*i*i)%m

//雙重探查

h(k,i)=(h1(k)+i*h2(k))%m

《演算法導論》雜湊表

雜湊表 hash table 是實現字典操作 查詢 插入 刪除 的有效資料結構,具有很高的查詢效率。一定情況下查詢元素的期望時間是o 1 優於鍊錶 o n 和直接定址的陣列 查詢也是o 1 相當。但當實際儲存的關鍵字數比可能的關鍵字總數小的時候,雜湊錶比陣列有效。一些記號 直接定址,關鍵字k存放在第...

演算法導論 雜湊表

include include includeusing namespace std const int length 6 待插入的數列長度 const int tablesize 11 雜湊表的容量 應該打大於數列長度以保證每個元素都有處可放 typedef struct hash hashtab...

演算法導論 雜湊表

華電北風吹 天津大學認知計算與應用重點實驗室 日期 2015 9 8 雜湊表 hash table 是一種支援高效插入,查詢,刪除操作的動態集合結構。並且在滿足一些合理假設下,這些操作的平均時間複雜度可以達到 1 這也是雜湊表能夠和二叉搜尋樹 紅黑樹 抗衡的乙個重要方面。一 直接定址表 當關鍵字集合...