`
java-mans
  • 浏览: 11458091 次
文章分类
社区版块
存档分类
最新评论

Hash Table (哈希表)

 
阅读更多

Ahash tableorhash mapis adata structurethat uses ahash functionto map identifying values, known askeys(e.g., a person's name), to their associatedvalues(e.g., their telephone number). A hash table implements anassociative array. The hash function is used to transform the key into the index (thehash) of anarrayelement (theslotorbucket) where the corresponding value is to be sought.

The implementation of this calculation is thehash function,f:

index = f(key, arrayLength)

The hash function calculates anindexwithin the array from the datakey.arrayLengthis the size of the array.

A basic requirement is that the function should provide auniform distributionof hash values.

Load factor

The performance of most collision resolution methods does not depend directly on the numbernof stored entries, but depends strongly on the table'sload factor, the ration/sbetweennand the sizesof its array of buckets.

Separate chaining

In the strategy known asseparate chaining,direct chaining, or simplychaining, each slot of the bucket array is a pointer to alinked listthat contains the key-value pairs that hashed to the same location. Lookup requires scanning the list for an entry with the given key. Insertion requires adding a new entry record to either end of the list belonging to the hashed slot. Deletion requires searching the list and removing the element.


Dynamic resizing

To keep the load factor under a certain limit, e.g. under 3/4, many table implementations expand the table when items are inserted.

Resizing is accompanied by a full or incremental tablerehashwhereby existing items are mapped to new bucket locations.

To limit the proportion of memory wasted due to empty buckets, some implementations also shrink the size of the table—followed by a rehash—when items are deleted. From the point ofspace-time tradeoffs, this operation is similar to the deallocation in dynamic arrays.


Resizing by copying all entries

A common approach is to automatically trigger a complete resizing when the load factor exceeds some thresholdrmax. Then a new larger table isallocated, all the entries of the old table are removed and inserted into this new table, and the old table is returned to the free storage pool. Symmetrically, when the load factor falls below a second thresholdrmin, all entries are moved to a new smaller table.


http://blog.csdn.net/eaglex/article/details/6305997

http://www.360doc.com/content/11/0817/11/18042_141124440.shtml




分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics