15. HashTables_outside

Separate chaining let each cell in the table point to

Info iconThis preview shows pages 9–14. Sign up to view the full content.

View Full Document Right Arrow Icon
Separate Chaining:  let  each cell in the table  point to a linked list of  entries that map there Separate chaining is  simple, but requires  additional memory  outside the table 0 1 2 3 4 451-229-0004 981-101-0004 025-612-0001 © 2010 Goodrich, Tamassia
Background image of page 9

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

View Full Document Right Arrow Icon
Hash Tables 10 Map with Separate Chaining 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  t ≠  null then             {k was found} n = n - 1 return  t © 2010 Goodrich, Tamassia
Background image of page 10
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
Background image of page 11

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

View Full Document Right Arrow Icon
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
Background image of page 12
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 
Background image of page 13

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

View Full Document Right Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page9 / 16

Separate Chaining let each cell in the table point to a...

This preview shows document pages 9 - 14. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online