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

简介: 数据结构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>
相关文章
|
13天前
|
存储 算法
【单向链表】数据结构——单向链表的介绍与代码实现&笔记
【单向链表】数据结构——单向链表的介绍与代码实现&笔记
|
13天前
|
搜索推荐 算法
【排序】数据结构——排序算法概念及代码详解(插入、冒泡、快速、希尔)
【排序】数据结构——排序算法概念及代码详解(插入、冒泡、快速、希尔)
|
7天前
|
算法
数据结构和算法常见的问题和代码
数据结构和算法常见的问题和代码
|
11天前
|
存储 NoSQL 算法
redis数据结构—哈希表
redis数据结构—哈希表
15 0
|
17天前
|
存储 机器学习/深度学习 缓存
【数据结构】布隆过滤器原理详解及其代码实现
【数据结构】布隆过滤器原理详解及其代码实现
|
20天前
|
存储 算法 大数据
深入解析力扣170题:两数之和 III - 数据结构设计(哈希表与双指针法详解及模拟面试问答)
深入解析力扣170题:两数之和 III - 数据结构设计(哈希表与双指针法详解及模拟面试问答)
|
24天前
|
存储 算法
数据结构和算法——了解哈希表(哈希查找、散列的基本思想)
数据结构和算法——了解哈希表(哈希查找、散列的基本思想)
19 0
|
24天前
|
算法 C语言
数据结构和算法——桶排序和基数排序(图示、伪代码、多关键字排序,基数排序代码)
数据结构和算法——桶排序和基数排序(图示、伪代码、多关键字排序,基数排序代码)
12 0
|
24天前
|
算法 C语言
数据结构和算法——归并排序(有序子列的归并、递归算法、非递归算法、思路图解、C语言代码)
数据结构和算法——归并排序(有序子列的归并、递归算法、非递归算法、思路图解、C语言代码)
13 0
|
24天前
|
存储 算法 C语言
数据结构和算法——堆排序(选择排序、思路图解、代码、时间复杂度、堆排序及代码)
数据结构和算法——堆排序(选择排序、思路图解、代码、时间复杂度、堆排序及代码)
15 0