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. 交换机console口连接

    http://www.webkaka.com/info/archives/knowledge/2010/08/30452/

  2. 如何往linux上面上传东西

    在linux中rz 和 sz 命令允许开发板与主机通过串口进行传递文件了,下面我们就来简单的介绍一下rz 和 sz 命令的例子. rz,sz是Linux/Unix同Windows进行ZModem文件传 ...

  3. cmake 使用

    1.cmake 显示编译命令: 在顶层CMakeLists.txt里设置 set(CMAKE_VERBOSE_MAKEFILE ON) 或者  cmake .        再           m ...

  4. grep用法【转】

    简介 grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它 ...

  5. 无向图最小割Stoer-Wagner算法学习

    无向连通网络,去掉一个边集可以使其变成两个连通分量则这个边集就是割集,最小割集当然就权和最小的割集. 使用最小切割最大流定理: 1.min=MAXINT,确定一个源点 2.枚举汇点 3.计算最大流,并 ...

  6. centos6中iptables单机网络防火墙的使用

    概述: iptables:基于软件的形式实现的一种防火墙的软件程序 Firewall:工作在主机或网络边缘,对进出的报文按事先定义的规则进行检查,并且由匹配到的规则进行处理的一组硬件或软件,甚至可能是 ...

  7. 通达OA系统优化-对mysql数据库减肥

    OA系统冗余数据过多,访问效率受到影响,现需要对历史数据进行一次清理,以提高OA访问速度 大的数据主要体现在流程上,流程数据主要放在flow_run,flow_run_data,flow_run_pr ...

  8. js 将图片连接转换称base64格式

    我们把图像文件的内容直接写在了HTML 文件中,这样做的好处是,节省了一个HTTP 请求.坏处呢,就是浏览器不会缓存这种图像.现在我们提供一个js: function convertImgToBase ...

  9. Intellij IDEA14 搜索框及控制台乱码解决

    搜索ctrl+F及ctrl+H的搜索框.调试的时候控制台.导入module都显示为为中文乱码 如下: 解决方案: File->Setting->IDE Settings->Appea ...

  10. mysql更新字段值提示You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode

    1 引言 当更新字段缺少where语句时,mysql会提示一下错误代码: Error Code: 1175. You are using safe update mode and you tried ...