为了学习的目的,我正在编写自己的哈希映射实现。我使用separate chaining with list heads作为我的主题。我该如何改进我自己的哈希映射的实现
这是结构会是什么样子:
| 0 | ---> | 11 | ---> | 33 | ---> | -- | ---> | 121 | ---> | TAIL |
| 1 | ---> | 12 | ---> | 34 | ---> | -- | ---> | 122 | ---> | TAIL |
| - |
| - |
| - |
| D-1 | ---> | -- | ---> | -- | ---> | -- | ---> | -- | ---> | TAIL |
这是链表,其中的一个阵列,
d =大小的数组,
| 11 | =带键的元素; 11和元素插入排序方式
算法:
void Insert(key, value):
int bucket = hash_fn(key); // key % D, for now
// accessing this bucket or array-index in array is O(1)
// insert in linked list at the right position
LL[bucket]->insert(new object(key, value))
bool Lookup(key):
int bucket = hash_fn(key); // key % D, for now
// search for key in LL[bucket]
关注:如果很多的元素被映射到同一个桶中,搜索将不会是O(1),事实上,它可能倾向于O(n)。
我该如何改进?
你几乎读了我的想法,自我平衡树的想法,以获得O(日志(米))的行为。但是,我很欣赏使用一个好的散列函数的智慧。调查散列表的工作方式是一种非常实用的学习体验!谢谢。 – brainydexter 2012-01-19 05:41:40