How many hash buckets

前端 未结 5 778
刺人心
刺人心 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 11:14

    Depends on the type of hash table you are building. If you are using a fixed array based hash table (as opposed to linked lists for buckets), you should resize the array either when the table is full or when you have hit a max probe count (depending on whether you care more about speed or memory). If you are using linked lists, memory isn't as much of a concern since and don't have to probe for empty spaces, so resizing isn't as big of a deal.

    The key with hash tables is the hashing algorithm, not the number of buckets. Ideally, you always want at most one item in each bucket, so you should ideally be resizing when the number of items in the hash table = the number of buckets. If your data isn't evenly distributed, you are better of with a better hash algorithm than a better resize strategy.

提交回复
热议问题