Hash Table (哈希表)

本文深入解析了哈希表和哈希映射的数据结构特性,包括使用哈希函数将标识值(键)映射到关联值的位置(槽或桶),通过哈希函数计算索引,并详细阐述了负载因子、分离链表、动态重装等关键概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值