数据结构114-哈希表的扩容实现代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>哈希表的封装</title>
</head>
<body>
    <script>
        function hashFunc(str, size) {
        var hashCode = 0;
        //霍纳算法
        for (var i = 0; i < str.length; i++) {
          hashCode = 37 * hashCode + str.charCodeAt(i);
        }
        var index=hashCode%size
        return index
      }
        function HashTable(){
            this.storage=[]
            this.count=0
            this.limit=7*2
            //方法 
            HashTable.prototype.put=function(key,value){
                //根据key获取对应的index
                var index=this.hashFunc(key,this.limit)
                //根据index取出对应的bucket
                var bucket=this.storage[index]
                //、
                if(bucket==null){
                    bucket=[],
                    this.storage[index]=bucket
                }
                for(var i=0;i<bucket.length;i++){
                    var tuple=bucket[i]
                    if(tuple[0]==key){
                        tuple[1]=value
                        return
                    }
                }
                bucket.push([key,value])
                this.count+=1
                //判断是否需要扩容
                if(this.count>this.limit*0.75){
                    this.resize(this.limit*2)
                }
            }
            HashTable.prototype.get=function(key){
                //
                var index=this.hashFunc(key,this.limit)
                //根据index取出对应的bucket
                var bucket=this.storage[index]
                if(bucket==null){
                    return null
                }
                for(var i=0;i<bucket.length;i++){
                    var tuple=bucket[i]
                    if(tuple[0]==key){
                       return tuple[1]
                    }
                }
                return null
            }
            HashTable.prototype.remove=function(key){
                var index=this.hashFunc(key,this.limit)
                //根据index取出对应的bucket
                var bucket=this.storage[index]
                if(bucket==null){
                    return null
                }
                for(var i=0;i<bucket.length;i++){
                    var tuple=bucket[i]
                    if(tuple[0]==key){
                    bucket.splice(i,1)
                    this.count--
                    return tuple[1]
                    if(this.limit>7&&this.count<this.limit*0.25){
                        this.resize(Math.floor(this.limit/2))
                    }
                }
                }
              return null
            }
            HashTable.prototype.isEmpty=function(key){
                return this.count==0
            }
            HashTable.prototype.size=function(key){
                return this.count
            }
            HashTable.prototype.resize=function(key){
                var oldStorage=this.storage
                //重置所有的属性
                this.storage=[]
                this.count=0
                this.limit=newLimit

                for(var i=0;i<bucket.length;i++){
                    var bucket=oldStorage[i]
                    if(bucket==null){
                        continue
                    }
                    for(var j=0;j<bucket.length;j++){
                    var tuple=bucket[j]
                    this.put(tuple[0],tuple[1])
                    
                }
                }

            }
        }
    </script>
</body>
</html>

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值