散列表碰撞处理、开链法、HashTable散列
<span style="color: #0000ff;">function<span style="color: #000000;"> put(key,data) {
<span style="color: #0000ff;">var pos = <span style="color: #0000ff;">this<span style="color: #000000;">.betterHash(key);
<span style="color: #0000ff;">var index = 0<span style="color: #000000;">;
<span style="color: #0000ff;">if (<span style="color: #0000ff;">this.table[pos][index] ==<span style="color: #000000;"> undefined) {
<span style="color: #0000ff;">this.table[pos][index] =<span style="color: #000000;"> data;
}<span style="color: #0000ff;">else<span style="color: #000000;"> {
<span style="color: #0000ff;">while (<span style="color: #0000ff;">this.table[pos][index] !=<span style="color: #000000;"> undefined) {
++<span style="color: #000000;">index;
}
<span style="color: #0000ff;">this.table[pos][index] =<span style="color: #000000;"> data;
}
}
<span style="color: #008000;">/<span style="color: #008000;">散列函数<span style="color: #008000;">/
<span style="color: #0000ff;">function<span style="color: #000000;"> betterHash(string) {
const H = 37<span style="color: #000000;">;
<span style="color: #0000ff;">var total = 0<span style="color: #000000;">;
<span style="color: #0000ff;">for (<span style="color: #0000ff;">var i = 0; i < string.length; ++<span style="color: #000000;">i) {
total += H * total +<span style="color: #000000;"> string.charCodeAt(i);
}
total = total % <span style="color: #0000ff;">this<span style="color: #000000;">.table.length;
<span style="color: #0000ff;">if (total < 0<span style="color: #000000;">) {
total += <span style="color: #0000ff;">this.table.length-1<span style="color: #000000;">;
}
<span style="color: #0000ff;">return<span style="color: #000000;"> parseInt(total);
}
<span style="color: #0000ff;">function<span style="color: #000000;"> showDistro() {
<span style="color: #0000ff;">var n = 0<span style="color: #000000;">;
<span style="color: #0000ff;">for (<span style="color: #0000ff;">var i = 0; i < <span style="color: #0000ff;">this.table.length; ++<span style="color: #000000;">i) {
<span style="color: #0000ff;">if (<span style="color: #0000ff;">this.table[i][n] !=<span style="color: #000000;"> undefined) {
console.log(i + ": " + <span style="color: #0000ff;">this<span style="color: #000000;">.table[i]);
}
}
}
<span style="color: #0000ff;">function<span style="color: #000000;"> buildChains() {
<span style="color: #0000ff;">for (<span style="color: #0000ff;">var i = 0; i < <span style="color: #0000ff;">this.table.length; ++<span style="color: #000000;">i) {
<span style="color: #0000ff;">this.table[i] = <span style="color: #0000ff;">new<span style="color: #000000;"> Array();
}
}
<span style="color: #008000;">//<span style="color: #008000;"> get for separate chaining
<span style="color: #0000ff;">function<span style="color: #000000;"> get(key) {
<span style="color: #0000ff;">var index = 0<span style="color: #000000;">;
<span style="color: #0000ff;">var pos = <span style="color: #0000ff;">this<span style="color: #000000;">.betterHash(key);
</span><span style="color: #0000ff;">while</span> ((<span style="color: #0000ff;">this</span>.table[pos][index] != undefined)&&(<span style="color: #0000ff;">this</span>.table[pos][index] !=<span style="color: #000000;"> key)) {
index </span>+= 1<span style="color: #000000;">;
}
</span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">this</span>.table[pos][index] ==<span style="color: #000000;"> key) {
console.log(key</span>+" 的键值为: "+<span style="color: #0000ff;">this</span><span style="color: #000000;">.table[pos][index]);
</span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">this</span><span style="color: #000000;">.table[pos][index];
}</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{
console.log(</span>"无该键值"<span style="color: #000000;">);
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> undefined;
}
}
<span style="color: #008000;">/<span style="color: #008000;">测试开链法<span style="color: #008000;">/
<span style="color: #0000ff;">var someNames = ["David","Jennifer","Donnie","Raymond"<span style="color: #000000;">,"Cynthia","Mike","Clayton","Danny","Jonathan"<span style="color: #000000;">];
<span style="color: #0000ff;">var hTable = <span style="color: #0000ff;">new<span style="color: #000000;"> HashTable();
hTable.buildChains();
<span style="color: #0000ff;">for (<span style="color: #0000ff;">var i = 0; i < someNames.length; ++<span style="color: #000000;">i) {
hTable.put(someNames[i],someNames[i]);
}
hTable.showDistro();
hTable.betterHash("Jennifer"<span style="color: #000000;">);
hTable.get("Jennidfer"<span style="color: #000000;">);
hTable.get("Jennifer");
原文链接:https://www.f2er.com/js/403376.html