使用链接的快速哈希表
尝试更好地理解哈希表上的链接。寻求一个简单的表,该表对一个值进行哈希处理,并且仅将一个整数与哈希值相关联。这是所讨论问题的示例代码......
/* hash string value return int */
public int hashFunction(String D) {
char[] Thing = D.toCharArray();
for(int i=0; i < Thing.length; i++){
index += Thing[i]; }
return index % TABLE_SIZE;
}
/* hash string value return void */
public void hashFunction(String D) {
char[] Thing = D.toCharArray();
for(int i=0; i < Thing.length; i++){
index += Thing[i];}
int hash = index % TABLE_SIZE;
if(table[hash] == null){
table[hash] = new HashEntry( Level, Value );
}
else{
table[hash].setNext(nd);
}
}
/* miscellaneous code snippet */
if(table[hash] == null){
table[hash] = new HashEntry();
}
else if (Data.compareTo("VAR") == 0) {
Data = inFile.next();
char[] Thing = Data.toCharArray();
for(int i=0; i < Thing.length; i++){
hash += Thing[i];}
hash = hash % TABLE_SIZE;
hm.setIntoHashTable(hash);
Data = inFile.next();
if(Data.compareTo("=") == 0) {
Data = inFile.next();
int value = Integer.parseInt(Data);
hm.getIntoHashTable(he.setValue(value));
}
}
Attempting to better understand chaining on a hashtable. Seeking a simple table that hashes a value and only associates one integer to the value hashed. Here is sample code of the problem in question...
/* hash string value return int */
public int hashFunction(String D) {
char[] Thing = D.toCharArray();
for(int i=0; i < Thing.length; i++){
index += Thing[i]; }
return index % TABLE_SIZE;
}
/* hash string value return void */
public void hashFunction(String D) {
char[] Thing = D.toCharArray();
for(int i=0; i < Thing.length; i++){
index += Thing[i];}
int hash = index % TABLE_SIZE;
if(table[hash] == null){
table[hash] = new HashEntry( Level, Value );
}
else{
table[hash].setNext(nd);
}
}
/* miscellaneous code snippet */
if(table[hash] == null){
table[hash] = new HashEntry();
}
else if (Data.compareTo("VAR") == 0) {
Data = inFile.next();
char[] Thing = Data.toCharArray();
for(int i=0; i < Thing.length; i++){
hash += Thing[i];}
hash = hash % TABLE_SIZE;
hm.setIntoHashTable(hash);
Data = inFile.next();
if(Data.compareTo("=") == 0) {
Data = inFile.next();
int value = Integer.parseInt(Data);
hm.getIntoHashTable(he.setValue(value));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当索引发生冲突时,就会发生链接。
假设您有一个 TABLE_SIZE=10
现在您必须存储字符串
abc
,因此您得到的哈希值为4
现在当您要存储字符串
cba
时,最终会得到相同的哈希4
因此,现在要在同一索引处存储
cba
,您将在索引4
处创建并存储一个 List。该列表将包含
abc
和bca
。这个列表称为链
,这种在同一散列中存储多个值的过程称为链接
。伪代码如下所示:
该表将声明为:
您可能必须抑制警告,因为数组和泛型运行不顺利。
Well the chaining occurs when you have collision in the indexes.
Lets assume you have a TABLE_SIZE=10
Now you have to store string
abc
, so you get hash as4
Now when you gonna store string
cba
, you end up with same hash4
So now to store
cba
at the same index, you will create and store a List at index4
.The List will contain both
abc
andbca
. This List is calledchain
and this process of storing multiple values at the same hash is calledChaining
.Pseudo code look like:
The table will be declared as:
You might have to suppress warning as arrays and generics dont go smoothly.