Sub topic 4
Heap Files: Cheap Insertion, Linear Search
Assume you know basic records, disk blocks, and arrays. You should also know how to calculate a ceiling and understand that a disk reads a whole block rather than one record at a time.
A heap file stores records in no particular order. New records usually go into any block with free space, or into a new block at the end. A heap file is also called an unordered file.
Blocking factor and file size
Let:
Bbe the block size in bytes.Rbe the fixed record size in bytes.rbe the number of records.
For unspanned records, a record cannot cross a block boundary. The blocking factor is the number of records that fit in one block:
bfr = floor(B / R)
The number of data blocks is:
b = ceiling(r / bfr)
For example, if B = 1024 bytes and R = 100 bytes:
bfr = floor(1024 / 100) = 10- A file containing
30,000records needsceiling(30,000 / 10) = 3,000blocks.
The unused space in each block is a consequence of unspanned storage. Here, each full block leaves 24 bytes unused.
import math
def heap_file_size(block_size, record_size, record_count):
records_per_block = block_size // record_size
data_blocks = math.ceil(record_count / records_per_block)
return records_per_block, data_blocks
bfr, blocks = heap_file_size(1024, 100, 30000)
print("Blocking factor:", bfr)
print("Data blocks:", blocks)Search and update costs
A heap file has no ordering that a search algorithm can use. To find a record by key, the system may need to examine blocks from the beginning until it finds the record.
| Operation | Typical block accesses | Reason |
|---|---|---|
| Search for a record that is absent | `b` | Every block must be checked |
| Successful search, average case | approximately `b / 2` | The record is equally likely to be in any block |
| Insert with known free space | about 1 | Read or write a suitable block |
| Insert when no space exists | about 1 new block write | Append a new block |
| Delete after locating the record | at least the search cost | The record must first be found |
A successful search may be described more precisely as (b + 1) / 2 block accesses when every block is equally likely. GATE questions often accept or expect the simpler average approximation b / 2.
Deletion can mark a record as deleted. This avoids moving other records but creates unused holes. Periodic reorganization can reclaim those holes, but reorganization reads and writes many blocks.
How insertion works
The file manager can keep a free-space list or a directory describing which blocks have room. With that metadata, insertion does not require scanning the whole file. It selects a block, writes the record into a free slot, and updates the block metadata.
Heap files work well when applications frequently insert records and do full scans. They work poorly for repeated searches by a field unless an index provides a faster path.
Exercise
A heap file has 2,400 records. Each block holds 20 records. How many blocks are required, and what is the average number of block accesses for a successful search?
Show answer
The file requires 2,400 / 20 = 120 blocks. The average successful search examines about 120 / 2 = 60 blocks, or more precisely 60.5 blocks when using (b + 1) / 2.