Skip to main content
Engineering LibreTexts

11.5: Hashing- Open Addressing

  • Page ID
    34698
  • Like separate chaining, open addressing is a method for handling collisions. In Open Addressing, all elements are stored in the hash table itself. So at any point, size of the table must be greater than or equal to the total number of keys (Note that we can increase table size by copying old data if needed).

    • Insert(k): Keep probing until an empty slot is found. Once an empty slot is found, insert k.
    • Search(k): Keep probing until slot’s key doesn’t become equal to k or an empty slot is reached.
    • Delete(k): Delete operation is interesting. If we simply delete a key, then search may fail. So slots of deleted keys are marked specially as “deleted”.
      Insert can insert an item in a deleted slot, but the search doesn’t stop at a deleted slot.

    Open Addressing is done following ways:

    a) Linear Probing: In linear probing, we linearly probe for next slot. For example, typical gap between two probes is 1 as taken in below example also.
    let hash(x) be the slot index computed using hash function and S be the table size

    If slot hash(x) % S is full, then we try (hash(x) + 1) % S
    If (hash(x) + 1) % S is also full, then we try (hash(x) + 2) % S
    If (hash(x) + 2) % S is also full, then we try (hash(x) + 3) % S 
    ..................................................
    ..................................................

    Let us consider a simple hash function as “key mod 7” and sequence of keys as 50, 700, 76, 85, 92, 73, 101.

    Open addressing when collisions occur

    Clustering: The main problem with linear probing is clustering, many consecutive elements form groups and it starts taking time to find a free slot or to search an element.

    "Hashing | Set 3 (Open Addressing)" by Pulkit Goel is licensed under CC BY-SA 4.0