关于hash冲突的解决
分离链接法:
public class SeparateChainingHashTable<AnyType>{
private static final int DEFAULT_TABLE_SIZE=101;
private List<AnyType> []theLists;
private int currentSize;
public SeparateChainingHashTable()
{this(DEFAULT_TABLE_SIZE);}
public SeparateChainingHashTable(int size){
theLists=new LinkedList[nextPrime(size)];
for(int i=0;i<theLists.length;i++)
theLists[i]=new LinkedList<AnyType>();
}
public void makeEmpty(){
for(int i=0;i<theLists.length;i++)
theList[i].clear();
current=0;
}
public boolean contains(AnyType x){
List<AnyType> whichList=theLists[myhash(x)];
return whichList.contains(x);
}
public void insert(AnyType x){
List<AnyType> whichList=theLists[myhash(x)];
if(!whichList.contains(x)){
whichList.add(x);
if(++currentSize>theLists.length)
rehash();
}
}
public void remove(AnyType x){
List<AnyType> whichList=theLists[myhash(x)];
if(whichList.contains(x)){
whichList.remove(x);
currentSize--;
}
}
public void rehash(){
List<AnyType> [] oldLists=theLists;
theList=new List[nextPrime(2*theList.length)];
for(int j=0;i<theList.length;j++)
theList[i]=new LinkedList<AnyType>();
currentSize=0;
for(int i=0;i<oldLists.length;i++)
for(AnyType item:oldLists[i])
insert(item);
//这里调用的自己写的类的insert方法,而不是对oldList对象进行add
}
}
public int myhash(AnyType x){
int hashVal=x.hashCode();
hashVal=hashVal%theLists.length;
if(hashVal<0)
hashVal=hashVal+theLists.length;
return hashVal;
}
---------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------
平方探测:
public class QuadraticProbingHashTable<AnyType>{
private static final int DEFAULT_TABLE_SIZE=11;
private HashEntry<AnyType> [] array;
private int currentSize;
public static class HashEntry<AnyType>{
public AnyType element;
public boolean isActive;
public HashEntry(AnyType e)
{this(e,true);}
public HashEntry(AnyType e,boolean i)
{element=e;isActive=i;}
}
public QuadraticProbingHashTable()
{this(DEFAULT_TABLE_SIZE);}
public QuadraticProbingHashTable(int size)
{allocateArray(size);makeEmpty();}
public void allocateArray(int arraysize)
{array=new HashEntry(arraysize);}
public void makeEmpty(){
currentSize=0;
for(int i=0;i<array.length;i++)
array[i]=null;
}
public boolean contains(AnyType x){
int currentPos=findPos(x);
return isActive(currentPos);
}
public int findPos(AnyType x){
int offset=1;
int currentPos=myhash(x);
//使用平方探测法进行散列
while(array[currentPos]!=null&&
!array[currentPos].element.equal(x)){
currentPos=currentPos+offset;
offset=offset+2;
if(currentPos>array.length)
currentPos=currentPos-array.length;
}
return currentPos;
}
public boolean isActive(int currentPos){
return array[currentPos]!=null&&
array[currentPos].isActive;
}
public void insert(AnyType x){
int currentPos=findPos(x);
if(isActive(currentPos))
return;
array[currentPos]=new HashEntry<AnyType>(x,true);
if(++currentSize>array.length/2)
rehash();
}
private void rehash(){
HashEntry<AnyType> []oldArray=array;
allocateArray(nextPrime(2*oldArray.length))
currentSize=0;
for(int i=0;i<oldArray.length;i++)
if(oldArray[i]!=null&&oldArray[i].isActive)
insert(oldArray[i].element);
}
public void remove(AnyType x){
int currentPos=findPos(x);
if(isActive(currentPos))
array[currentPos].isActive=false;
}
}
关于hash冲突的解决的更多相关文章
- hash 冲突及解决办法。
hash 冲突及解决办法. 关键字值不同的元素可能会映象到哈希表的同一地址上就会发生哈希冲突.解决办法: 1)开放定址法:当冲突发生时,使用某种探查(亦称探测)技术在散列表中形成一个探查(测)序列.沿 ...
- Hash冲突的解决--暴雪的Hash算法
Hash冲突的解决--暴雪的Hash算法https://usench.iteye.com/blog/2199399https://www.bbsmax.com/A/kPzOO7a8zx/
- Cuckoo Hash——Hash冲突的解决办法
参考文献: 1.Cuckoo Filter hash算法 2.cuckoo hash 用途: Cuckoo Hash(布谷鸟散列).问了解决哈希冲突的问题而提出,利用较少的计算换取较大的空间.占用空间 ...
- Hash冲突的解决方法
虽然我们不希望发生冲突,但实际上发生冲突的可能性仍是存在的.当关键字值域远大于哈希表的长度,而且事先并不知道关键字的具体取值时.冲突就难免会发 生.另外,当关键字的实际取值大于哈希表的长度时,而且表中 ...
- Hash冲突以及解决
哈希函数:它把一个大范围的数字哈希(转化)成一个小范围的数字,这个小范围的数对应着数组的下标.使用哈希函数向数组插入数据后,这个数组就是哈希表. 冲突 当冲突产生时,一个方法是通过系统的方法找到数组的 ...
- hash冲突解决和javahash冲突解决
其实就是四种方法的演变 1.开放定址法 具体就是把数据的标志等的对长度取模 有三种不同的取模 线性探测再散列 给数据的标志加增量,取模 平方探测再散列 给数据的标志平方,取模 随机探测再散列 把数据的 ...
- Map之HashMap的get与put流程,及hash冲突解决方式
在java中HashMap作为一种Map的实现,在程序中我们经常会用到,在此记录下其中get与put的执行过程,以及其hash冲突的解决方式: HashMap在存储数据的时候是key-value的键值 ...
- hash冲突随笔
一:hash表 也叫散列表,以key-value的形式存储数据,就是将需要存储的关键码值通过hash函数映射到表中的位置,可加快访问速度. 二:hash冲突 如果两个相同的关键码值通过hash函数映射 ...
- 链表法解决hash冲突
/* @链表法解决hash冲突 * 大单元数组,小单元链表 */ #pragma once #include <string> using namespace std; template& ...
随机推荐
- 网络流(四)dinic算法
传送门: 网络流(一)基础知识篇 网络流(二)最大流的增广路算法 网络流(三)最大流最小割定理 网络流(四)dinic算法 网络流(五)有上下限的最大流 网络流(六)最小费用最大流问题 转自:http ...
- usb-cam (3)摄像机标定文件-ORB-SLAM标定文件
http://www.luohanjie.com/2017-04-05/the-problem-of-calibration-data-in-orb-slam2.html ORB_SLAM2中标定数据 ...
- python3 is和==
'''is 判断左右两端的数据是否是同一个内存地址== 判断左右两端的数据是否一样'''# s1 = "alex"# s2 = "alex"# print(s1 ...
- day06数据类型----元组、字典、集合
一.元组(tuple): python中将一些不能修改的值称为不可变的,而不可变的列表则被称之为元组. 注意元组一旦被定义则不可修改,因此一般我们不定义空元组. 元组是有序的,可存放多个数据| ...
- Spring配置跨域请求
本文主要是Spring+SpringMVC+MyBatis/MyBatis Plus框架环境,包括SpringBoot同样适用. 1.编写拦截器 package com.interceptor; im ...
- ConcurrentHashMap中的putIfAbsent方法的使用以及返回值的含义
public V putIfAbsent(@NotNull K key, @NotNull V value) putIfAbsent方法主要是在向ConcurrentHashMap中添加键—值对的时候 ...
- VUE2 第五天学习--过渡效果
阅读目录 1.理解VUE---过渡效果 回到顶部 1.理解VUE---过渡效果 1. 过渡的-css-类名会有4个(css) 类名在 enter/leave 在过渡中切换.1. v-enter: 进入 ...
- wifidog源码分析 - wifidog原理
wifidog是一个用于配合认证服务器实现无线网页认证功能的程序,常见的情景就是使用于公共场合的无线wifi接入点,首先移动设备会连接公共wifi接入点,之后会弹出网页要求输入用户名密码,认证过后才能 ...
- Luogu3352 ZJOI2016 线段树 概率、区间DP
传送门 考虑对于每一个位置\(i\),计算所有可能的结果出现的概率. 定义一个区间\([l,r]\)为对于\(x\)的极大区间,当且仅当\(\max \limits _{i=l}^r \{a_i\} ...
- 简单的策略模式Strategy演示
策略模式,即规则在变化之中,结果终归为一. 公司给员工计算工资,如有加班费,差旅费,每个月的生活补帖等等其它费用需要计算.这个费的规则是不尽相同. 不管策略的规则怎样,终归需要计算出一个结果 工资: ...