Hash Tables Bryce Boe 2013/08/20 CS24, Summer 2013 C - - PowerPoint PPT Presentation

hash tables
SMART_READER_LITE
LIVE PREVIEW

Hash Tables Bryce Boe 2013/08/20 CS24, Summer 2013 C - - PowerPoint PPT Presentation

Hash Tables Bryce Boe 2013/08/20 CS24, Summer 2013 C Outline Lab 8 Solu@on Hash Tables Hash Tables ADT that has expected running @me


slide-1
SLIDE 1

Hash ¡Tables ¡

Bryce ¡Boe ¡ 2013/08/20 ¡ CS24, ¡Summer ¡2013 ¡C ¡

slide-2
SLIDE 2

Outline ¡

  • Lab ¡8 ¡Solu@on ¡
  • Hash ¡Tables ¡
slide-3
SLIDE 3

Hash ¡Tables ¡

  • ADT ¡that ¡has ¡expected ¡running ¡@me ¡of ¡O(1) ¡

for ¡all ¡opera@ons ¡

  • How ¡can ¡we ¡do ¡this? ¡

– Trade ¡space ¡for ¡running ¡@me ¡

slide-4
SLIDE 4

Hash ¡Func@on ¡

  • Converts ¡a ¡key ¡into ¡an ¡index ¡that ¡fits ¡inside ¡of ¡

the ¡hash-­‑table ¡array ¡

  • Ideally ¡should ¡be ¡uniformly ¡distributed ¡
slide-5
SLIDE 5

Collisions ¡

  • What ¡should ¡we ¡do ¡when ¡collisions ¡occur? ¡

– Linear ¡Probing ¡

  • new_hash ¡= ¡(old_hash ¡+ ¡c) ¡% ¡max_size ¡
  • Can ¡result ¡in ¡clustering, ¡but ¡beXer ¡locality ¡of ¡reference ¡

– Quadra@c ¡Probing ¡

  • new_hash ¡= ¡(old_hash ¡+ ¡c*i2) ¡% ¡max_size ¡
  • May ¡not ¡examine ¡all ¡buckets ¡

– Buckets ¡and ¡chaining ¡

  • Store ¡a ¡linked ¡list ¡at ¡each ¡loca@on ¡to ¡store ¡collisions ¡in ¡
slide-6
SLIDE 6

Dele@ons ¡and ¡probing ¡

  • How ¡can ¡we ¡handle ¡dele@ons ¡of ¡items ¡when ¡

performing ¡probing? ¡

– Mark ¡the ¡“empty” ¡space ¡as ¡deleted ¡

  • Frees ¡up ¡the ¡space ¡and ¡does ¡not ¡break ¡previous ¡entries ¡
  • Becomes ¡inefficient ¡over@me ¡

– Acceptable ¡due ¡to ¡re-­‑sizing ¡the ¡hash ¡table ¡