Hash functions KLA

  • I'm going hash all of you into 10 buckets (0-9) by your birthday
  • The hash functions:
    • By the decade of your birth year
      • hash(birthday) = (year/10) % 10
    • By the last digit of your birth year
      • hash(birthday) = year % 10
    • By the last digit of your birth month
      • hash(birthday) = month % 10
    • By the last digit of your birth day
      • hash(birthday) = day % 10

Example: On Board

  • Key space: integers
     
  • Table size: 10
     
  • hash(k) = k mod 10
    • Technically, hash(k) = k,
      which is then mod'ed by
      the table size of 10
       
  • Insert: 7, 18, 41, 34
     
  • How do we find them?

Another Example

  • Key space: integers
     
  • Table size: 6
     
  • hash(k) = k mod 6
     
  • Insert: 7, 18, 41, 34, 12
     
  • How do we find them?

Separate Chaining

  • All keys that map to the same hash value are kept in a "bucket"
    • This "bucket" is another data structure, typically a linked list

     
  • hash(k) = k mod 10
     
  • Insert: 10, 22, 107, 12, 42