HashMap Collision Resolution

Separate Chaining

Use data structure (such as linked list) to store multiple items that hash to the same slot

1. use linked list
Collision: insert item into linked list
Find: compute hash value, then do Find on linked list

2. use BST
O(log N) time instead of O(N). But lists are usually small - not worth the overhead of BSTs

Open addressing (or probing or Closed Hashing)

Search for other slots using a second function and store items in first empty slot that is found

Separate chaining can take up a lot of space...

Given an item X, try cells h0(X), h1(X), h2(X), .., hi(X)
hi(X) = (Hash(X) + F(i)) mod TableSize
(Define F(0) = 0)

Linear: F(i) = i
Quadratic: F(i) = i2
Double Hashing: F(i) = i * Hash2(X)

details

 

posted @ 2015-11-10 01:37  树獭君  阅读(246)  评论(0编辑  收藏  举报