hash散列中需要确定key和value的唯一确定关系。

hash散列便于快速的插入删除和修改,不便于查找最大值等其他操作

以下为字符和数字的hash散列:

function HashTable () {
this.table = new Array(137);
this.value = new Array();
this.simpleHash = simpleHash;
this.betterHash = betterHash;
this.display = display;
this.put = put;
this.get = get;
this.buildChains = buildChains; // 开链法解决碰撞
} function simpleHash (data) {
var total = 0;
for(var i =0; i<data.length; i++){
total= total + data.charCodeAt(i);
}
return total % this.table.length;
} function betterHash (data) {
var total = 0;
const h = 37; // 挑选合适的质数
data = data.toString();
for(var i=0;i<data.length; i++){
total = total*h + data.charCodeAt(i);
}
total = total % this.table.length;
if(total<0) {
return total+=this.table.length-1;
}
return parseInt(total);
} function put (key, value) {
var pos = this.betterHash(key);
if(this.table[pos] === undefined){
this.table[pos] = key;
this.value[pos] = value;
}else{
while(this.table[pos]!==undefined){
pos++
}
this.table[pos] = key;
this.value[pos] = value;
}
} function get(key) {
var hash = -1;
hash = this.betterHash(key);
if (hash > -1) {
for (var i = hash; this.table[hash] != undefined; i++) {
if (this.table[hash] == key) {
return this.value[hash];
}
}
}
return undefined;
} function display () {
var _this = this;
this.value.forEach(function(item, index){
if (item!==undefined) {
console.log(_this.table[index] + ": " + item);
} })
} function buildChains () {
this.table.forEach(function (item, index) {
item = new Array();
})
}

hash的使用方法:

function buildChains () {
this.table.forEach(function (item, index) {
item = new Array();
})
} var someNames = ["David", "Jennifer", "Donnie", "Raymond","Cynthia", "Mike", "Clayton", "Danny", "Jonathan", "Donnie"]; var hs = new HashTable();
someNames.forEach(function(item, index){
hs.put(item, item + "Val")
})
hs.display(); console.log(hs.get("David"))

js数据结构之hash散列的详细实现方法的更多相关文章

  1. js数据结构之栈和队列的详细实现方法

    队列 队列中我们主要实现两种: 1. 常规队列 2. 优先队列(实际应用中的排队加急情况等) 常规队列的实现方法如下: // 常规队列 function Queue () { this.queue = ...

  2. javascript数据结构与算法--散列

    一:javascript数据结构与算法--散列  一:什么是哈希表? 哈希表也叫散列表,是根据关键码值(key,value)而直接进行访问的数据结构,它是通过键码值映射到表中一个位置来访问记录的,散列 ...

  3. 算法与数据结构(十二) 散列(哈希)表的创建与查找(Swift版)

    散列表又称为哈希表(Hash Table), 是为了方便查找而生的数据结构.关于散列的表的解释,我想引用维基百科上的解释,如下所示: 散列表(Hash table,也叫哈希表),是根据键(Key)而直 ...

  4. JavaScript数据结构与算法-散列练习

    散列的实现 // 散列类 - 线性探测法 function HashTable () { this.table = new Array(137); this.values = []; this.sim ...

  5. PAT A1145 Hashing - Average Search Time (25 分)——hash 散列的平方探查法

    The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...

  6. java 解决Hash(散列)冲突的四种方法--开放定址法(线性探测,二次探测,伪随机探测)、链地址法、再哈希、建立公共溢出区

    java 解决Hash(散列)冲突的四种方法--开放定址法(线性探测,二次探测,伪随机探测).链地址法.再哈希.建立公共溢出区 标签: hashmaphashmap冲突解决冲突的方法冲突 2016-0 ...

  7. 【hash】什么是hash,什么是哈希,什么是hash散列,什么是hash一致性算法【关于hash的详解】

    什么是hash,什么是哈希,什么是hash散列,什么是hash一致性算法

  8. [转]c# 对密码执行散列和 salt 运算方法

    本文转自:http://www.cnblogs.com/CnBlogFounder/archive/2008/07/04/1235690.html 大家对密码执行散列和Salt运算一定不陌生.两个Vi ...

  9. 【数据结构】之散列链表(Java语言描述)

    散列链表,在JDK中的API实现是 HashMap 类. 为什么HashMap被称为“散列链表”?这与HashMap的内部存储结构有关.下面将根据源码进行分析. 首先要说的是,HashMap中维护着的 ...

随机推荐

  1. 变量,id()

    >>> a = 1 >>> print id(a) 2870961640 >>> b = a >>> print id(b) 2 ...

  2. CXF2.7整合spring发布webservice,返回值类型是Map和List<Map>类型

    在昨天研究了发布CXF发布webservice之后想着将以前的项目发布webservice接口,可是怎么也发布不起来,服务启动失败,原来是自己的接口有返回值类型是Map. 研究了一番之后,发现: we ...

  3. sqlplus连接远程数据库

    方式一:简易连接,不用进行网络配置,其实就是tnsname.ora文件 命令:sqlplus 用户名/密码@ip地址[:端口]/service_name [as sysdba] 示例:sqlplus ...

  4. 机器学习编程语言之争,Python夺魁

    机器学习编程语言之争,Python夺魁 随着科技的发展,拥有高容量.高速度和多样性的大数据已经成为当今时代的主题词.数据科学领域中所采用的机器学习编程语言大相径庭.究竟哪种语言最适合机器学习成为争论不 ...

  5. uboot中的快捷菜单的制作说明 【转】

    转自:http://blog.chinaunix.net/uid-22030783-id-366971.html   在uboot中加入快捷操作菜单的方法非常简单,在论坛发布的uboot201003V ...

  6. setfacl报错Operation not supported

    对文件目录setfacl权限设置时报错Operation not supported Google一下,发现是分区acl权限问题 一般情况下(ext4),默认acl支持都是加载的.但如果遇到二般情况, ...

  7. 关于MySQL 8.0的几个重点【转】

    转自 关于MySQL .0的几个重点,都在这里 https://mp.weixin.qq.com/s/QUpk9uuS2JTli1GT6HuORA 一.关于MySQL Server的改进 1.1 re ...

  8. [转]VS2015 Git 源码管理工具简单入门

    VS2015 Git 源码管理工具简单入门   1.VS Git插件 1.1 环境 VS2015+GitLab 1.2 Git操作过程图解 1.3 常见名词解释 拉取(Pull):将远程版本库合并到本 ...

  9. RestTemplate -springwebclient

    1 使用jar版本 - spring-web-4.3.8.RELEASE.jar 场景:backend,post请求远端,header中加入accessToken,用于权限控制 HttpHeaders ...

  10. Python-生产者消费模型 线程

    7.生产者消费者模型(*****)(思聪吃热狗代码) 在并发编程中使用生产者和消费者模式能够解决绝大多数并发问题. 该模式通过平衡生产线程和消费线程的工作能力来提高程序的整体处理数据的速度 为什么要使 ...