Hash Table (Hash Map)
哈希表是根据key直接进行访问的数据结构(这一结构的实现通常采用数组),它通过把关键码值映射(这个映射函数叫做散列函数)到表中一个位置来访问记录,以加快查找的速度。
a hash table (hash map) is a data structure that implements an associative array abstract data type, a structure that can map keys to values. A hash table uses a has function to compute an index, also called a hash code, into an array of buckets or slots, from which the desired value can be found.
Collision resolution
- Separate chaining (most used): each bucket is independent, insert the record into the bucket when a conflict occurs (has entries with the same index). (i.e. 数组不再直接存储键值对,而是把“链”作为自己的元素)
- linked list
- store the first record of each chain in the slot array itself
- self-balancing binary search tree
- Open addressing: all records are stored in the bucket array itself. When a new record is inserted, the buckets are examined, starting with the hashed-to slot and proceeding in some probe sequence (linear probing / ..), until an unoccupied slot is found. (i.e. 当发生地址冲突时,按照某种方法继续探测哈希表中的其他存储单元)
Load factor
填入表中的元素个数 / 散列表的长度
Dynamic resizing
- resizing directions: shrink / grow
- resizing implementation:
- all-at-once rehashing: enlarging the hash table all at once, which includes allocating a new larger table, removing each entry from the old table, and inserting into the new table.
- rehashing gradually
Reference