15. HashTables_outside

# Delegate operations to a list based map at each cell

This preview shows pages 10–15. Sign up to view the full content.

Delegate operations to a list-based map at each cell: Algorithm  find (k): return  A[h(k)].find(k)  Algorithm  put (k,v): t = A[h(k)].put(k,v)  if  t =  null then  {k is a new key} n = n + 1 return  t Algorithm  erase (k): t = A[h(k)].erase(k) if    null then             {k was found} n = n - 1 return  t ©  2010 Goodrich, Tamassia

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Hash Tables 11 Linear Probing Open addressing : the colliding  item is placed in a different cell  of the table Linear probing:  handles  collisions by placing the  colliding item in the next  (circularly) available table cell Each table cell inspected is  referred to as a “probe” Colliding items lump together,  causing future collisions to  cause a longer sequence of  probes Example: h ( x ) = x mod 13 Insert keys 18, 41,  22, 44, 59, 32, 31,  73, in this order                            0 1 2 3 4 5 6 7 8 9 10 11 12     41     18 44 59 32 22 31 73   0 1 2 3 4 5 6 7 8 9 10 11 12 ©  2010 Goodrich, Tamassia
Hash Tables 12 Search with Linear Probing Consider a hash table  A  that uses linear  probing find ( k ) We start at cell  h ( k ) We probe consecutive  locations until one of the  following occurs An item with key  k  is  found, or An empty cell is found,  or N  cells have been  unsuccessfully probed  Algorithm find ( k ) i h ( k ) p 0 repeat c A [ i ] if c = return null else if c.key () = k return c.value () else i ( i + 1) mod N p p + 1 until p = N return null ©  2010 Goodrich, Tamassia

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Hash Tables 13 Updates with Linear Probing To handle insertions and  deletions, we introduce a  special object, called  AVAILABLE , which  replaces deleted elements erase ( k ) We search for an entry with  key  k   If such an entry  ( k, o )  is  found, we replace it with  the special item  AVAILABLE  and we return  element  o Else, we return  null put ( k, o ) We throw an exception if  the table is full We start at cell  h ( k ) We probe consecutive  cells until one of the  following occurs A cell  i  is found that is  either empty or stores  AVAILABLE , or N  cells have been  unsuccessfully probed We store  ( k, o )  in cell  i ©  2010 Goodrich, Tamassia
Hash Tables 14 Double Hashing Double hashing uses a  secondary hash function  d ( k )

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern