第五章 雜湊

2021-06-14 13:27:42 字數 1879 閱讀 9683

雜湊表adt,只支援二叉樹查詢所允許的一部分操作。比如插入,刪除,查詢等。那些需要元素間排序資訊的操作將不會得到支援。比如findmin,findmax和線性時間按排序順序列印整個表的操作時不支援的。

雜湊函式在理想狀態是能將任何兩個不同的關鍵字對映到不同的單元,但是這是不可能,因為關鍵字是無窮的,但是雜湊表是有大小的。所以我們要找的雜湊函式應該能夠在單元之間均勻的分配關鍵字。而且需要能夠解決衝突。

通常保證雜湊表的大小是素數。

分離鏈結法

將雜湊到同乙個值的所有元素儲存在乙個鍊錶中

#ifndef hash_sep_h

#define hash_sep_h

typedef int elementtype;

struct listnode;

typedef struct listnode *position;

typedef position list;

struct hashtbl;

typedef struct hashtbl *hashtable;

hashtable initializetable(int tablesize);

position find(elementtype key, hashtable h);

void insert(elementtype key, hashtable h);

void printhashsep(hashtable h);

#endif

#include "hashsep.h"

#include using namespace std;

struct listnode ;

struct hashtbl ;

int hash(int n, int tablesize)

bool isprime(int n)

return true;

}int nextprime(int n)

}hashtable initializetable(int tablesize)

return h;

}position find(elementtype key, hashtable h)

void insert(elementtype key, hashtable h) }}

void printhashsep(hashtable h) }}

int main(int argc, char **argv)

;struct hashentry ;

struct hashtbl ;

bool isprime(int n)

return true;

}int nextprime(int n)

}int hash(int key, int tablesize)

hashtable initializetable(int tablesize)

position find(elementtype key, hashtable h)

return currentpos;

}void insert(elementtype key, hashtable h)

}int main(int argc, char **argv)

雙雜湊對於雙雜湊,一種流行的選擇是f(i) = i *

hash2(x)。我們將第二個雜湊函式應用到x並在距離hash2(x),2hash2(x)處探測。注意,函式一定不要算得0值。

再雜湊如果表填的太滿,操作的執行時間將開始消耗過長,且insert操作可能失敗。一種解決方法是建立另外乙個大約兩倍大的表(而且使用乙個相關的新雜湊函式),掃瞄整個原始雜湊表,計算每個(未刪除的)元素的新雜湊值並將其插入到新錶中。

python第五章 Python學習(第五章)

記錄所有的名片字典 card list defshow menu 顯示資訊 print 50 print 歡迎使用 名片管理系統 v1.0 print print 1.新增名片 print 2.顯示全部 print 3.搜尋名片 print print 0.退出系統 print 50 defnew ...

第五章 函式

第五章 函式 1 函式的定義 shell函式定義可以放在 bash profile 檔案中,也可以在使用該函式的指令碼中,還可以在命令列中 通過 source 或 執行bash profile檔案,使修改能夠立即生效 function 函式名 declare f 顯示定義的函式清單 export f...

第五章總結

5.1選單 選單是windows應用程式視窗的乙個非常重要的組成部分,視窗的選單欄一般在標題欄下面。這個選單通常稱為 主選單 5.1.1建立和程式設計選單 選單用於sdi 單文件介面 或mdi 多文件介面 以及基於對話方塊的應用程式,在基於對話方塊的應用程式中新建 設計並編輯選單後,在該對話方塊的 ...