1

据此,哈希表中搜索的时间复杂度为 O(1)。

但是,如果发生碰撞,那么显然这应该是 O(1) + 一些东西。

我的问题是:

当你说

get(someKey) 

从散列表中,散列函数应用于 someKey,并直接从该位置检索数据。

但是想象一下分离链接用于冲突解决。并想象 someKey 和 someOtherKey 在我们的散列函数应用于它们之后具有相同的输出。假设它是值“25”。

所以当我说

get(someKey)

我将从位置“25”获取数据。这使它成为 O(1)。伟大的。

然而当我说

get(someOtherKey) 

现在someOtherKey链接到someKey所在的位置。

当对someOtherKey应用散列时,我得到 25。

我如何获得我需要的价值?内部是什么?还有别的表吗?算法流程如何?是否有其他表用于存储所有碰撞?

谢谢你。我希望我的问题很清楚!

4

1 回答 1

3

有许多不同的数据结构可以用于处理冲突。这是一个很好的总结。http://en.wikipedia.org/wiki/Hash_table

The hash function narrows the search down to a single bucket in the data structure. The bucket then contains another data structure for resolving collisions. It can be link to an array, where the keys are maintained in a sorted or unsorted order. The link might be to the first element in a linked list of keys, or to the root node of a b-tree. The important point is that the hashing function very quickly narrows down the scope of the search.

Once the scope is narrowed, some other less-efficient search can be useful to work through the collisions. It's all about the trade-offs. You want a hashing algorithm that gives a large enough range of hashes (and buckets) to minimize collisions, limited to how much memory you can afford. If the collisions are rare, a linear search through a linked list of collisions isn't bad. If there many collisions, then the efficiency of re-sizing arrays for the buckets becomes more important.

于 2013-03-30T15:50:59.837 回答