[Javascript Data Structures] HashTable 哈希表

(1) 字典 键值对[key, value]

<script type="text/javascript">
     function Dictionary(){
       var obj={};
       // 设置值
       this.set=function(index,value){
           obj[index]=value;
       }
       // 删除
       this.remove=function(index){
           for(var i in obj){
               if (i==index) {
                     delete obj[index];
                     break;
               }
           }
           //if(has(index)){
           //  delete obj[index];
           //  }
       }
       // 是否有 
       this.has=function(index){
           return index in obj;
       }
       // 查出来的值放到数组中
       this.values=function(){
           var array=[];
           for(var i in obj){
              array.push(obj[i]);
           }
           console.log(array);
        }
        // dictionary的长度
        this.size=function(){
           var num=0;
           for(var i in obj){
              num++;
           }
           console.log(num);
       }
       // 展示以对象形式
       this.show=function(){
           return obj;
       }

     }


     var hash=new Dictionary();
     hash.set("1","jessica");
     hash.set("2","krystal");
     hash.set("3","ljy");
     hash.values();

     hash.remove("2");
     hash.values();
     hash.size();
     console.log(hash.has("3"));
     console.log(hash.show());
</script>

Result:

这里写图片描述


(2) HashMap

散列算法是能尽快的在数据结构中 找到一个值
记录的值和关键字是存在一定的关系


[1] 除留余数法

H(key)=key mod p;


[2] 如果冲突的话, 使用开放定址法 线性探查

H=(H(key)+d) mod p;
d :
   1,2,3,...n
   随机数
   。。。。

eg:
这里写图片描述


[3] 链地址法

这里写图片描述

编程思想:

   利用HashTable+单向链表 相结合
   每次insert一个data的时候, 先去检测在数组中是否为undefined, 说明在这个空还没有值加入, 
   那么将结合单向链表的使用, 用data创建一个node(obj 有data 和next)
   如果再次创建了重复的位置, 需要检测是否数组为undefined, 如果不是, 那么将搜索链表的最后位置, 将node加入到链表中。
<script type="text/javascript">

     /*单向链表 */    
        function Linkedlist(){
           //head其实就是array[n]是否有指向

           //初始化结点node 创建obj
           function Node(data){
              this.data=data;
              this.next=null;
           }

           /*增加node*/ 
           this.addnode=function(array,mod,data){
              var node=new Node(data); 
              if (array[mod]===undefined){
                  array[mod]=node;
              }else{
                  //已经有node
                  var current=array[mod];
                  while(current.next){
                    current=current.next;
                  }
                  //搜索到最后, 加入node
                  current.next=node;
              }
           }

          /*展示出链表内容*/ 
           this.print=function(head){
             var current=head;
             console.log(current);
             while(current){
                document.write("<td>"+current.data+"->"+"</td>"); 
                current=current.next;
                console.log(current);
             }
             document.write("<td>"+"null"+"</td>"); 
           }

          /*find data*/ 
          this.finddata=function(array,mod,searchdata){
             var current=array[mod];
             while(current){
               if (current.data===searchdata) {
                    document.write("Have the value: in array["+mod+"]="+searchdata);
                    break;
               }else{
                   current=current.next;                   
                   if(current===null){
                      document.write("Don't have "+ searchdata);
                      break;
                   }
                   continue;
               }               
             }
          }

        }// linkedlist end





     function HashMap(){
      var array=new Array(11);
       /*Hashtable*/

       //add data
         this.addhashmap=function(data){
            var mod=data%11; 
            var linkedlist=new Linkedlist();
            linkedlist.addnode(array,mod,data);          
         }

       // print hash map  
         this.printhashmap=function(){
            document.write(" <table>");
            for (var i = 0; i < array.length; i++) {
              document.write("<tr>");
                 if(array[i]!=null){
                    var linkedlist=new Linkedlist();
                    document.write("<td>"+i+": "+"</td>");
                    linkedlist.print(array[i]);
                 }else{
                    document.write("<td>"+i+": "+"</td>");
                 } 
              document.write("</tr>");                 
            }
            document.write("</table>");
         }

        // find data
         this.find=function(searchdata){
            var mod=searchdata%11;
            if (array[mod]===undefined) {
                document.write("Don't have "+ searchdata);
            }else{
                var linkedlist=new Linkedlist();
                linkedlist.finddata(array,mod,searchdata);
            }
         }
     }

      var hashmap=new HashMap();
      hashmap.addhashmap(60);
      hashmap.addhashmap(17);
      hashmap.addhashmap(29);
      hashmap.addhashmap(38);
      hashmap.addhashmap(5);
      hashmap.addhashmap(18);
      hashmap.printhashmap();

      hashmap.find(17);

</script>

这里写图片描述

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值