JavaScript封装一个简单的HashTable

本文介绍了如何使用JavaScript实现一个简单的哈希表,包括put、get、remove方法,哈希函数以及动态调整容量的resize方法。通过实例演示了如何插入、获取和删除键值对,并关注了负载因子对哈希表性能的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

哈希表具有查找快,分布均匀等特点,一起来学习哈希表,实现简单的哈希表封装吧~

/**
 *  封装HashTable
 *  主要方法
 *  put 插入
 *  get 获取
 *  remove 删除
 *  hashFunc 哈希函数
 *  resize 扩容或者缩容
 * @constructor
 */

function HashTable () {

    this.storage = [];  //仓库
    this.limit = 7;     //长度容量
    this.count = 0;     //现有长度

    //HashTable方法

    //插入
    HashTable.prototype.put = function (key, value) {

        var index = this.hashFunc(key, this.limit)

        var bucket = this.storage[index]

        if (bucket == null) {
            bucket = []
            this.storage[index] = bucket
        }

        var override = false

        for (var i = 0; i < bucket.length ; i++) {

            var tuple = bucket[i]

            if (tuple[0] === key) {
                tuple[1] = value
                override = true
            }
        }

        if (!override) {

            bucket.push([key, value])

            this.count ++
        }

        if (this.count > this.limit * .75) {

            this.resize(this.limit * 2)
        }
    }

    //获取
    HashTable.prototype.get = function (key) {

        var index = this.hashFunc(key, this.limit)

        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)

        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 --

                if(this.limit > 7 && (this.count < this.limit * .25)) {

                    this.resize(Math.floor(this.limit / 2))
                }

                return tuple[1]

            }
        }
        return null

    }

    //哈希扩容
    HashTable.prototype.resize = function (newLimit) {

        var oldStorage = this.storage

        this.storage = []
        this.limit = newLimit
        this.count = 0

        for (var i = 0; i < oldStorage.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])
            }

        }

    }


    //哈希函数
    HashTable.prototype.hashFunc = function (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;
    }
}

var a = new HashTable();

a.put("cat", "猫");
a.put("dog", "狗");
a.put("egg", "蛋");

a.put("dogss", "狗sd");
a.put("eggss", "蛋s");
a.put("eggsssd", "蛋sds");

console.log(a);


a.remove("eggsssd");
a.remove("dogss");
a.remove("eggss");
a.remove("egg");
a.remove("dog");



console.log(a);






 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值