JavaScript数据结构-13.散列碰撞(开链法)

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="UTF-8">
 5         <title>开链散列</title>
 6     </head>
 7     <body>
 8         <script>
 9             function HashTable(){
10                 this.table = new Array(137);
11                 this.betterHash = betterHash;
12                 this.showDistro = showDistro;
13                 this.put = put;
14                 this.get = get;
15                 this.buildChains = buildChains;
16             }
17 
18             function betterHash(data){
19                 const H =37;
20                 var total = 0;
21                 for(var i=0;i<data.length;i++){
22                     total += H*total + data.charCodeAt(i);
23                     
24                 }
25                 total %= this.table.length;
26                 if(total <0){
27                     total += this.table.length-1;
28                 }
29                 console.log(data+"->"+ total % this.table.length);
30                 return  parseInt(total);
31             }
32             function put(key,data){
33                 var pos = this.betterHash(key);
34                 var index = 0;
35                 if(this.table[pos][index] == undefined ){
36                     this.table[pos][index+1] = data;
37                     this.table[pos][0] = key;
38                     index++;
39                 }else{
40                     while(this.table[pos][index] != undefined ){
41                         index++;
42                     }
43                     this.table[pos][index+1] = data;
44                 }
45             }
46             function showDistro(){
47                 for(var i = 0;i<this.table.length;i++){
48                     if(this.table[i][0] != undefined){
49                         console.log(this.table[i][0],":",this.table[i]);
50                     }
51                 }
52             }
53             function get(key){
54                 var index = 0;
55                 var pos = this.betterHash(key);
56                 if(this.table[pos][index] == key){
57                     return this.table[pos][index+1];
58                     index+=2;
59                 }else{
60                     while(this.table[pos][index] != key){
61                         index += 2;
62                     }
63                     return this.table[pos][index+1];
64                 }
65                 return undefined;
66             }
67             function buildChains(){
68                 for(var i=0;i<this.table.length;i++){
69                     this.table[i] = new Array();
70                 }
71             }
72             
73             
74             var obj = new HashTable();
75             obj.buildChains();
76             obj.put("k1","zhangsan");
77             obj.put("k2","lisi");
78             obj.put("k3","javascript");
79             obj.showDistro();
80             console.log(obj.get("k2"));
81         </script>
82     </body>
83 </html>

 

posted @ 2017-07-16 21:28  橙云生  阅读(266)  评论(0编辑  收藏  举报