Java顺序链接散列

时间:2014-03-26 18:18:33

标签: java hash chaining sequential

只是寻找一些帮助/洞察这个问题的问题。可能有人甚至可以指出我正确的方向,最终解决它。

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

(key.hashCode() & 0x7fffffff) % M

无论如何,这个问题...............

按顺序将密钥E A S Y Q U T I O N插入到M = 5列表的初始空表中,使用单独的链接。使用散列函数11 K % M将字母表的第K个字母转换为表索引。

我绝不是哈希专家,但在这个主题工作了几个星期后,这个问题在我看来仍然是完全的胡言乱语。

EDIT 如果它有帮助,这里是哈希表代码:

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)); 

}

}

1 个答案:

答案 0 :(得分:0)

当我解释这个问题时,我不认为你想要调用hashcode方法。这个问题提到了字母表中的第K个字母&#39;。使用它,我会认为&#39; E&#39;是字母表的第4个字母(&#39; A&#39;是第0个字母)。应用他们给出的哈希函数,即(11 * 4) % 5 = 4。所以,&#39; E&#39;被添加为M [4]列表的第一个条目。然后,您将对其他字母重复此操作。

问题并不清楚是否有&#39; Kth字母&#39;基于零或基于一个;即是&#39; A&#39;零信或第一个字母?我假设它是零{,由KeyLetter - 'A'计算。我还假设11 K % M表示(11 * K) % M