MCS-208 · Unit 7

Advanced Trees

Section 6 of 9

Block 3 · Unit 7Advanced Trees16 min · Section 6 of 9

7.6B-Tree Insertion, Deletion and Applications

Covers IGNOU MCS-208 §7.4.1, 7.4.2

A B-tree grows in an unusual way: new keys always go into leaves, and when a node overflows it splits and pushes its middle key up. When the root splits, a new root is created, so the tree grows at the top, and all leaves stay on the same level.

Insertion

To insert key kk:

  1. Search for kk to find the leaf where it belongs; insert it there in sorted position.
  2. If the leaf now has mm keys (one too many), split it: the middle key moves up into the parent, the keys to its left form one node and the keys to its right another.
  3. If the parent now overflows, split it too, and so on upwards. If the root splits, the middle key becomes a new root with two children: the height grows by 1.

(Cormen’s version splits every full node on the way down before inserting, so a single downward pass suffices; the resulting trees satisfy the same rules.)

Deletion (outline)

Deleting a key from a leaf may leave it with fewer than ⌈m/2⌉−1\lceil m/2 \rceil - 1 keys (underflow). It is repaired by:

  • borrowing: if a neighbouring sibling has more than the minimum, move a key from the parent down into the node and a key from the sibling up into the parent (a rotation);
  • merging: otherwise merge the node, its sibling and the separating key from the parent into one node; the parent loses a key and may underflow in turn.

A key in an internal node is first replaced by its inorder predecessor or successor (which is in a leaf), and that key is deleted from the leaf. If the root loses its last key, its single child becomes the new root and the height shrinks by 1.

Applications

  • Database indexes: almost every relational database (MySQL InnoDB, PostgreSQL, Oracle, SQL Server) stores indexes as B-trees or B+ trees, giving O(log⁡n)O(\log n) look-up instead of O(n)O(n) for an unindexed scan.
  • File systems: NTFS, ext4 (directory indexes), HFS+/APFS and Btrfs use B-tree variants to find files and blocks.
  • B+ trees (a variant) keep all records in the leaves and link the leaves in a list, so range queries (“all orders from March”) are fast.

Key takeaways

  • Insert into a leaf; on overflow (mm keys) split and move the middle key up.
  • Splits can travel up to the root; a root split adds a new root (height + 1).
  • Deletion repairs underflow by borrowing from a sibling or merging with it.
  • Used in database indexes and file systems; B+ trees add linked leaves for range queries.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

In an order-5 B-tree, a leaf holds [B D F H] and J is inserted. What happens?

Show answer

Answer: C. The leaf splits into [B D] and [H J], and F moves up to the parent.

Five keys B D F H J are one too many; the middle key F goes up and the others form two nodes of 2 keys each.

Question 2 · multiple choice

How does the height of a B-tree increase?

Show answer

Answer: B. The root splits and a new root is created above it.

Growing at the top keeps all leaves on the same level, which is one of the B-tree rules.

Question 3 · multiple choice

During a deletion, a node falls below the minimum number of keys and its sibling also has only the minimum. What is done?

Show answer

Answer: D. Merge the node, the sibling and the separating key from the parent.

Borrowing would make the sibling underflow, so the two nodes are merged; the parent loses one key.

Question 4 · multiple choice

In the final tree of the example, which nodes does a search for B read?

Show answer

Answer: A. [M], [D G], [A C]

B < M: go left to [D G]; B < D: go to the leftmost child [A C]; B is not there and it is a leaf, so B is not in the tree (as the program’s search output shows).

Question 5 · multiple choice

Why do databases often use B+ trees rather than plain B-trees?

Show answer

Answer: D. All records are in the leaves, which are linked together, so range queries can scan the leaves in order.

Internal nodes of a B+ tree still hold keys (as signposts), but the data and a linked leaf level make sequential access fast.

Question 6 · short answer

Insert 10, 20, 30, 40, 50, 60, 70 into an empty B-tree of order 3 (at most 2 keys per node), drawing the tree after each split.

Show worked answer

[10 20]; insert 30 → [10 20 30] overflows: split, 20 moves up: root [20], leaves [10] [30]. Insert 40 → [30 40]. Insert 50 → [30 40 50] overflows: 40 moves up: root [20 40], leaves [10] [30] [50]. Insert 60 → [50 60]. Insert 70 → [50 60 70] overflows: 60 moves up to root [20 40 60], which overflows: 40 moves up to a new root. Final: root [40]; level 2: [20] [60]; leaves [10] [30] [50] [70]. All leaves are on level 3.

↑ ↓ move↵ openTab switch type