Lesson 18 +35 XP

B-Tree & B+ Tree: Worked Insertion & Deletion

B-Tree & B+ Tree: Worked Insertion & Deletion

The exam usually gives a list of keys and asks you to build a B-tree or B+ tree of a given order, then delete a few keys. This lesson works through the notes' examples step by step. Notation: [7 | 13] is a node with keys 7 and 13; children are listed below it from left to right.

Rules for a B-Tree of Order m

  • Every node has at most m children and m − 1 keys.
  • Every non-root node has at least ⌈m/2⌉ children (⌈m/2⌉ − 1 keys); the root has at least 2 children unless it is a leaf.
  • All leaves are on the same level.
  • Insertion always goes into a leaf. If the leaf now has m keys (overflow), split it: the median key moves up into the parent, the keys left of it form one node and the keys right of it another. A split can travel up to the root; splitting the root makes the tree one level taller.

For m = 5: max 4 keys, min 2 keys; an overflowing node has 5 keys and the 3rd one goes up.

  1. B-Tree of Order 5-Insert 3, 14, 7, 1, 8, 5, 11, 17, 13, 6, 23, 12, 20, 26, 4, 16, 18, 24, 25, 19
InsertEventTree after the step
3, 14, 7, 1fill the root leaf[1 3 7 14]
8[1 3 7 8 14] overflows → 7 goes up[7] → [1 3], [8 14]
5, 11, 17fit in leaves[7] → [1 3 5], [8 11 14 17]
13[8 11 13 14 17] overflows → 13 up[7 13] → [1 3 5], [8 11], [14 17]
6, 23, 12, 20fit in leaves[7 13] → [1 3 5 6], [8 11 12], [14 17 20 23]
26[14 17 20 23 26] overflows → 20 up[7 13 20] → [1 3 5 6], [8 11 12], [14 17], [23 26]
4[1 3 4 5 6] overflows → 4 up[4 7 13 20] → [1 3], [5 6], [8 11 12], [14 17], [23 26]
16, 18, 24, 25fit in leaves[4 7 13 20] → [1 3], [5 6], [8 11 12], [14 16 17 18], [23 24 25 26]
19leaf [14 16 17 18 19] overflows → 17 up; root [4 7 13 17 20] overflows → 13 up, new rootsee below
TEXT
                          [13]
              /                          \
          [4 | 7]                     [17 | 20]
        /    |     \               /      |       \
    [1 3]  [5 6]  [8 11 12]    [14 16]  [18 19]  [23 24 25 26]

Final tree: height 2 (three levels), 20 keys stored in 9 nodes.

2. The Same Keys in a B-Tree of Order 4

For m = 4 a node holds at most 3 keys; an overflowing node has 4 keys. Using the notes' convention (left 2 keys, the 3rd key goes up, right 1 key):

InsertEvent
1[1 3 7 14] → 7 up: [7] → [1 3], [14]
17[8 11 14 17] → 14 up: [7 14] → [1 3 5], [8 11], [17]
6[1 3 5 6] → 5 up: [5 7 14] → [1 3], [6], [8 11 13], [17]
12[8 11 12 13] → 12 up; root [5 7 12 14] → 12 up, new root
26[17 20 23 26] → 23 up into [14] → [14 23]
18[16 17 18 20] → 18 up → [14 18 23]
TEXT
                         [12]
              /                         \
          [5 | 7]                   [14 | 18 | 23]
        /    |    \             /     |      |      \
   [1 3 4]  [6]  [8 11]      [13]  [16 17] [19 20] [24 25 26]

(A different split convention-right-biased-gives a different but equally valid tree. Always state the rule you use.)

3. Deletion from a B-Tree (order 5, min 2 keys)

Rules

  1. If the key is in a leaf with more than the minimum keys, just remove it.
  2. If the key is in an internal node, replace it by its inorder predecessor (largest key in the left subtree) or successor (smallest key in the right subtree), then delete that key from its leaf.
  3. If a leaf underflows (fewer than 2 keys):
  • Borrow - if an adjacent sibling has more than the minimum, move the parent's separator down into the leaf and the sibling's nearest key up into the parent.
  • Merge - otherwise, merge the leaf, the separator from the parent and the sibling into one node. The parent loses a key and may underflow in turn; if the root becomes empty, the tree shrinks by one level.

Example on the order-5 tree from section 1. Delete 8, 20, 18, 5:

DeleteCaseResult
8leaf [8 11 12] has 3 keys → simple removalleaf becomes [11 12]
20internal key → replace by successor 23 (from [23 24 25 26])right node [17 23] → [14 16], [18 19], [24 25 26]
18leaf [19] underflows; right sibling [24 25 26] can lend → borrow: 23 comes down, 24 goes up[17 24] → [14 16], [19 23], [25 26]
5leaf [6] underflows; both siblings [1 3] and [11 12] are at minimum → merge [1 3] + 4 + [6]left node [4 7] becomes [7] - it underflows too
(cont.)sibling [17 24] is at minimum → merge [7] + 13 + [17 24]; root is now empty and is removedtree height shrinks by one
TEXT
                       [7 | 13 | 17 | 24]
        /           |          |          |          \
   [1 3 4 6]     [11 12]    [14 16]    [19 23]     [25 26]

4. B+ Tree Rules (order m)

  • Internal nodes hold up to m − 1 routing keys and m child pointers; all data keys live in the leaves, which are linked left to right.
  • Non-root internal nodes have at least ⌈m/2⌉ children; each leaf holds at least ⌊m/2⌋ keys.
  • Leaf split: the leaf is divided into two halves and the smallest key of the right leaf is copied up (it stays in the leaf too).
  • Internal split: works like a B-tree split-the middle key moves up and does not stay in either half.

5. B+ Tree of Order 4-the Notes' Insertion Sequence

Insert 5, 9, 1, 3, 4, 59, 65, 45, 89, 29, 68, 108, 165, 298, 219, 569, 37, 47 (max 3 keys per node; a 4-key leaf splits 2 | 2; a 4-key internal node splits left 2, 3rd up, right 1).

InsertEventIndex (internal) keys afterwards
5, 9, 1single leaf (1 5 9)-
3leaf (1 3 5 9) splits → (1 3) (5 9), 5 copied up[5]
4, 59fit[5]
65(5 9 59 65) splits → (5 9) (59 65), 59 up[5 59]
45, 89fit: (5 9 45), (59 65 89)[5 59]
29(5 9 29 45) splits → (5 9) (29 45), 29 up[5 29 59]
68(59 65 68 89) splits → (59 65) (68 89), 68 up; root [5 29 59 68] overflows → 59 moves up, new root[59] → [5 29], [68]
108fit: (68 89 108)
165(68 89 108 165) splits → 108 up[59] → [5 29], [68 108]
298fit: (108 165 298)
219(108 165 219 298) splits → 219 up[59] → [5 29], [68 108 219]
569, 37fit: (219 298 569), (29 37 45)
47(29 37 45 47) splits → (29 37) (45 47), 45 up[59] → [5 29 45], [68 108 219]
TEXT
                                   [59]
                 /                                    \
          [5 | 29 | 45]                        [68 | 108 | 219]
       /     |     |     \                   /     |     |      \
  (1 3 4) (5 9) (29 37) (45 47)  →  (59 65) (68 89) (108 165) (219 298 569)
  └──────────── all leaves are linked left to right for range scans ────────────┘

6. B+ Tree Exercise: Build, then Delete (order 4)

Insert 1, 4, 16, 25, 9, 20, 13, 15, 10, 11, 12:

InsertEventTree
1, 4, 16one leaf(1 4 16)
25split, 16 copied up[16] → (1 4), (16 25)
9, 20fit[16] → (1 4 9), (16 20 25)
13(1 4 9 13) splits, 9 up[9 16] → (1 4), (9 13), (16 20 25)
15fit[9 16] → (1 4), (9 13 15), (16 20 25)
10(9 10 13 15) splits, 13 up[9 13 16] → (1 4), (9 10), (13 15), (16 20 25)
11fit… (9 10 11) …
12(9 10 11 12) splits, 11 up; root [9 11 13 16] overflows → 13 moves upsee below
TEXT
                    [13]
             /               \
        [9 | 11]             [16]
       /    |    \          /     \
   (1 4) (9 10) (11 12)  (13 15) (16 20 25)

Delete 13, 15, 1 (each leaf needs at least 2 keys, each internal node at least 1 key):

  1. Delete 13: leaf (13 15) → (15) underflows. Right sibling (16 20 25) can lend: 16 moves over → (15 16), (20 25); the parent key becomes 20. The root key 13 no longer exists in a leaf, so it is updated to 15.
  2. Delete 15: leaf (15 16) → (16) underflows; sibling (20 25) is at minimum → merge into (16 20 25). The parent [20] loses its key and underflows. Its sibling [9 11] has a spare key → borrow through the root: the child (11 12) moves across, the root key 15 comes down as the separator and 11 goes up to the root → root [11], children [9] and [15].
  3. Delete 1: leaf (1 4) → (4) underflows; sibling (9 10) is at minimum → merge into (4 9 10). Parent [9] becomes empty; its sibling [15] is at minimum → merge [ ] + 11 + [15] = [11 15]. The root is now empty and is removed.
TEXT
           [11 | 15]
         /     |     \
  (4 9 10)  (11 12)  (16 20 25)

A routing key such as 15 may stay in the index even after 15 is deleted from the leaves-it is still a correct signpost (everything left of it is smaller, everything right of it is at least 15).

7. B-Tree vs B+ Tree

BasisB-TreeB+ Tree
Where keys/records liveIn every nodeOnly in leaves (internal nodes are an index)
Duplicated keysNoneSeparator keys repeat in the index
Leaf linksNoLeaves linked as a list
SearchMay stop at an internal nodeAlways ends at a leaf-uniform cost
Range query / sequential scanNeeds an inorder traversalWalk the linked leaves
Fan-outLower (data takes space in internal nodes)Higher, so the tree is shallower
Typical useGeneral in-memory/disk searchDatabase indexes and file systems
🎯 END-TERM EXAM PREP

📝 IPU End-Term Exam Corner (5-10 Marks)

  • Q1. Construct a B-tree of order 5 by inserting 3, 14, 7, 1, 8, 5, 11, 17, 13, 6, 23, 12, 20, 26, 4, 16, 18, 24, 25, 19. Then delete 8, 20, 18 and 5. (10 Marks)
  • Write: the rules (max 4 / min 2 keys), a tree after every split, and the borrow/merge reasoning for every deletion.
  • Q2. Insert 1, 4, 16, 25, 9, 20, 13, 15, 10, 11, 12 into a B+ tree of order 4 and then delete 13, 15, 1. (10 Marks)
  • Q3. Differentiate between a B-tree and a B+ tree. Why are B+ trees preferred for database indexing? (5 Marks)