【问题标题】:Java sequential chaining hashingJava顺序链散列
【发布时间】:2014-03-26 18:18:33
【问题描述】:

只是寻找一些帮助/了解这个问题的问题..也许有人甚至可以为我指出正确的方向以最终解决它。

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

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

不管怎样,问题......

使用单独的链接将键 E A S Y Q U T I O N 按该顺序插入到最初为空的 M = 5 列表表中。使用哈希函数11 K % 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)); 

}

}

【问题讨论】:

  • 这不是一个真正的问题,而是一个指令。你的问题是什么,你想要什么样的答案?
  • @hatchet 这是一个指令,我认为遵循该指令是“问题”,成功地这样做是“答案”。我只是不确定如何遵循它,以及它要求我做什么

标签: java hash chaining sequential


【解决方案1】:

在我解释问题时,我认为您不想调用 hashcode 方法。问题提到了“字母表中的第 K 个字母”。使用它,我会假设'E'是字母表的第4个字母('A'是第0个)。应用他们给出的哈希函数,那就是(11 * 4) % 5 = 4。因此,“E”作为第一个条目添加到 M[4] 处的列表中。然后,您将对其他字母重复此操作。

“第K个字母”是从零开始还是从一开始的问题尚不清楚;即“A”是第零个字母还是第一个字母?我假设它是由KeyLetter - 'A' 计算的零值。我还假设11 K % M 表示(11 * K) % M

【讨论】:

  • 好的,谢谢。这有很大帮助,没想到那样。然而,在那张纸条上,Java中有一个快速甚至内置的方法来查找字母的字母数字吗?
  • @user3245237 - 你可以只减去,如int k = 'E' - 'A';。当然,如果您不得不担心大写/小写、非 ASCII 字符等可能会使它复杂化。但如果你只是在谈论 A-Z,它应该可以工作。
【解决方案2】:

这是一个例子。首先,我们看一下公式:

(K * 13) % M

地点:

K = position of the letter in the alphabet (starting from 1).

13 = constant

M = fixed size of our array (in this case we are going to imagine is 6, starts from 0).

E    (5 * 13) % 6 = 5
A    (1 * 13) % 6 = 1
S    (19 * 13) % 6 = 1
Y    (25 * 13) % 6 = 1
Q    (17 * 13) % 6 = 5
U    (21 * 13) % 6 = 3
E    (5 * 13) % 6 = 5
S    (19 * 13) % 6 = 1
T    (20 * 13) % 6 = 2
I    (9 * 13) % 6 = 3
O    (15 * 13) % 6 = 3
N    (14 * 13) % 6 = 2

如果我们想在一个固定长度为 6 的数组中可视化这些结果(请记住,我们从 0 开始计数),我们将以“S N O E”结尾。如果数值因公式而重复,则交换。

0  1  2  3  4  5
               E         
   A           E
   S           E
   Y           E
   Y           Q
   Y     U     Q
   Y     U     E
   S     U     E
   S  T  U     E
   S  T  I     E
   S  T  O     E
   S  N  O     E

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-06
    • 2020-05-29
    • 1970-01-01
    • 2018-06-26
    • 2017-03-27
    • 2015-12-13
    • 1970-01-01
    • 2018-10-29
    相关资源
    最近更新 更多