Skip to main content

B-tree insertion algorithm; AVL of months

Short answer

B-tree insertion: search to leaf, insert key; if node overflows (m keys), split by moving median to parent, splitting node into two. Repeat up to root if needed. AVL tree for months: root mar, left jan (left aug with apr and feb with left dec, right jun with left jul), right oct (left may with right nov, right sep).

Write an algorithm for the insertion of B-Tree. Create an AVL tree for the following data: jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec

Algorithm for insertion in a B-tree

A B-tree of order m has at most m children (m − 1 keys) per node, every internal node other than the root has at least ⌈m/2⌉ children (the root has at least 2 unless it is a leaf), and all leaves are on the same level. Insertion always happens in a leaf; the tree grows upward by splitting.

BTreeInsert(root, key)        /* order m: max m-1 keys, min ceil(m/2)-1 keys */
1. If the tree is empty, create a root node with the key; stop.
2. Search from the root down to the leaf where key belongs
   (at each node follow the child between the two keys that surround key).
   If key is found already, stop (no duplicates).
3. Insert key into that leaf in sorted order.
4. While the current node has m keys (overflow):
      a. Find the median key (middle of the m keys).
      b. Split the node into two nodes: keys before the median stay in
         the left node, keys after the median go to a new right node.
      c. Move the median up into the parent, between the pointers
         to the two halves.
      d. If the node was the root, create a new root holding only the
         median (the tree grows one level taller); stop.
      e. Otherwise make the parent the current node and repeat.
5. Stop.

The cost is O(log n) node accesses because the height is O(log n).

AVL tree for the month names

Months are compared alphabetically (apr < aug < dec < feb < jan < jul < jun < mar < may < nov < oct < sep). Insert in the given order and rebalance when a balance factor becomes ±2.

Step-by-step insertion

StepInsertTree after step (root(left,right))Imbalance / rotation
1janjanbalanced
2febjan(feb,-)balanced
3marjan(feb,mar)balanced
4aprjan(feb(apr,-),mar)balanced
5mayjan(feb(apr,-),mar(-,may))balanced
6junjan(feb(apr,-),mar(jun,may))balanced
7juljan(feb(apr,-),mar(jun(jul,-),may))balanced
8augjan(aug(apr,feb),mar(jun(jul,-),may))at feb: LR: left at apr, right at feb
9sepjan(aug(apr,feb),mar(jun(jul,-),may(-,sep)))balanced
10octjan(aug(apr,feb),mar(jun(jul,-),oct(may,sep)))at may: RL: right at sep, left at may
11novmar(jan(aug(apr,feb),jun(jul,-)),oct(may(-,nov),sep))at jan: RR: left rotate at jan
12decmar(jan(aug(apr,feb(dec,-)),jun(jul,-)),oct(may(-,nov),sep))balanced

Final AVL tree

mar
├─L:jan
│  ├─L:aug
│  │  ├─L:apr
│  │  └─R:feb
│  │     └─L:dec
│  └─R:jun
│     └─L:jul
└─R:oct
   ├─L:may
   │  └─R:nov
   └─R:sep

Height = 5, and every node has balance factor −1, 0 or +1.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Data Structure and Algorithm chapter 6 questions →