How to implement a dynamic-size hash table?

前端 未结 1 1373
执笔经年
执笔经年 2021-02-20 18:54

I know the basic principle of the hash table data structure. If I have a hash table of size N, I have to distribute my data into these N buckets as evenly as possible.

B

相关标签:
1条回答
  • 2021-02-20 19:07

    See the Dynamic resizing section of the Hash table article on Wikipedia.

    The usual approach is to use the same logic as a dynamic array: have some number of buckets and when there is too much items in the hash table, create a new hash table with a larger size and move all the items to the new hash table.

    Also, depending on the type of hash table, this resizing might not be necessary for correctness (i.e. it would still work even without resizing), but it is certainly necessary for performance.

    0 讨论(0)
提交回复
热议问题