博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

2022/1/26

Posted on 2022-01-26 16:40  为你摘星星  阅读(27)  评论(0)    收藏  举报

一、哈希表

  1.秦九韶算法(也称霍纳法则), Pn(x)= anx n+a(n-1)x(n-1)+…+a1x+a0=((…(((anx +an-1)x+an-2)x+ an-3)…)x+a1)x+a0  ,时间复杂度由O(n*n)级别变为O(n)级别; 

    hashCode = hashCode*37 + str.charCodeAt(i);

  2.哈希表的方法  put(插入新数据并且修改value)、get(根据key值获取value)、remove(根据key值移除数据)、isEmpty、size。

  3.避免哈希表中产生冲突,我才用的是链地址法。

  4.经过观看文档并且未参考其他人的代码,手写实现栈、队列、优先队列、链表、双向链表、集合、字典的结构之后,我觉得实现哈希表的结构还挺容易的。

  5.代码实现

  

 

 

 

        function HashTable() {
            let storage = [];
            //count计数器,用于记录已经存在了多少条数据
            let count = 0;
            //用于表示一共可以存放多少数据
            let limit = 8;

            this.hashFunc = function (str, max) {
                let hashCode = 0;
                for (let i = 0; i < str.length; i++) {
                    hashCode = 37 * hashCode + str.charCodeAt(i);
                };
                hashCode = hashCode % max;
                return hashCode;
            }

            this.put = function (key, value) {
                let index = this.hashFunc(key, limit);
                function Node(key, value) {
                    this.key = key;
                    this.value = value;
                    this.next = null;
                };
                let newNode = new Node(key, value);
                if (storage[index]) {
                    let current = storage[index];
                    if (current.key === key) {
                        current.value = value;
                        return;
                    };
                    while (current.next) {
                        current = current.next;
                        if (current.key === key) {
                            current.value = value;
                            return;
                        }
                    };
                    current.next = newNode;
                } else {
                    storage[index] = newNode;
                };
                count += 1;
            }

            this.get = function (key) {
                let index = this.hashFunc(key, limit);
                if (!storage[index]) return null;
                let current = storage[index];
                while (current.key !== key) {
                    if (current.next === null) return null;
                    current = current.next;
                };
                return current.value;
            };

            this.remove = function (key) {
                let index = this.hashFunc(key, limit);
                if (!storage[index]) return false;
                let current = storage[index];
                let previous = null;
                if (current.key === key) {
                    if (current.next === null) {
                        storage[index] = null;
                    } else {
                        storage[index] = current.next;
                    };
                    count--;
                    return true;
                }
                while (current.key !== key) {
                    if (current.next === null) return false;
                    previous = current;
                    current = current.next;
                };
                previous.next = current.next;
                count--;
                return true;
            };

            this.isEmpty = function () {
                return count === 0;
            };

            this.size = function () {
                return count;
            }

            this.item = storage;
        };