Skip to content
GATEiit

GATE CSE · Databases

File organization and indexing (e.g. B and B+ trees)

How records are packed into blocks, primary, clustering and secondary indexes, multilevel indexes, and B and B+ trees: order, fan-out, height and block-access counts as GATE asks them.

Before you start

  • Search trees →

    B and B+ trees generalise binary search trees to many keys per node.

  • Binary search and hashing →

    Search cost on a sorted file or a single-level index is a binary search over blocks; hashed files use hashing.

  • Logarithms and ceilings

    Every block-count answer is a ceiling, and the number of levels is a ceiling of a logarithm.

Concept notes

1. Records, blocks and file organisation

Disk I/O is counted in blocks. A file of r records of R bytes sits on blocks of B bytes.

  • Blocking factor (unspanned, records do not cross blocks): bfr=⌊B/R⌋.
  • Number of blocks: b=⌈r/bfr⌉.
  • In a spanned organisation records may cross block boundaries, so b=⌈rR/B⌉. Assume unspanned unless told.
Organisation Equality search (block accesses) Notes
Heap (unordered) b/2 on average, b if absent Fast insert at the end
Sorted on a field ⌈log⁡2b⌉ by binary search Slow insert; good range scans on that field
Hashed on a field about 1 (plus overflow) No range queries

GATE counts each block read as one access and ignores CPU time. An index exists to cut the ⌈log⁡2b⌉ or b/2 cost to a few accesses.

2. Single-level indexes: primary, clustering, secondary

An index is a sorted file of entries ⟨key, block pointer⟩. Entries are small, so many fit in a block: the index blocking factor is bfri=⌊B/(K+P)⌋.

Index Built on Entries Dense or sparse
Primary Ordering key field of a sorted file One per data block (block anchor) Sparse
Clustering Ordering non-key field One per distinct value Sparse
Secondary Any non-ordering field One per record (key field) Dense

A dense index has an entry for every search-key value (every record); a sparse index has entries for only some. A file can have at most one primary or clustering index (it is sorted one way), but many secondary indexes.

Search cost with a single-level index: binary search the bi index blocks, then read one data block: ⌈log⁡2bi⌉+1

A secondary index usually has far more entries than a primary one (one per record instead of one per block), so it has more blocks, but it is still much faster than a linear scan of an unsorted file.

3. Multilevel indexes

Binary search halves the index each step. A multilevel index instead treats the index itself as a sorted file and builds a sparse index on it, repeating until the top level fits in one block. Each level divides by the fan-out fo=bfri, which is far bigger than 2.

If the first level has r1 entries, it fills ⌈r1/fo⌉ blocks. The next level has one entry per block of the level below, so each level shrinks by a factor of fo. The number of levels is t=⌈log⁡for1⌉  (at least 1) and a lookup reads one block per level plus the data block: t+1 accesses.

Example: fo=100 and r1=5000 entries. Level 1 has 50 blocks, level 2 has 1 block, so t=2=⌈log⁡1005000⌉ and a lookup costs 3 accesses. Binary search on the 50-block single-level index would cost ⌈log⁡250⌉+1=7.

A static multilevel index is hard to keep sorted under inserts and deletes. B-trees and B+ trees are dynamic multilevel indexes: nodes are kept between half full and full, so the tree stays balanced and shallow.

4. B-trees

A B-tree of order p (here: at most p child pointers per node) satisfies:

  • Every node has at most p children and at most p−1 keys, stored in sorted order.
  • Every internal node except the root has at least ⌈p/2⌉ children; the root has at least 2 (unless it is a leaf).
  • All leaves are at the same level, so the tree is balanced.
  • Every key appears exactly once, in some node, together with its data (record) pointer.

Order from block size. A node holds p block pointers, p−1 keys and p−1 record pointers: p Pb+(p−1)(K+Pr)≤B

Insertion: insert into the correct leaf. If it overflows (has p keys), split it around the middle key, which moves up to the parent; splits can propagate to the root, which is the only way the height grows.

A B-tree can find a key in an internal node and stop early, but range scans must jump up and down the tree. Questions also use other definitions of order (for example minimum degree t with 2t−1 maximum keys), so read the definition given in the question.

5. B+ trees

A B+ tree keeps all keys with record pointers in the leaves; internal nodes hold only keys and child pointers to guide the search. Leaves are linked left to right, so a range query finds the first key and then walks the leaf chain.

Order from block size (internal order p = max children, leaf order pleaf = max key–record-pointer pairs): p Pb+(p−1)K≤Bpleaf(K+Pr)+Pb≤B The extra Pb in a leaf is the next-leaf pointer. With no record pointers inside, an internal node has a higher fan-out than a B-tree node, so the tree is shallower.

Insertion: a leaf split copies the first key of the new right leaf up into the parent (it stays in the leaf); an internal split moves the middle key up.

B-tree B+ tree
Record pointers In every node Only in leaves
Key repeated in internal nodes No Yes (as separators)
Fan-out for the same block Lower Higher
Range query Awkward Walk linked leaves
Search ends Any level Always at a leaf

Height bounds for L levels (root = level 1, leaves = level L): maximum keys =pL−1⋅pleaf; minimum keys =2⌈p/2⌉L−2⌈pleaf/2⌉ for L≥2.

A two-level B+ tree306010203040506070internal: keys + child pointersleaves: every key with its record pointer, linked left to right
Internal nodes only guide the search; the leaves hold every key and are chained for range scans.

Formulas to remember

Blocking factor (unspanned)
bfr=⌊BR⌋
Blocks in a file
b=⌈rbfr⌉
Single-level index searchBinary search on index blocks, then one data block
⌈log⁡2bi⌉+1
Multilevel index levelsr1 = entries in the first level; lookup costs t+1 accesses
t=⌈log⁡for1⌉
B-tree order
p Pb+(p−1)(K+Pr)≤B
B+ tree internal order
p Pb+(p−1)K≤B
B+ tree leaf order
pleaf(K+Pr)+Pb≤B
Minimum children of a non-root internal nodeRoot needs at least 2
⌈p/2⌉

Common mistakes

  • Forgetting the next-leaf pointer when computing the B+ tree leaf order.
  • Using a B+ tree formula for a B-tree: B-tree nodes also hold a record pointer per key.
  • Rounding the order up; the inequality needs the largest integer that fits, so always round down.
  • Counting index entries per record for a primary index; it has one entry per data block.
  • Forgetting the final data-block access after the index search.
  • Mixing order definitions (max children, max keys, minimum degree); use the one the question defines.

Worked examples

Try each step yourself, then reveal the working.

Example 1: B+ tree order and height

numerical

Block size 2048 B, search key 12 B, block pointer 8 B, record pointer 10 B. Find the internal order p and leaf order pleaf of a B+ tree, and the number of levels needed to index 106 records if every node is full.

  1. Step 1: Solve the internal-node inequality.

    Try it on paper first.

Example 2: Primary vs secondary index cost

numerical

A file has 50,000 records of 200 B, sorted on key field K, in 2048 B blocks (unspanned). An index entry is 20 B (12 B key + 8 B pointer). Find the block accesses to find one record by (a) binary search on the file, (b) a single-level primary index on K, (c) a multilevel primary index, (d) a single-level secondary index on another key field.

  1. Step 1: Blocking factor and file blocks.

    Try it on paper first.

Example 3: Why B+ trees beat B-trees on fan-out

numerical

With the sizes of the first example (block 2048 B, key 12 B, block pointer 8 B, record pointer 10 B), find the order of a B-tree and compare it with the B+ tree internal order 103.

  1. Step 1: Write the B-tree node inequality.

    Try it on paper first.

Example 4: Minimum and maximum keys in a 3-level B+ tree

numerical

A B+ tree has internal order p=5 (at most 5 children) and leaf order pleaf=4 (at most 4 keys per leaf). It has exactly 3 levels (root, one internal level, leaves). Find the maximum and minimum number of keys it can hold.

  1. Step 1: Maximum: fill every node.

    Try it on paper first.

Previous year questions

10 official GATE CSE questions on this topic, newest first.

Practise these 10 PYQs →

Topic test

10 questions: up to 4 official PYQs and original practice questions, with GATE marking (MCQ wrong answers lose marks). Score 60% or more to pass. A new set is drawn each time.

Mark as mastered

Loading your progress…

My revision notes

Notes written by the GATEiit team against the official GATE CSE syllabus; being checked by a reviewer. Updated 2026-11-21. Spotted a mistake? Use Report an issue on any question, or contact us.