JavaScript数据结构-5-2-散列表-线性探查法

HashTable.js

function getPrime(n) {
    if (n <= 3) {
        return n;
    }
    var isPrime = false;
    while (!isPrime) {
        isPrime = true;
        var sqrt = Math.ceil(Math.sqrt(n));
        for (var i = 2; i <= sqrt; i++) {
            if (n % i == 0) {
                isPrime = false;
                --n;
                break;
            }
        }
    }
    return n;
}
class HashNode {
    constructor(key, value) {
        this.value = value;
        this.key = key;
    }
}
class HashTable {
    constructor(n) {
        this.table = new Array(n);
    }
    hash(key) {
        let length = this.table.length;
        let p = getPrime(length);
        return key % p; //
    }
    put(key, value) {
        var p = new HashNode(key, value);
        var position = this.hash(key);
        var base = position;
        var i = -1; //记录循环次数
        while (this.table[position] != undefined) {
            if (++i > this.table.length - 1) return false;
            position = (base + i) % this.table.length;
        }
        this.table[position] = p;
        return true;
        //当找到一个空位存放键值对,或者循环完数组,都没找到空位,则结束循环
    }
    get(key) {
        var position = this.hash(key);
        var base = position;
        if (this.table[position] == undefined) {
            return false;
        } else {
            for (var i = 1; i <= this.table.length; i++) {
                if (this.table[position] != undefined && this.table[position].key === key) return this.table[position].value;
                else {
                    position = (base + i) % this.table.length;
                }
            }
        }
        return false;
    }
    remove(key) {
        var position = this.hash(key);
        var base = position;
        if (this.table[position] == undefined) {
            return false;
        } else {
            for (var i = 1; i <= this.table.length; i++) {
                if (this.table[position] != undefined && this.table[position].key === key) {
                    this.table[position] = undefined;
                    return true;
                } else {
                    position = (base + i) % this.table.length;
                }
            }
        }
        return false;
    }
}

export {
    HashTable
};

test.js

import {HashTable} from "./HashTable.js";
var oneHash = new HashTable(3);
console.log(oneHash.put(1, 'a'));
console.log(oneHash.put(4, 'b'));
console.log(oneHash.put(7, 'c'));
console.log(oneHash.remove(7));
console.log(oneHash.remove(3));
console.log(oneHash.get(7));
console.log(oneHash.get(4));
console.log(oneHash.get(1));
console.log(oneHash.put(7, 'cd'));
console.log(oneHash.get(7));
console.log(oneHash.put(8, 'd')); //表满
console.log(oneHash.get(8));
console.log(oneHash.table);

index.html




	
		
	
	
	
	





再散列法和平方探查法略

 

你可能感兴趣的:(前端,JavaScript数据结构,散列表,开放定址法,线性探测法)