1. Gyanastra
  2. Database Systems
  3. Static Hashing Operations Cost and Hash versus B+ Tree
  4. Open addressing: deletion needs tombstones
Free lesson, no account needed 4 min read

Database Systems · Static Hashing Operations Cost and Hash versus B+ Tree

Open addressing: deletion needs tombstones

Structure. No chains. All BB slots live in one array of buckets; on collision at h(K)h(K) we probe h(K)+1,h(K)+2,…h(K)+1, h(K)+2,\dots (linear probing) mod BB until a free slot is found. Each probe of a different bucket is a separate I/O.

2.1 Insertion

Method: probe from h(K)h(K) forward; the first empty slot is where the record goes. Cost = number of probes = number of I/Os (each probed bucket is read; the final one is written).

Worked example 4. B=11B=11, linear probing, h(K)=K mod 11h(K)=K \bmod 11. Insert keys 20, 31, 42 in that order.

  • 20: 20 mod 11=920 \bmod 11 = 9 → slot 9 free. 1 probe.
  • 31: 31 mod 11=931 \bmod 11 = 9 → occupied; probe 10 → free. 2 probes.
  • 42: 42 mod 11=942 \bmod 11 = 9 → occupied; 10 occupied; 0 free. 3 probes.

2.2 The deletion problem

If you simply empty the slot of a deleted key, a later search stops early and wrongly reports "not found". Example: after inserting 20, 31, 42 above, delete 31 (slot 10). Now search for 42: it hashes to 9 (occupied by 20), probes 10 — empty — and stops. 42 is at slot 0 but is never reached. Wrong answer.

Fix: tombstones. Mark the slot deleted (a tombstone), not empty. Search treats a tombstone as "keep probing"; insertion may reuse a tombstone but must remember the first tombstone seen and continue probing to confirm the key is not further along.

Method for delete: probe exactly as for search; on finding the key, mark the slot as a tombstone and write it back. Cost = probes to find + 1 write.

Worked example 5. In the table above, delete 31: probe 9 (20, no), probe 10 (31, found) → mark tombstone. Cost = 2 reads + 1 write = 3 I/Os. Now search 42: probe 9 (no), probe 10 (tombstone → continue), probe 0 (42, found). Correct.

Trap: tombstones accumulate. After many delete/insert cycles the table fills with tombstones, probe sequences lengthen, and search cost rises even though few live records remain. The only cure is a rehash (Section 4). Also: a tombstone may be reused by an insert, but only after the insert has confirmed the key is absent — otherwise you can create duplicate keys.


Why static hashing cannot do ranges or ordered scans

A hash function is deliberately designed so that adjacent keys land in unrelated buckets. Consequences:

  • Range query WHERE age BETWEEN 20 AND 30: you cannot compute which buckets hold ages 20–30. The only correct plan is a full scan of all BB buckets (plus overflow chains): cost ≈B+(overflow blocks)\approx B + (\text{overflow blocks}).
  • Ordered scan / ORDER BY / MIN / MAX: hashing gives no order, so you must read everything and sort: cost = full scan + external sort.
  • Prefix / LIKE 'abc%': same problem — no locality.

What to use instead: a B+ tree (or any ordered index). A B+ tree keeps keys sorted in leaf level and links leaves, so a range [a,b][a,b] costs one root-to-leaf descent plus a sequential scan of the qualifying leaves. That is the whole reason B+ trees exist alongside hashing.

Query typeStatic hashB+ tree
Equality K = vO(1)O(1) expected — bestO(log⁡fn)O(\log_f n) — good
Range a ≤ K ≤ bfull scan, O(B)O(B)descent + leaf scan — best
ORDER BY K, MIN/MAXfull scan + sortleaf scan, already sorted
Prefix matchfull scanrange on prefix

Pages 1–2 of 2