Java顺序链式哈希

Java顺序链式哈希,java,hash,chaining,sequential,Java,Hash,Chaining,Sequential,只是想对这个问题寻求一些帮助/见解……也许有人甚至可以为我指出最终解决这个问题的正确方向 我目前有一个使用标准哈希函数的完整的单独链接哈希表算法: (key.hashCode() & 0x7fffffff) % M 无论如何,问题是 使用单独的链接,按此顺序将键ea S Y Q U T I O N插入M=5列表的初始空表中。使用哈希函数11k%M将字母表的第K个字母转换为表索引 我决不是一个散列专家,但在这个主题上工作了几个星期后,这个问题在我看来仍然完全是胡言乱语 编辑 如果有帮助,

只是想对这个问题寻求一些帮助/见解……也许有人甚至可以为我指出最终解决这个问题的正确方向

我目前有一个使用标准哈希函数的完整的单独链接哈希表算法:

(key.hashCode() & 0x7fffffff) % M
无论如何,问题是

使用单独的链接,按此顺序将键
ea S Y Q U T I O N
插入
M=5
列表的初始空表中。使用哈希函数
11k%M
将字母表的第K个字母转换为表索引

我决不是一个散列专家,但在这个主题上工作了几个星期后,这个问题在我看来仍然完全是胡言乱语

编辑 如果有帮助,以下是哈希表代码:

public class SeparateChainingHashST<Key, Value> {
private static final int INIT_CAPACITY = 4;


private int N;                                // number of key-value pairs
private int M;                                // hash table size
private SequentialSearchST<Key, Value>[] st;  // array of linked-list symbol tables


// create separate chaining hash table
public SeparateChainingHashST() {
    this(INIT_CAPACITY);
}

// create separate chaining hash table with M lists
public SeparateChainingHashST(int M) {
    this.M = M;
    st = (SequentialSearchST<Key, Value>[]) new SequentialSearchST[M];
    for (int i = 0; i < M; i++)
        st[i] = new SequentialSearchST<Key, Value>();
} 

// resize the hash table to have the given number of chains b rehashing all of the keys
private void resize(int chains) {
    SeparateChainingHashST<Key, Value> temp = new SeparateChainingHashST<Key, Value>(chains);
    for (int i = 0; i < M; i++) {
        for (Key key : st[i].keys()) {
            temp.put(key, st[i].get(key));
        }
    }
    this.M  = temp.M;
    this.N  = temp.N;
    this.st = temp.st;
}

// hash value between 0 and M-1
private int hash(Key key) {
    return (key.hashCode() & 0x7fffffff) % M;
} 

// return number of key-value pairs in symbol table
public int size() {
    return N;
} 

// is the symbol table empty?
public boolean isEmpty() {
    return size() == 0;
}

// is the key in the symbol table?
public boolean contains(Key key) {
    return get(key) != null;
} 

// return value associated with key, null if no such key
public Value get(Key key) {
    int i = hash(key);
    return st[i].get(key);
} 

// insert key-value pair into the table
public void put(Key key, Value val) {
    if (val == null) { delete(key); return; }

    // double table size if average length of list >= 10
    if (N >= 10*M) resize(2*M);

    int i = hash(key);
    if (!st[i].contains(key)) N++;
    st[i].put(key, val);
} 

// delete key (and associated value) if key is in the table
public void delete(Key key) {
    int i = hash(key);
    if (st[i].contains(key)) N--;
    st[i].delete(key);

    // halve table size if average length of list <= 2
    if (M > INIT_CAPACITY && N <= 2*M) resize(M/2);
} 

// return keys in symbol table as an Iterable
public Iterable<Key> keys() {
    Queue<Key> queue = new Queue<Key>();
    for (int i = 0; i < M; i++) {
        for (Key key : st[i].keys())
            queue.enqueue(key);
    }
    return queue;
} 


public static void main(String[] args) { 
    SeparateChainingHashST<String, Integer> st = new SeparateChainingHashST<String, Integer>();
    for (int i = 0; !StdIn.isEmpty(); i++) {
        String key = StdIn.readString();
        st.put(key, i);
    }

    // print keys
    for (String s : st.keys()) 
        StdOut.println(s + " " + st.get(s)); 

}
公共类分隔hashst{
专用静态最终int初始容量=4;
private int N;//键值对的数目
私有int M;//哈希表大小
private SequentialSearchST[]st;//链表符号表数组
//创建单独的链接哈希表
公共分离主义{
这(初始容量);
}
//使用M个列表创建单独的链接哈希表
公共分离机CAININGHASHST(int M){
这个,M=M;
st=(SequentialSearchST[])新的SequentialSearchST[M];
for(int i=0;i=10,则表大小加倍
如果(N>=10*M)调整大小(2*M);
int i=散列(键);
如果(!st[i].包含(键))N++;
st[i].put(key,val);
} 
//如果键在表中,则删除键(和关联的值)
公共无效删除(密钥){
int i=散列(键);
如果(st[i].包含(键))N--;
st[i]。删除(键);

//如果list INIT_CAPACITY的平均长度&&N将表大小减半在我解释这个问题时,我认为您不想调用hashcode方法。这个问题提到了“字母表的第k个字母”。使用它,我会假设“E”是字母表的第4个字母(其中“A”是第0个)。应用它们给出的散列函数,即
(11*4)%5=4
。因此,“E”被添加为列表中M[4]处的第一个条目。然后对其他字母重复此操作


问题不清楚“第K个字母”是以零为基础还是以一为基础;即“A”是第0个字母还是第一个字母?我假设它是第0个字母,由
关键字字母-“A”
计算。我还假设
11k%m
表示
(11*K)%M

这不是一个真正的问题,而是一个指令。你的问题是什么,你在寻找什么样的答案?@hatchet这是一个指令,我想遵循该指令就是“问题”,成功地做到这一点就是“答案”。我只是不确定如何理解它,以及它到底要我做什么。谢谢。这很有帮助,我没有这样想过。不过,在这一点上,java中有没有一种快速甚至内置的方法来查找字母的字母数?@user3245237-您可以直接进行减法,如
int k='E'-'a';
当然,如果你不得不担心大写/小写、非ASCII字符等,这可能会使它变得复杂。但是如果你只是在说A-Z,它应该可以工作。