How many hash buckets

前端 未结 5 780
刺人心
刺人心 2021-01-31 10:29

If I notice that a hash table (or any other data structure built on a hash table) is filling up, at what point should you build a new table with more buckets. And given n items

5条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-31 10:57

    There are typically two types of hashtables: open and closed.

    In an open hashtable you find the right bucket based on the hash, and then build a list of items hanging off that bucket.

    In a closed hashtable you find the initial bucket using the hash value, and if it is occupied you probe for the next value. In the simplistic case you can do this by looking for the next free bucket, or you can create a second hash value from your item and step by that (though you must ensure that this is prime modulo the hash tables size so you will visit all the buckets).

    An open hashtable typically isn't resized. You set the initial size to be what you feel is reasonable for the problem. As others have pointed out you could resize on an open hashtable, but reasoning about the performance of this data structure now becomes very hard. If you resize when the length of a given bucket is L then you might end up resizing on just L items in the whole hashtable, which is very inefficient.

    A closed hashtable is resized when the load factor (no. of items in the hashtable / no. of buckets) hits some predefined value. I tend to use 80%, but the exact value is unlikely to be too critical.

    The benefit of a closed hashtable is that the amortized cost of inserting an item is always O(1) (assuming a good hash function). Inserting a particular item might be O(N) due to the cost of resizing, but that is done very infrequently.

提交回复
热议问题