zoukankan      html  css  js  c++  java
  • 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;
        }
        
  • 相关阅读:
    振动传感器
    水银开关式碰撞传感器
    倾斜传感器
    光敏传感器
    激光传感器的使用
    html字符串 转 json
    浏览器播放视频加快进功能
    处理箭头函数单个参数括号规则冲突
    VUE-directive指令之语法、引用、钩子函数、参数
    win10系统自动升级怎么还原到以前的系统
  • 原文地址:https://www.cnblogs.com/WP-WangPin/p/13946162.html
Copyright © 2011-2022 走看看