(js描述的)数据结构[哈希表1.2](9)

一. 优秀的哈希函数

1.快速的计算: 需要快速的计算来获得对应的hashCode(霍纳法则来减少乘除次数)
2.均匀的分布: 尽可能将元素映射到不同的位置,让元素在哈希表中均匀分布

二.哈希表的扩容

在这里插入图片描述

三.哈希函数的实现

		
 		// 封装哈希表
         function HashMap() {
             //属性
             this.storage = []  //存储数据
             this.count = 0     //插入元素数量
             this.limit = 7     //数组容量

            //方法
            //哈希函数
            HashMap.prototype.hashFunc =  function (str, size) {
                //定义hashCode
                var hashCode = 0
                for (var i =0; i < str.length ; i++) {
                    hashCode = 37*hashCode + str.charCodeAt(i)
                }
                var index = hashCode % size
                return index
            }

            // 插入&修改
            HashMap.prototype.put =function(key, value) {
                // 1.根据key获取对应的index
                var index = this.hashFunc(key, this.limit)
                // 2.根据index取出对应的bucket
                var bucket = this.storage[index]
                // 3.判断bucket是否为null
                if (bucket == null) {
                    bucket = []
                    this.storage[index] = bucket
                }
                // 4.判断是否是修改数据
                for (var i = 0; i< bucket.length; i++) {
                    var tuple = bucket[i]
                    if (tuple[0] == key) {
                        tuple[1] = value
                        return
                    }
                }
                // 5.进行添加操作
                bucket.push([key, value])
                this.count += 1

                //扩容
                if (this.count > this.limit * 0.75) {
                    this.resize(this.limit * 2)
                }
            }

            //获取操作
            HashMap.prototype.get =function(key) {
                // 1.根据key获取对应的index
                var index = this.hashFunc(key, this.limit)
                // 2.根据index取出对应的bucket
                var bucket = this.storage[index]
                // 3.判断bucket是否为null
                if (bucket == null) {
                    return null
                }
                // 4.判断是否存在数据
                for (var i = 0; i< bucket.length; i++) {
                    var tuple = bucket[i]
                    if (tuple[0] == key) {
                        return tuple[1]
                    }
                }
                return null
            } 
            //删除操作
            HashMap.prototype.remove =function(key) {
                // 1.根据key获取对应的index
                var index = this.hashFunc(key, this.limit)
                // 2.根据index取出对应的bucket
                var bucket = this.storage[index]
                // 3.判断bucket是否为null
                if (bucket == null) {
                    return null
                }
                // 4.判断是否存在数据
                for (var i = 0; i< bucket.length; i++) {
                    var tuple = bucket[i]
                    if (tuple[0] == key) {
                        bucket.splice(i , 1)
                        this.count--
                        if (this.limit > 7 && this.count < this.limit *0.25) {
                            thiss.resize(Math.floor(this.limit / 2))
                        }
                        return tuple[1]
                    }
                }
                return null
            } 
            // 其他方法
            //isEmpty方法
            HashMap.prototype.isEmpty = function() {
                return this.count == 0
            }
            //size方法
            HashMap.prototype.size = function() {
                return this.count
            }

            //数组扩容或缩容
            HashMap.prototype.resize = function(newLimit) {
                //保存所有属性
                var oldStorage = this.storage
                //重置所有属性
                this.count = 0
                this.limit = newLimit
                this.storage = []
                //遍历oldStorage中所有的bucket
                for (var i = 0; i < oldStorage.length; i++) {
                    var bucket = oldStorage[i]
                    if (bucket == null) {
                        continue
                    }
                    for (var j = 0; i< bucket.length; j++) {
                        var tuple = bucket[j]
                        this.put(tuple[0], tuple[1])
                    }
                }
            }
        }
posted @ 2020-04-05 11:39  jacksonni  阅读(137)  评论(0编辑  收藏  举报