散列表碰撞处理、开链法、HashTable散列

前端之家收集整理的这篇文章主要介绍了散列表碰撞处理、开链法、HashTable散列前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
散列表碰撞处理、开链法、HashTable散列
解决了散列函数的碰撞处理问题 .table = Array(137.betterHash = betterHash;函数 .showDistro = showDistro;显示散列表里的数据 .buildChains = buildChains;生成二维数组 .put = put; .get = get; <span style="color: #008000;">//<span style="color: #008000;"> put for separate chaining
<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;"&gt;while</span> ((<span style="color: #0000ff;"&gt;this</span>.table[pos][index] != undefined)&amp;&amp;(<span style="color: #0000ff;"&gt;this</span>.table[pos][index] !=<span style="color: #000000;"&gt; key)) {
    index </span>+= 1<span style="color: #000000;"&gt;;
}

</span><span style="color: #0000ff;"&gt;if</span>(<span style="color: #0000ff;"&gt;this</span>.table[pos][index] ==<span style="color: #000000;"&gt; key) {
    console.log(key</span>+" 的键值为: "+<span style="color: #0000ff;"&gt;this</span><span style="color: #000000;"&gt;.table[pos][index]);
    </span><span style="color: #0000ff;"&gt;return</span> <span style="color: #0000ff;"&gt;this</span><span style="color: #000000;"&gt;.table[pos][index];
}</span><span style="color: #0000ff;"&gt;else</span><span style="color: #000000;"&gt;{
    console.log(</span>"无该键值"<span style="color: #000000;"&gt;);
    </span><span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; 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

猜你在找的JavaScript相关文章