• JavaScript解决散列表的冲突(线性探查法)


    线性探查:当想表中的某个位置添加一个元素的时候,如果索引为position的位置已经被占据了,就尝试position+1的位置。如果position的位置也被占据了,就尝试position+2的位置,以此类推,直到在散列表中找到一个空闲的位置。

        put(key, value) {
            if (key != null || value != null) {
                let position = hashCode(key);    //获取散列函数生成的位置
                if (this.table[position] != null) {  //如果位置上为空
                    this.table[position] = new ValuePair(key, value);  //将元素添加在位置上
                } else {
                    let index = position + 1;    //如果位置上没空,就向下寻找下一个空位置
                    while (this.table[index] != null) {
                        index++;
                    }
                    this.table[index] = new ValuePair(key, value);  //将元素添加到空位置上
                }
                return true;
            }
            return false;
        }
        get(key) {
            let position = hashCode(key);  //通过散列函数找到位置
            if (this.table[position] != null && this.table[position].key === key) {
                return this.table[position].value;  //如果位置正好是要找的值
            }
            let index = position + 1;   //从位置开始遍历到散列表,找到键所在的键值对或null
            while (this.table[index] != null && this.table[position].key !== key) {
                index++;
            }
            if (this.table[index] != null && this.table[index].key === key) { //如果不为null就是所找位置
                return this.table[index].value;
            }
            return undefined;   //没找到返回undefined
        }
        verifyRemoveSideEffect(key, removedPosition) {  //验证是否有副作用
            const hash = this.hashCode(key);
            let index = removedPosition + 1;   //position是索引值
            while (this.table[index] != null) {   //如果下面有元素
                let posHash = this.hashCode(this.table[index].key);  //判断是不是之前向下移的元素
                if (posHash <= hash || posHash <= removedPosition) {   //散列值是否小于索引值或删除键的散列值
                    this.table[removedPosition] = this.table[index];  //把之前的
                    delete this.table[index];
                    removedPosition = index;
                }
                index++;   //一个一个元素向下查找,直到下一个元素是null
            }
        }
        remove(key) {
            const position = this.hashCode(key);  //散列值
            if (this.table[position] != null && this.table[position].key === key) {
                delete this.table[position];   //散列值位置上正好是key所在对象
                this.verifyRemoveSideEffect(key, position);  //删除后把应该上移的元素上移
                return true;  
            }
            let index = position + 1;
            while (this.table[index] != null && this.table[index].key !== key) {
                index++;
            }
            if (this.table[index] != null && this.table[index].key === key) {
                delete this.table[index];
                this.verifyRemoveSideEffect(key, index);
                return true;
            }
            return false;
        }
        
  • 相关阅读:
    http与https的区别以及https的加密原理
    android 网络之 httppost
    android 定时器与Canvas结合使用
    逛超市遇到问题了还是好心人多
    获取最新前十篇文章
    做菜 蒸鸡蛋
    使用ASP.NET 2.0 Profile存储用户信息_1[翻译] Level 200
    ajax 笔记不用刷新实现数据的分页显示 2 (上)
    在人力资源树当中,上级对下级员工信息查看修改操作。
    今年入冬于来的第一场雪
  • 原文地址:https://www.cnblogs.com/WP-WangPin/p/13946162.html
Copyright © 2020-2023  润新知