我需要实现一个散列算法,其中碰撞处理是通过线性探测完成的。我知道根据定义,哈希表不会存储重复项。我不允许预先处理单词列表文件以首先消除重复项,然后尝试将单词加载到哈希表中。我的类arrayHashWithLinearProbing()是soppose来处理这个部分。以下是我的课程和一些其他信息,以帮助了解我在做什么。 (注意:我正在从文本文件中读取)我需要帮助,以便哈希表不会存储重复项。
import java.io.*;
import java.util.*;
import java.math.*;
public class HashWithLinearProbing {
int tableSize = 423697;
BigInteger ts = new BigInteger(Integer.toString(tableSize));
Hash[] hashTable;
public HashWithLinearProbing() {
hashTable = new Hash[tableSize];
for (int i = 0; i < tableSize; i++) {
hashTable[i] = new Hash();
}
}
public int hashVal(String p) {
String s = "";
for (int i = 0; i < p.length(); i++) {
s = s + (int) p.charAt(i);
}
BigInteger bi = new BigInteger(s);
BigInteger k = bi.remainder(ts);
int j = k.intValue();
return j;
}
public void arrayHashWithLinearProbing(String s) {
// you will implement a hash table using the hashVal() method described
// above.
// Linear probing will use the "next" field to chain all the values that
// have collisions.
// this method will store String s at a linearly probed location
// starting from hashVal(s) in the table
// if s is not already i the table. Essentially, you will not have any
// duplicates.
int hashIndex = hashVal(s);
int previousI = -1;
int i = hashIndex;
// Find the next slot
do {
if (hashTable[i].val.isEmpty()) {
hashTable[i].val = s;
break;
}
Hash hash = hashTable[i];
if (hash.val.equals(s)) {
hash.val = s;
break;
}
previousI = i;
i = (i + 1) % tableSize;
hashTable[previousI].next = i;
} while (i != hashIndex);
}
非常感谢您的帮助