js字典和散列表实例详解
程序员文章站
2022-03-28 20:41:34
...
js字典 字典则是以[键,值]的形式来存储元素。字典也称作映射,本文主要和大家分享js字典和散列表实例详解,希望能帮助到大家。
function Dictionary() { var items = {}; this.has = function(key) { return key in items; } this.set = function(key,value){ items[key] = value; } this.remove = function(Key){ if(this.has(Key)){ delete items[Key]; return true; } return false; } this.get = function(key){ return this.has(Key)?items[Key]:undefined; } this.values = function() { var values = {}; for (var k in items) { //{1} if (this.has(k)) { values.push(items[k]); //{2} } } return values; }; this.size = function(){ return Object.keys(items).length; //{4} }; this.sizeLegacy = function(){ var count = 0; for(var prop in items) { //{5} if(items.hasOwnProperty(prop)) //{6} ++count; //{7} } return count; }; this.getItems = function() { return items; } }
散列表
function HashTable() { var table = []; var loseloseHashCode = function (key) { var hash = 0; for(var i = 0;i<key.length;i++){ hash += key.charCodeAt(i); } return hash % 37; } this.put = function(key,value){ var position = this.loseloseHashCode(key); console.log(position + ' - ' + key); table[position] = value; } this.get = function(key){ return table[this.loseloseHashCode(key)]; } this.remove = function(key) { table[loseloseHashCode(key)] = undefined; }; this.print = function() { for (var i = 0; i < table.length; ++i) { //{1} if (table[i] !== undefined) { //{2} console.log(i + ": " + table[i]);//{3} } } }; }
hash值相同的时候有冲突
19 - Gandalf HashTable.js:12 29 - John HashTable.js:12 16 - Tyrion HashTable.js:12 16 - Aaron HashTable.js:12 13 - Donnie HashTable.js:12 13 - Ana HashTable.js:12 5 - Jonathan HashTable.js:12 5 - Jamie HashTable.js:12 5 - Sue HashTable.js:12 32 - Mindy HashTable.js:12 32 - Paul HashTable.js:12 10 - Nathan HashTable.js:24 5: sue@email.com HashTable.js:24 10: nathan@email.com HashTable.js:24 13: ana@email.com HashTable.js:24 16: aaron@email.com HashTable.js:24 19: gandalf@email.com HashTable.js:24 29: johnsnow@email.com HashTable.js:24 32: paul@email.com
1. 分离链接
分离链接法包括为散列表的每一个位置创建一个链表并将元素存储在里面。它是解决冲突的
最简单的方法,但是它在 HashTable 实例之外还需要额外的存储空间。重写put get remove print'方法
function HashTable() { var table = []; var loseloseHashCode = function (key) { var hash = 0; for(var i = 0;i<key.length;i++){ hash += key.charCodeAt(i); } return hash % 37; } this.put = function(key,value){ var position = loseloseHashCode(key); //undefined创建链表 if(table[position] == undefined) table[position] = new LinkedList(); //undefined不为空 链表后面加元素 table[position].append(new ValuePair(key,value)); } this.get = function(key){ var position = loseloseHashCode(key); if(table[position] !== undefined ){ var current = table[position].getHead(); while(current.next){ if(current.element.key === key) return current.element.value; current = current.next; } if(current.element.key === key) return current.element.value; } return undefined; } this.remove = function(key) { var position = loseloseHashCode(key); if(table[position] !== undefined){ var current = table[position].getHead(); while(current.next){ if(current.element.key === key){ table[position].remove(current.element); if(table[position].isEmpty()) table[position === undefined] return true; } current = current.next; } if(current.element.key === key){ table[position].remove(current.element); if(table[position].isEmpty()) table[position === undefined] return true; } return true; } return fasle; }; this.print = function() { for (var i = 0; i < table.length; ++i) { //{1} if (table[i] !== undefined) { //{2} var current = table[i].getHead(); while(current.next){ console.info("output",current.element.toString()); current = current.next; } console.info("output",current.element.toString());//{3} } } }; //新增的方法 创建node元素 将key,value放入这个对象 var ValuePair = function(key, value){ this.key = key; this.value = value; console.info('[' + this.key + ' - ' + this.value + ']'); this.toString = function() { return '[' + this.key + ' - ' + this.value + ']'; } }; }
2. 线性探查
另一种解决冲突的方法是线性探查。当想向表中某个位置加入一个新元素的时候,如果索引
为index的位置已经被占据了,就尝试index+1的位置。如果index+1的位置也被占据了,就尝试
index+2的位置,以此类推
function HashTable() { var table = []; var loseloseHashCode = function (key) { var hash = 0; for(var i = 0;i<key.length;i++){ hash += key.charCodeAt(i); } return hash % 37; } this.put = function(key,value){ var position = loseloseHashCode(key); if(table[position] == undefined){ table[position] = new ValuePair(key,value); }esle{ var index = position; while(table[index] !== undefined){ index++; } table[index] = new ValuePair(key,value); } } this.get = function(key){ var position = loseloseHashCode(key); if(table[position] !== undefined ){ if(table[position].key === key){ return table[position].value; }else{ var index =++position; while(table[index]===undefined||table[index].key!==key){ index++ } if(table[index].key===key){ return table[index].value; } } } return undefined; } this.remove = function(key) { var position = loseloseHashCode(key); if(table[position] !== undefined ){ if(table[position].key === key){ table[position] = undefined; return true; }else{ var index =++position; while(table[index]===undefined||table[index].key!==key){ index++ } if(table[index].key===key){ table[position] = undefined; return true; } } } return false; }; this.print = function() { for (var i = 0; i < table.length; ++i) { //{1} if (table[i] !== undefined) { //{2} console.info("output",table[i].toString()); } } }; //新增的方法 创建node元素 将key,value放入这个对象 var ValuePair = function(key, value){ this.key = key; this.value = value; console.info('[' + this.key + ' - ' + this.value + ']'); this.toString = function() { return '[' + this.key + ' - ' + this.value + ']'; } }; }
3.创建更好的散列函数
我们实现的“lose lose”散列函数并不是一个表现良好的散列函数,因为它会产生太多的冲
突。如果我们使用这个函数的话,会产生各种各样的冲突。一个表现良好的散列函数是由几个方
面构成的:插入和检索元素的时间(即性能),当然也包括较低的冲突可能性。我们可以在网上
找到一些不同的实现方法,或者也可以实现自己的散列函数
function HashTable() { var table = []; var loseloseHashCode = function (key) { var hash = 5381; for(var i = 0;i<key.length;i++){ hash += hash*33+key.charCodeAt(i); } return hash % 1013; } this.put = function(key,value){ var position = loseloseHashCode(key); table[position] = new ValuePair(key,value); } this.get = function(key){ var position = loseloseHashCode(key); if(table[position] !== undefined ) return table[position].value; return undefined; } this.remove = function(key) { var position = loseloseHashCode(key); if(table[position] !== undefined ){ table[position] = undefined; return true; } return false; }; this.print = function() { for (var i = 0; i < table.length; ++i) { //{1} if (table[i] !== undefined) { //{2} console.info("output",table[i].toString()); } } }; //新增的方法 创建node元素 将key,value放入这个对象 var ValuePair = function(key, value){ this.key = key; this.value = value; console.info('[' + this.key + ' - ' + this.value + ']'); this.toString = function() { return '[' + this.key + ' - ' + this.value + ']'; } }; }
以上就是js字典和散列表实例详解的详细内容,更多请关注其它相关文章!
下一篇: html5实现分层屏幕适配