js数据结构与算法——集合,字典,哈希

来源:互联网 发布:淘宝页头制作视频教程 编辑:程序博客网 时间:2024/05/16 03:10

集合:无序,不重复 ,以[值,值]形式存储数据

字典:以[键,值]形式存储  映射 Map

哈希:散列函数的作用是给定一个键值,然后返回值在表中的地址

 

集合:

var Set=function(){
var items={};
this.hasValue=function(value){
return items.hasOwnProperty(value);
};
this.add=function (value){
if(!this.hasValue(value)){
items[value]=value;
return true;
}
return false;
};
this.remove=function(value){
if(this.hasValue(value)){
delete items[value];
return true;
}
return false;
};
this.clear=function(){
return items={};
};
this.size=function (){
return Object.keys(items).length;
};
this.values=function(){
return Object.keys(items);
};
this.union =function (otherSet) {
var unionSet = new Set();
var values = this.values();
for (vari =0; i < values.length;i++) {
unionSet.add(values[i]);
}
values = otherSet.values();
for (vari =0; i < values.length;i++) {
unionSet.add(values[i]);
}
return unionSet;
};
this.intersection =function (otherSet) {
var intersectionSet = new Set();
var values = this.values();
for (vari =0; i < values.length;i++) {
if (otherSet.has(values[i])) {
intersectionSet.add(values[i]);
}
}
return intersectionSet;
};
this.difference =function (otherSet) {
var differenceSet = new Set();
var values = this.values();
for (vari =0; i < values.length;i++) {
if (!otherSet.has(values[i])) {
differenceSet.add(values[i]);
}
}
return differenceSet;
};
this.subset =function (otherSet) {
if (this.size() >otherSet.size()) {
return false;
} else {
var values = this.values();
for (vari =0; i < values.length;i++) {
if (!otherSet.has(values[i])) {
return false;
}
}
return true;
}
};

}
-------------------------------------------------------------------------------------------------------------------------
字典:
function Dictionary(){
var items={};
this.hasValue=function(key){
return keyinitems;
};
this.set=function(key,value){
items[key]=value;
};
this.remove=function(key){
if(this.hasValue(key)){
delete items[key];
return true;
} else {
return false;
}
};
// 查找给定的值,并返回索引
this.get=function(value){
return this.hasValue(value)?items[value]:undefined;
};
// 以数组的形式返回所有值
this.values=function(){
var items={};
for(varkin items){
if(this.hasValue(k)){
values.push(items[k]);
}
}
return values;
};

this.clear=function(){
return items={};
};
this.size=function (){
return Object.keys(items).length;
};
this.values=function(){
return Object.keys(items);
};

}
---------------------------------------------------------------------------------------------------------------------------
哈希:
function HashTable(){
var table=[];
var loseloseHashCode =function(key){
var hash=0;
for (vari=0;i<key.length;i++){
hash+=key.charCodeAt(i);
}
return hash %37;
};
this.put=function(key,value){
var position=loseloseHashCode(key);
table[position]=value;
console.log(position +'-'+key);
};
this.get=function(key){
return table[loseloseHashCode(key)];
};
this.remove=function(key){
return table[loseloseHashCode(key)]=undefined;
};
// 对于 HashTable 类来说,我们不需要像 ArrayList 类一样从 table 数组中将位置也移除。由
// 于元素分布于整个数组范围内,一些位置会没有任何元素占据,并默认为 undefined 值。我们也
// 不能将位置本身从数组中移除(这会改变其他元素的位置),否则,当下次需要获得或移除一个
// 元素的时候,这个元素会不在我们用散列函数求出的位置上。
};
var hash =newHashTable();
hash.put('Gandalf','gandalf@email.com');
hash.put('John','johnsnow@email.com');
hash.put('Tyrion','tyrion@email.com');
console.log(hash.get('Gandalf'));
console.log(hash.get('Loiane'));
hash.remove('Gandalf');
console.log(hash.get('Gandalf'));


我们实现的“lose lose”散列函数并不是一个表现良好的散列函数,因为它会产生太多的冲
突。如果我们使用这个函数的话,会产生各种各样的冲突。一个表现良好的散列函数是由几个方
面构成的:插入和检索元素的时间(即性能),当然也包括较低的冲突可能性。我们可以在网上
找到一些不同的实现方法,或者也可以实现自己的散列函数。
另一个可以实现的比“lose lose”更好的散列函数是djb2:
var djb2HashCode = function (key) {
var hash = 5381;
for (var i = 0; i < key.length; i++) {
hash = hash * 33 + key.charCodeAt(i);
}
return hash % 1013;
};


****************
处理冲突:
线性探查:当想向表中某个位置加入一个新元素的时候,如果索引为index的位置已经被占据了,就尝试index+1的位置。如果index+1的位置也被占据了,就尝试
index+2的位置,以此类推。
this.put = function(key, value){
var position = loseloseHashCode(key); // {1}
if (table[position] == undefined) { // {2}
table[position] = new ValuePair(key, value); // {3}
} else {
var index = ++position; // {4}
while (table[index] != undefined){ // {5}
index++; // {6}
}
table[index] = new ValuePair(key, value); // {7}
}
};

this.get = function(key) {
var position = loseloseHashCode(key);
if (table[position] !== undefined){ //{8}
if (table[position].key === key) { //{9}
return table[position].value; //{10}
} else {
var index = ++position;
while (table[index] === undefined
|| table[index].key !== key){ //{11}
index++;
}
if (table[index].key === key) { //{12}
return table[index].value; //{13}
}
}
}
return undefined; //{14}
};

阅读全文
0 0
原创粉丝点击