散列

作者: _____西班木有蛀牙 | 来源:发表于2018-07-01 19:22 被阅读13次
// 通过将字符串的ASCII码值的和来计算hash值
// 缺点:hash值分配不均匀
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>链表</title>
</head>

<body>
<script>
    // hash
    function HashTable () {
        this.table = new Array(137);
        this.simpleHash = simpleHash;
        this.showDistro = showDistro;
        this.put = put;
        this.get = get;
    }

    function simpleHash (data) {
        var total = 0;
        for (var i = 0; i < data.length;i++) {
            total += data.charCodeAt(i);
        }
        return total % this.table.length;
    }

    
    // 插入
    function put (data) {
        var pos = this.simpleHash(data);
        this.table[pos] = data;
    }

    function get (key) {
        return this.table[this.simpleHash(data)];
    }

    function showDistro () {
        var n = 0;
        for (var i = 0; i < this.table.length; i++) {
            if (this.table[i] != undefined) {
                console.log('键值是->' + i + '值是 【' + this.table[i] + '】')
            }
        }
    }

    var hTable = new HashTable();
    hTable.put('china');
    hTable.put('Japan');
    hTable.put('America');
    hTable.put('nicha');
    hTable.put('131');
    hTable.put('78');
    hTable.put('39');
    hTable.put('460');
    hTable.showDistro();
</script>
</body>

</html>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>链表</title>
</head>

<body>
<script>
    // hash
    function HashTable () {
        this.table = new Array(137);
        this.simpleHash = simpleHash;
        this.betterHash = betterHash;
        this.showDistro = showDistro;
        this.put = put;
        this.get = get;
    }
    
    function simpleHash (data) {
        var total = 0;
        for (var i = 0; i < data.length;i++) {
            total += data.charCodeAt(i);
        }
        return total % this.table.length;
    }

    function betterHash (data) {
        var H = 31;
        var total = 0;
        for (var i = 0; i < data.length; i++) {
            total += H * total + data.charCodeAt(i);
        }
        if (total < 0) {
            total += this.table.length - 1;
        }
        return total % this.table.length;
    }

    // 插入
    function put (data) {
        // var pos = this.simpleHash(data);
        var pos = this.betterHash(data);
        this.table[pos] = data;
    }

    function get (key) {
        return this.table[this.simpleHash(data)];
    }

    function showDistro () {
        var n = 0;
        for (var i = 0; i < this.table.length; i++) {
            if (this.table[i] != undefined) {
                console.log('键值是->' + i + '值是 【' + this.table[i] + '】')
            }
        }
    }

    var hTable = new HashTable();
    hTable.put('china');
    hTable.put('Japan');
    hTable.put('America');
    hTable.put('nicha');
    hTable.put('131');
    hTable.put('78');
    hTable.put('39');
    hTable.put('460');
    hTable.showDistro();
</script>
</body>

</html>
// 开链法,把一维数组转换为二维数组
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>链表</title>
</head>

<body>
<script>
    // hash
    function HashTable () {
        this.table = new Array(137);
        this.buildChians = buildChians;
        this.simpleHash = simpleHash;
        this.betterHash = betterHash;
        this.showDistro = showDistro;
        this.put = put;
        this.get = get;
    }
    
    function buildChians () {
        for (var i = 0; i < this.table.length; i++) {
            this.table[i] = new Array();
        }
    }

    function simpleHash (data) {
        var total = 0;
        for (var i = 0; i < data.length;i++) {
            total += data.charCodeAt(i);
        }
        return total % this.table.length;
    }

    function betterHash (data) {
        var H = 31;
        var total = 0;
        for (var i = 0; i < data.length; i++) {
            total += H * total + data.charCodeAt(i);
        }
        if (total < 0) {
            total += this.table.length - 1;
        }
        return total % this.table.length;
    }

    // 插入
    function put (data) {
        var pos = this.simpleHash(data);
        // var pos = this.betterHash(data);
        // this.table[pos] = data;
        var index = 0;
        if (this.table[pos][index] == undefined) {
            this.table[pos][index] = data;
            index ++;
        } else {
            while (this.table[pos][index] != undefined) {
                ++index;
            }
            this.table[pos][index] = data;
        }
    }

    function get (key) {
        return this.table[this.simpleHash(data)];
    }

    function showDistro () {
        var n = 0;
        for (var i = 0; i < this.table.length; i++) {
            if (this.table[i][0] != undefined) {
                console.log('键值是->' + i + '值是 【' + this.table[i] + '】')
            }
        }
    }

    var hTable = new HashTable();
    hTable.buildChians();
    hTable.put('china');
    hTable.put('Japan');
    hTable.put('America');
    hTable.put('nicha');
    hTable.showDistro();
</script>
</body>

</html>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>链表</title>
</head>

<body>
<script>
    // hash
    function HashTable () {
        this.table = new Array(137);
        this.simpleHash = simpleHash;
        this.showDistro = showDistro;
        this.put = put;
        this.get = get;
    }

    function simpleHash (data) {
        var total = 0;
        for (var i = 0; i < data.length;i++) {
            total += data.charCodeAt(i);
        }
        return total % this.table.length;
    }
    
    // 插入
    function put (data) {
        var pos = this.simpleHash(data);
        // this.table[pos] = data;
        if (this.table[pos] == undefined) {
            this.table[pos] = data;
        } else {
            while (this.table[pos] != undefined) {
                pos++;
            }
            this.table[pos] = data;
        }
    }

    function get (key) {
        var hash = this.simpleHash(key);
        console.log(hash);
        for (var i = hash; i < this.table.length; i++) {
            if (this.table[i] == key) {
                return i;
            }
        }
        return undefined;
    }

    function showDistro () {
        var n = 0;
        for (var i = 0; i < this.table.length; i++) {
            if (this.table[i] != undefined) {
                console.log('键值是->' + i + '值是 【' + this.table[i] + '】')
            }
        }
    }

    var hTable = new HashTable();
    hTable.put('china');
    hTable.put('Japan');
    hTable.put('America');
    hTable.put('nicha');
    console.log(hTable.get('nicha'))
    hTable.showDistro();
</script>
</body>

</html>

相关文章

  • 散列 & 线性散列

    Hashing 散列 原理: use key value to compute page address of t...

  • python数据结构教程 Day10

    本节重点: 散列 散列函数 完美散列函数 hashlib 散列函数设计 冲突解决方案 一、散列 能够使得查找的次数...

  • 散列

    散列值与相等性 等值对象的散列值必须相等。散列相等未必等值。 散列表算法 其他说明 key必须是可散列的。可散列需...

  • 散列

  • 散列

    定义 散列是一种常见的数据存储技术,散列后的数据可以快速插入或者取用。散列使用的数据解构叫做散列表。在散列中插入、...

  • 散列

    HashMap HashMap也是我们使用非常多的Collection,它是基于哈希表的 Map 接口的实现,以k...

  • 散列

    哈希码是一个散列值,通过单向函数求得,范围是int,数量有限,所以会发生散列值冲突。HashMap、Hashtab...

  • 散列

    散列的定义与整数散列 问题提出:给出 N 个正整数,再给出 M 个正整数,问这 M 个数中的每个数分别是否在 N ...

  • 散列

    散列函数:把查找表中的关键字映射成该关键字对应的地址。Hash(key)=Addr这里的地址可以是数组下标,索引或...

  • 散列

    前面我们讲过字典,虽然我们说过通过使用查找方法来提高处理效率,但是这样的效果其实并不是很好,因此我们还需要使用一种...

网友评论

      本文标题:散列

      本文链接:https://www.haomeiwen.com/subject/bchhuftx.html