1. Gyanastra
  2. Database Systems
  3. Extendible Hashing Worked Insertions
  4. The problem it solves
Free lesson, no account needed 5 min read

Database Systems · Extendible Hashing Worked Insertions

The problem it solves

Static hashing fixes the number of buckets MM at creation. If the file grows, buckets overflow into chains; if it shrinks, space is wasted. Extendible hashing makes the number of buckets dynamic by hashing keys to a binary string and using only a prefix of that string to choose a bucket. The prefix length is the global depth; it grows and shrinks as needed, and the directory doubles (or halves) accordingly.

Definitions and symbols

SymbolNameMeaning
h(K)h(K)hash valueA binary string of some fixed length LL (e.g. 4 bits). We write it explicitly, e.g. h(5)=0101h(5)=0101.
ggglobal depthNumber of leading bits of h(K)h(K) used by the directory. Directory has 2g2^{g} entries.
lil_ilocal depth of bucket iiNumber of leading bits that all keys in bucket ii share; li≤gl_i \le g always.
bbbucket capacityMax keys per bucket (given in the problem).
dddirectoryArray of 2g2^{g} pointers to buckets.

Bucket-selection rule (the only rule you need): take the first gg bits of h(K)h(K), read them as an integer pp with 0≤p<2g0 \le p < 2^{g}; directory entry pp points to the bucket holding KK.

Key invariant: every key in a bucket with local depth ll has the same first ll bits. Two directory entries point to the same bucket iff they agree in their first ll bits (so 2g−l2^{g-l} entries share one bucket).

Convention warning (state this in the exam)

ConventionWhere usedDirectory index
Most-significant-bits-first (leading prefix)Elmasri–Navathe, Silberschatz, Korth–Sudarshan (the textbook convention; exam questions may use either, so follow the question)first gg bits, read left to right
Least-significant-bits-first (suffix)Some older texts / variantslast gg bits

Both are self-consistent; mixing them mid-problem gives wrong answers. Follow the bit order the question states (textbooks use leading bits; some papers use the last bits, e.g. K mod 2iK \bmod 2^{i}). If nothing is stated, say which one you are using.

Also note the depth naming: Elmasri–Navathe call the global depth dd and the local depth d′d'; Silberschatz uses ii for the number of prefix bits and iji_j for bucket jj's local value. We use gg and ll; the rules are identical.

The split rules (memorise these four lines)

When inserting KK into a full bucket BB (local depth lBl_B):

  1. If lB<gl_B < g (bucket is not at global depth): split BB only. Increment lBl_B to lB+1l_B+1; the two new buckets get local depth lB+1l_B+1; redistribute BB's keys plus KK by their next bit. Directory size unchanged — just repoint the 2g−lB2^{g-l_B} entries that shared BB: those whose bit at position lB+1l_B+1 is 0 point to the new bucket B0B_0, those with 1 point to B1B_1.
  2. If lB=gl_B = g (bucket is at global depth): double the directory first (g←g+1g \leftarrow g+1, directory now 2g2^{g} entries, each old entry duplicated), then split BB as in rule 1. Now lB<gl_B < g holds, so the split is a local one.
  3. No overflow ⇒ no split. Insert into the bucket; nothing else changes.
  4. Duplicate keys: if the key already exists, do nothing (or update) — never split for a duplicate.

Why rule 2 doubles first: after doubling, the old bucket's 2gold−lB=20=12^{g_{\text{old}}-l_B}=2^{0}=1 entry becomes 22 entries, so the split can hand one entry to each new bucket. Doubling is the only operation that changes gg.

Deletion / merge (brief):

  • Delete the key. If the bucket becomes empty and its buddy (the bucket sharing the same first l−1l-1 bits, differing only in bit ll) has the same local depth ll, merge them into one bucket of local depth l−1l-1 and repoint both directory entries.
  • After merging, if every bucket has l<gl < g, halve the directory (g←g−1g \leftarrow g-1). Repeat while the condition holds.
  • Never merge buckets of different local depth, and never let gg drop below the maximum local depth.

The step-by-step method (use this order every time)

  1. Compute h(K)h(K) in binary; write it out fully.
  2. Read the first gg bits → directory index pp.
  3. Follow d[p]d[p] to bucket BB. If BB has room, insert and stop.
  4. BB is full. Compare lBl_B with gg:
    • lB<gl_B < g → split BB (rule 1).
    • lB=gl_B = g → double directory, then split (rule 2).
  5. Redistribute: for each key in BB plus KK, look at bit number lB+1l_B+1 (1-indexed from the left). Bit 0 → bucket B0B_0; bit 1 → bucket B1B_1. Both new buckets get local depth lB+1l_B+1.
  6. Repoint the directory entries that used to point to BB.
  7. If a new bucket is still over capacity (possible when many keys share the next bit), repeat from step 4 on that bucket — this is the "cascading split" case.
Pages 1–2 of 2