Slippery Rock University Dr. Deborah Whitfield Go Browns!

Chapter 11: Hash Tables
Data Structures and Algorithms in Java
CpSc 374

Ideal Hash Table

Really Ideal Example Ideal Example - Words
English Dictionary (a to zyzzyva) Word Problems Dictionary Example Collision Resolution Strategies

Open Adressing - Linear Probing

Insert Operation
  1. index = h(key)
  2. If A[index] is occupied already

  3. // find the next availabe spot
    1. While (! available(A[++index])) /do nothing/ ;
    2. Place item in location index
    3. Note that if table is full => infinite loop, so check!
    4. index = (index + 1) % A.length(); // circular array
Find Operation
  1. index = h(key)
  2. If A[index] is occupied & key does not match
  3. While (! empty(A[++index])
    1. If key matches return index
    2. Else keep going
    3. Circular increment
  4. Didn't find it
Delete Operation (often not allowed)
  1. index = h(key)
  2. While (! empty(A[++index])
    1. If key matches
      mark as deleted
      return item
    2. Circular increment
  3. return error (null)
  • Note that if table is full => infinite loop, so check!
  • Also note, deleted => now available for an insert
  • Clustering can ocur

    Open Addressing - Quadratic Probing

    Open Addressing - Double Hashing Hash Table Problems

    Hash Table w/ Separate Chaining

    Chaining Considerations Hash Function Strategies Hashing Strings
    public static hashFunc3(String key) { 
       int hashVal = 0;
       for (int j=0; j<key.length(); j++) {
           int letter = key.charAt(j) - 96;  // get char code 
           hashVal = (hashVal * 27 + letter) % arraySize; 
           } // end for 
       return hashVal; 
     } // end hashFunc3() 
    

    WorkSheet