<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>开链散列</title>
</head>
<body>
<script>
function HashTable(){
this.table = new Array(137);
this.betterHash = betterHash;
this.showDistro = showDistro;
this.put = put;
this.get = get;
this.buildChains = buildChains;
} function betterHash(data){
const H =37;
var total = 0;
for(var i=0;i<data.length;i++){
total += H*total + data.charCodeAt(i); }
total %= this.table.length;
if(total <0){
total += this.table.length-1;
}
console.log(data+"->"+ total % this.table.length);
return parseInt(total);
}
function put(key,data){
var pos = this.betterHash(key);
var index = 0;
if(this.table[pos][index] == undefined ){
this.table[pos][index+1] = data;
this.table[pos][0] = key;
index++;
}else{
while(this.table[pos][index] != undefined ){
index++;
}
this.table[pos][index+1] = data;
}
}
function showDistro(){
for(var i = 0;i<this.table.length;i++){
if(this.table[i][0] != undefined){
console.log(this.table[i][0],":",this.table[i]);
}
}
}
function get(key){
var index = 0;
var pos = this.betterHash(key);
if(this.table[pos][index] == key){
return this.table[pos][index+1];
index+=2;
}else{
while(this.table[pos][index] != key){
index += 2;
}
return this.table[pos][index+1];
}
return undefined;
}
function buildChains(){
for(var i=0;i<this.table.length;i++){
this.table[i] = new Array();
}
} var obj = new HashTable();
obj.buildChains();
obj.put("k1","zhangsan");
obj.put("k2","lisi");
obj.put("k3","javascript");
obj.showDistro();
console.log(obj.get("k2"));
</script>
</body>
</html>

JavaScript数据结构-13.散列碰撞(开链法)的更多相关文章

  1. JavaScript数据结构-12.散列碰撞(线性探测法)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. JavaScript数据结构-11.散列

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. 散列数据结构以及在HashMap中的应用

    1. 为什么需要散列表? 对于线性表和链表而言,访问表中的元素,时间复杂度均为O(n).即便是通过树结构存储数据,时间复杂度也为O(logn).那么有没有一种方式可以将这个时间复杂度降为O(1)呢?当 ...

  4. 学习javascript数据结构(三)——集合

    前言 总括: 本文讲解了数据结构中的[集合]概念,并使用javascript实现了集合. 原文博客地址:学习javascript数据结构(三)--集合 知乎专栏&&简书专题:前端进击者 ...

  5. 散列表碰撞处理、开链法、HashTable散列

    散列表碰撞处理.开链法.HashTable散列 /** * 散列表碰撞处理.开链法.HashTable散列. * 将数组里的元素位置,也设置为数组,当两个数据的散列在同一个位置时, * 就可以放在这个 ...

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

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

  7. Python与数据结构[4] -> 散列表[2] -> 开放定址法与再散列的 Python 实现

     开放定址散列法和再散列 目录 开放定址法 再散列 代码实现 1 开放定址散列法 前面利用分离链接法解决了散列表插入冲突的问题,而除了分离链接法外,还可以使用开放定址法来解决散列表的冲突问题. 开放定 ...

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

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

  9. 散列--数据结构与算法JavaScript描述(8)

    散列 散列是一种常用的数据存储技术,散列后的数据可以快速地插入或取用. 散列使用的数据结构叫做散列表. 在散列表上插入.删除和取用数据都非常快,但是对于查找操作来说却效率低下,比如查找一组数据中的最大 ...

随机推荐

  1. 8.使用Exists监控ZNode的三大Change事件

    一. zookeeper是一个分布式的协调程序(所有程序都是通过订阅它来相互感知)   1. tcp(长链接) + watcher server ->client client ->ser ...

  2. [Elixir003] Mix Archives

    在[Elixir001]中使用 mix escript.build 生成一个lifelog 的escript启动脚本. 今天我们尝试一下另一种方式:生成Archives. 我们先添加一个Task 1. ...

  3. 今天踩过的坑——structs和mysql

    1 在action中写了interceptor-ref就不会用defaultStack啦.得自己补上2 继承CookiesAware是不够的,得在action中配置一下 <interceptor ...

  4. 【Dnc.Api.Throttle】适用于.Net Core WebApi接口限流框架

    Dnc.Api.Throttle    适用于Dot Net Core的WebApi接口限流框架 使用Dnc.Api.Throttle可以使您轻松实现WebApi接口的限流管理.Dnc.Api.Thr ...

  5. C# autofac的一些使用

    这次项目需要用autofac动态注册插件dll,插件修改或扩展后,在不重新编译的情况下能加载新的插件. 于是我们用autofac从配置文件注册.注册的文件固定named.这样不管插件怎么变,我们Res ...

  6. [uwp]MVVM之MVVMLight,一个登录注销过程的简单模拟

    之前学MVVM,从ViewModelBase,RelayCommand都是自己瞎写,许多地方处理的不好,接触到MVVMLigth后,就感觉省事多了. 那么久我现在学习MVVMLight的收获,简单完成 ...

  7. Cookie背景了解

    Cookie的复数形态是Cookies, 英文的意思是小甜饼,小饼干. 类型为小型文本文件, 指某些网站为了辨别用户身份储存在用户本地中断上的数据. 是前网景公司的员工 卢-蒙特利在1993年3月发明 ...

  8. ADV三星

    #include <iostream> using namespace std; #define SIZE 12 int data[SIZE]; int data1[SIZE]; int ...

  9. 爬取lol皮肤

    #!/usr/bin/python # -*- coding: utf-8 -*- # data:2018-11-23 # user:fei import re import requests imp ...

  10. ObjectARX动态添加AutoCAD传统下拉菜单入门篇(一)

    ObjectARX动态添加传统下拉菜单入门篇 图文by edata  , 转载注明出处 http://www.cnblogs.com/edata AutoCAD 添加传统下拉菜单有很多种方式,比较典型 ...