Javascript之数据结构与算法的HashMap实现

版权声明:转载请注明出处 https://blog.csdn.net/wushichao0325/article/details/86137104

Javascript之数据结构与算法的HashMap实现

1.自实现HashMap

let LinkedList=require("./LinkedList")
let loseloseHashCode=Symbol();
class HashMap{
    constructor(){
        this.table=[];
        this[loseloseHashCode]=function(key){//散列函数
            let hash=0;
            for(let i=0;i<key.length;i++){
                hash+=key.charCodeAt(i);//字符对应的asc码值
            }
            return hash%37;
        };//私有函数
        this[djb2HashCode]=function(key){//优化版散列函数
            let hash=5381;
            for(let i=0;i<key.length;i++){
                hash=hash*33+key.charCodeAt(i);
            }
            return hash%1013;
        }
        
    }
    //分离链接法
    put(key,value){
        let position=this[loseloseHashCode](key);

        if(this.table[position]==undefined){
            this.table[position]=new LinkedList();
            this.table[position].append(new ValuePair(key,value));  
        }else if(this.table[position]!=undefined){
            let hasKey=false;
            let current=this.table[position].getHead();
            if(current.element.key==key){
                hasKey=true;
            }
            while(current.next){//判断放入的key值是否已经存在,非position值即hash值。
                if(current.element.key==key){
                    haskey=true;
                    break;
                }
                current=current.next;
            }
            if(hasKey==false){
                this.table[position].append(new ValuePair(key,value));  
            }else{
                console.log("有重复的key值,不能再次放入")
            }
        }
             
    }
    //线性探查
    put_1(key,value){
        let position=this[loseloseHashCode](key);
        if(this.table[position]==undefined){
            console.log("该位置无元素")
            this.table[position]=new ValuePair(key,value);
        }else{
            console.log("该位置有元素")
            let hasKey=false;
            if(this.table[position].key==key){
                hasKey=true;
            }
            if(hasKey==false){//防止放入相同key值的值,非position
                var index=++position;
                while(this.table[index]!=undefined){
                    if(this.table[index].key==key){
                        hasKey=true;
                    }
                    index++;
                }
                if(hasKey==false){
                    this.table[index]=new ValuePair(key,value);
                }
            }
        }
    }
    //分离链接法
    get(key){
        var position=this[loseloseHashCode](key);
        if(this.table[position]!=undefined){
            let current=this.table[position].getHead();
            if(current.element.key==key){
                return current.element.value;
            }
            while(current.next){
                if(current.element.key==key){
                    return current.element.value;
                }
                current=current.next;
            }
        }
        return undefined;
    }
    //线性探查
    get_1(key){
        let position=this[loseloseHashCode](key);
        if(this.table[position]!==undefined){
            if(this.table[position].key==key){
                return this.table[position].value;
            }else{
                let index=++position;
                while(this.table[index]==undefined||this.table[index].key!=key){
                    index++;
                }
                if(this.table[index].key==key){
                    return this.table[index].value;
                }
            }
        }
        return undefined;
    }
    //分离链接法
    remove(key){
        let position=this[loseloseHashCode](key);
        if(this.table[position]!==undefined){
            let current=this.table[position].getHead();
            if(current.element.key==key){//判断第一个位置是否为匹配的值
                this.table[position].remove(current.element);
                if(this.table[position].isEmpty()){
                    this.table[position]=undefined;
                }
                return true;
            }
            while(current.next){
                if(current.element.key==key){
                    this.table[position].remove(current.element);
                    if(this.table[position].isEmpty()){
                        this.table[position]=undefined;
                    }
                    return true;
                }
                current=current.next;
            }
            
        }
        return false;
    }
    //线性探查
    remove_1(key){
        let position=this[loseloseHashCode](key);
        if(this.table[position]!==undefined){
            if(this.table[position].key==key){
                this.table[index]=undefined;
            }else{
                let index=++position;
                while(this.table[index]==undefined||this.table[index].key!=key){
                    index++;
                }
                if(this.table[index].key==key){
                    this.table[index]=undefined;
                }
            }
        }
    }
    getAll(){
        return this.table
    }
}

//----------------------------散列冲突---------------------
//1.分离链接
class ValuePair{//链表每一个元素存入的值为ValuePair对象
    constructor(key,value){
        this.key=key;
        this.value=value;
    }
    toString(){
        return '['+this.key+'-'+this.value+']';
    }
}
let hash=new HashMap();
hash.put_1('Gandalf','[email protected]');
hash.put_1('Gandalf','[email protected]');
hash.put_1('Gandalf','mail.com');
console.log(hash.get_1('Gandalf'));
console.log(hash.get_1('ll'))
console.log(hash.getAll())

注:其中LinkedList使用的是[https://blog.csdn.net/wushichao0325/article/details/84973099]中实现的LinkedList。

猜你喜欢

转载自blog.csdn.net/wushichao0325/article/details/86137104