CMPS340   File Processing
B-tree Insertion/Deletion Examples

 

Consider the B-tree T of order 4 illustrated below. (Due to typesetting restrictions, the nodes at the leaf level appear to be on two different levels.)

For each operation in the list (a) through (f), show the B-tree that results from performing that operation on T. (Each operation is to be applied to the tree T illustrated below, not to the tree resulting from applying all the previous operations to T.

Assume, in carrying out the operations, that redistribution is used whenever possible. That is, split an overflowing node only if all its adjacent siblings are full. Similarly, merge two nodes only if all adjacent siblings of the underflowing node are on the verge of underflowing.

(a) insert 23
(b) delete 12
(c) insert 14
(d) delete 63
(e) insert 41
(f) delete 96

                            +----+----+----+
                            | 24 | 63 | 95 |
                            +----+----+----+
                           /    /      \    \ 
              +-----------+    /        \    +---------------------+
             /                /          +------+                   \ 
            /                /                   \                   \
  +---+----+        +----+----+----+              +----+----+       +----+
  | 6 | 19 |        | 34 | 50 | 58 |              | 70 | 85 |       | 98 |
  +---+----+        +----+----+----+              +----+----+       +----+
  |   |    |        |    |     \    \             |    |    |       |    |
  |   |    |        |    |      \    +---+        |    |    |       |    |
  |   |    |        |    |       +----+   \       |    |    |       |    |
+---+ | +----+----+ |+----+----+----+ | +----+  +----+ | +----+  +----+ +----+
| 2 | | | 20 | 21 | || 39 | 44 | 48 | | | 61 |  | 67 | | | 88 |  | 96 | | 99 |
+---+ | +----+----+ |+----+----+----+ | +----+  +----+ | +----+  +----+ +----+
      |             |                 |                |
+---+----+----+  +----+----+----+  +----+----+----+  +----+----+
| 9 | 12 | 16 |  | 27 | 30 | 32 |  | 52 | 55 | 57 |  | 75 | 80 |
+---+----+----+  +----+----+----+  +----+----+----+  +----+----+

Solutions:
    +--+--+--+
(a) |20|21|23|       (simple insertion into leaf node; no overflow)
    +--+--+--+

    +--+--+
(b) | 9|16|          (simple deletion from leaf node; no underflow)
    +--+--+

(c)  (insertion causes overflow; redistribution in either of two directions
      is possible)

            +--+--+                  *               +--+--+
            | 9|19|                  *               | 6|16|
            +--+--+                  *               +--+--+
           /   |   \                or              /   |   \
          /    |    \                *             /    |    \                
         /     |     \               *            /     |     \              
  +--+--+ +--+--+--+  +--+--+        *        +--+ +--+--+--+  +--+--+--+
  | 2| 6| |12|14|16|  |20|21|        *        | 2| | 9|12|14|  |19|20|21|
  +--+--+ +--+--+--+  +--+--+        *        +--+ +--+--+--+  +--+--+--+  

(d) To delete a key at an internal node, first replace it with either the
    largest key in its left subtree or the smallest key in its right subtree.
    Then delete the key at the leaf, which we know how to do.  In this case,
    underflow results, forcing redistribution.

       +--+--+--+            *          +--+--+--+
       |24 61 95|            *          |24 67 95|
       +--+--+--+            *          +--+--+--+
          |   \              *             |   \
          |    \             *             |    \
    +--+--+--+  +--+--+      *       +--+--+--+  +--+--+
    |34 50 57|  |70 85|     or       |34 50 58|  |75 85|
    +--+--+--+  +--+--+      *       +--+--+--+  +--+--+
          |   \              *                  /   |   \
          |    \             *                 /    |    \
      +--+--+  +--+          *               +--+  +--+  +--+
      |52 55|  |58|          *               |70|  |80|  |88|
      +--+--+  +--+          *               +--+  +--+  +--+

(e) Insertion of 41 into the leaf containing 39, 44, and 48 results in
    overflow.  Because no adjacent sibling is less than full, the node is
    split, with 44 being promoted into the node containing 34, 50, and 58,
    which overflows as a consequence.  Redistribution at this level is
    possible, because there is a non-full adjacent sibling (two, in fact).


                  +--+--+--+          *         +--+--+--+
                  |34 63 95|          *         |24 58 95|
                  +--+--+--+          *         +--+--+--+
                 /   |                *            |   \
               /     |                *            |     \
             /       |                *            |       \
           /         |                *            |         \
 +--+--+--+         +--+--+--+        *         +--+--+--+    +--+--+--+
 | 6 19 24|         |44 50 58|        or        |34 44 50|    |63 70 85|
 +--+--+--+         +--+--+--+        *         +--+--+--+    +--+--+--+
       |  |         |  |  |   \       *         |  |  |   \   |  |   \  \
       |  |         |  |  |    +----+ *         |  |  |   |   |  |    )  \
       |  |         |  |  |         | *         |  |  |   |   |  |    |   \
       |  |         |  |  |         | *         |  |  |   |   |  |    |    \
       |  |         |  |  |         | *         |  |  |   |   |  |    |     \
  +--+--+ |    +--+--+ | +--+--+--+ | * +--+--+--+ | +--+ | +--+ |  +--+--+  )
  |20 21| |    |39 41| | |52 55 57| | * |27 30 32| | |48| | |61| |  |75 80|  |
  +--+--+ |    +--+--+ | +--+--+--+ | * +--+--+--+ | +--+ | +--+ |  +--+--+  |
          |            |            | *            |      |      |           |
        +--+--+--+    +--+       +--+ *      +--+--+  +--+--+--+ +--+      +--+
        |27 30 32|    |48|       |61| *      |39 41|  |52 55 57| |67|      |88|
        +--+--+--+    +--+       +--+ *      +--+--+  +--+--+--+ +--+      +--+

(f) Deleting 96 causes underflow in the node from which it was deleted.
    Because there are no adjacent siblings that are not on the verge of
    underflowing, merging is performed, resulting in the leaf node
    containing 98 99.  But this leaves the node that had contained 98 in
    an underflowing condition, and so redistribution with its only sibling
    is performed.

                     +--+--+--+
                     |24 63 85|
                     +--+--+--+
                           |   \
                           |    \
                           |     \
                         +--+     +--+
                         |70|     |95|
                         +--+     +--+
                        /   |     |   \
                       /    |     |    \
                      /     |     |     \
                  +--+  +--+--+  +--+   +--+--+
                  |67|  |75 80|  |88|   |98 99|
                  +--+  +--+--+  +--+   +--+--+