散列表,散列函数,碰撞处理解决:线性探测法
<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;">if (<span style="color: #0000ff;">this.table[pos] ==<span style="color: #000000;"> undefined) {
<span style="color: #0000ff;">this.table[pos] =<span style="color: #000000;"> key;
<span style="color: #0000ff;">this.values[pos] =<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] !=<span style="color: #000000;"> undefined) {
pos++<span style="color: #000000;">;
}
<span style="color: #0000ff;">this.table[pos] =<span style="color: #000000;"> key;
<span style="color: #0000ff;">this.values[pos] =<span style="color: #000000;"> data;
}
}
<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;">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] !=<span style="color: #000000;"> undefined) {
console.log(<span style="color: #0000ff;">this.table[i] + ": " + <span style="color: #0000ff;">this<span style="color: #000000;">.values[i]);
}
}
}
<span style="color: #008000;">//<span style="color: #008000;"> get for linear probing
<span style="color: #0000ff;">function<span style="color: #000000;"> get(key) {
<span style="color: #0000ff;">var hash = <span style="color: #0000ff;">this<span style="color: #000000;">.betterHash(key);
</span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">var</span> i = hash; <span style="color: #0000ff;">this</span>.table[hash] != undefined; i++<span style="color: #000000;">) {
</span><span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">this</span>.table[hash] ==<span style="color: #000000;"> key) {
console.log(</span>"查找到的键值为: "+<span style="color: #0000ff;">this</span><span style="color: #000000;">.values[hash]);
</span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">this</span><span style="color: #000000;">.values[hash];
}
}
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();
<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.get("Jennifer");
原文链接:https://www.f2er.com/js/403375.html