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;
        }
        
  • 相关阅读:
    基于XMPP实现的Openfire的配置安装+Android客户端的实现
    Android之基于XMPP协议即时通讯软件
    【Android XMPP】 学习资料收集贴(持续更新)
    R-ArcGIS探秘(1)安装以及Sample执行
    如何打造新媒体微营销平台
    29淘宝论坛推广技巧
    win10 UWP button
    Tomcat 6.x Perm区内存泄露问题
    Android WebView开发常见问题
    创建类模式大PK(总结)
  • 原文地址:https://www.cnblogs.com/WP-WangPin/p/13946162.html
Copyright © 2011-2022 走看看