javascript数据结构6-字典 散列 集合
来源:互联网 发布:商标域名抢注案例 编辑:程序博客网 时间:2024/06/14 05:44
6.1 字典
字典是一种以键- 值对形式存储数据的数据结构,就像电话号码簿里的名字和电话号码一
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>字典sample</title></head><body><script> function Dictionary(){ this.add = add; this.datastore = new Array(); this.find = find; this.remove = remove; this.showAll = showAll; this.count = count; this.clear = clear; } function add(key, value) { this.datastore[key] = value; } function find(key) { return this.datastore[key]; } function remove(key) { delete this.datastore[key]; } function showAll() { if(this.datastore!=null){ var datakeys=Array.prototype.slice.call(Object.keys(this.datastore)); for (var key in datakeys) { document.write(datakeys[key] + " -> " + this.datastore[datakeys[key]]+" "); // console.log(Object.keys(this.datastore)); console.log(key); } }else{ document.write("字典为空"); } } function count() { var n = 0; for (var key in Object.keys(this.datastore)) { ++n; } return n; } function clear() { // for (var key in Object.keys(this.datastore)) { // delete this.datastore[key]; // } delete this.datastore; } //测试 var dic=new Dictionary(); dic.add("123","R"); dic.add("456","Python"); dic.add("789","JavaScipt"); document.write("</br>**************字典数目**************</br>"); var n=dic.count(); document.write(n); document.write("</br>**************全部显示**************</br>"); dic.showAll(); document.write("</br>**************删除123--->R*************</br>"); dic.remove("123"); dic.showAll(); document.write("</br>**************清除**************</br>"); dic.clear(); dic.showAll();</script></body></html>
6.2 散列(HashTable)
它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度
使用:MD5 和 SHA-1 可以说是目前应用最广泛的Hash算法
java中已经实现
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>HashTable散列表</title></head><body><script> function HashTable() { this.table = new Array(137); //为了避免碰撞,首先要确保散列表中用来存储数据的数组其大小是个质数。这一点很关键,这和计算散列值时使用的取余运算有关。 this.simpleHash = simpleHash; //简单的散列表 this.betterHash = betterHash; //更好的HashTable,避免碰撞 this.showDistro = showDistro; this.put = put; //this.get = get; } function put(data) { var pos = this.simpleHash(data); this.table[pos] = data; } function simpleHash(data) { var total = 0; for (var i = 0; i < data.length; ++i) { total += data.charCodeAt(i); } document.write("Hash value: " + data + " -> " + total+"<br/>"); return total % this.table.length; } function showDistro() { var n = 0; for (var i = 0; i < this.table.length; ++i) { if (this.table[i] != undefined) { document.write(i + ": " + this.table[i]+"<br/>"); } } } function betterHash(string) { const H = 31; //较小的质数 书上37不行 var total = 0; for (var i = 0; i < string.length; ++i) { total += H * total + string.charCodeAt(i); } total = total % this.table.length; if (total < 0) { total += this.table.length-1; } return parseInt(total); } var someNames = ["David", "Jennifer", "Donnie", "Raymond", "Cynthia", "Mike", "Clayton", "Danny", "Jonathan"]; var hTable = new HashTable(); for (var i = 0; i < someNames.length; ++i) { hTable.put(someNames[i]); } hTable.showDistro();</script></body></html>
这就是碰撞,为避免碰撞,使用betterHash
修改:
function put(data) { // var pos = this.simpleHash(data); var pos = this.betterHash(data); this.table[pos] = data; }
0 0
- javascript数据结构6-字典 散列 集合
- 数据结构:JavaScript实现散列
- 数据结构:JavaScript实现字典
- JavaScript数据结构-字典
- javascript实现字典数据结构
- 数据结构与算法JavaScript - 散列
- 数据结构与算法JavaScript - 字典
- javascript字典数据结构Dictionary实现
- JavaScript数据结构之集合
- JavaScript数据结构-集合
- python 数据结构六 之 字典与集合
- Python字典集合数据结构深入理解
- js数据结构和算法(五)字典和散列(hash)
- 数据结构与算法JavaScript - 集合
- 数据结构JavaScript——集合
- 数据结构——字典(JavaScript)
- 散列 - 链表散列实现字典
- 数据结构之散列
- SQL语句中count(1)count(*)count(字段)用法的区别
- javascript数据结构5-链表2 存放点数据(x,y)
- centos下在线安装配置mysql
- junit示例
- [Caffe]:关于filler type
- javascript数据结构6-字典 散列 集合
- CentOS7修改时区的正确姿势
- 7.Reverse Integer
- Android安装apk时报 INSTALL_FAILED_INSUFFICIENT_STORAGE 错误的解决方法
- javascript数据结构7-二叉搜索树(BST)
- centos下在线安装配置jdk
- ajaxFileUpload+Struts2文件上传
- lucene6.2.1测试用例
- vim常用快捷键