- Asked 2/14
- 2071 Bhadra · 6 marks
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
| Step | Insert | Tree after step (root(left,right)) | Imbalance / rotation |
|---|---|---|---|
| 1 | jan | jan | balanced |
| 2 | feb | jan(feb,-) | balanced |
| 3 | mar | jan(feb,mar) | balanced |
| 4 | apr | jan(feb(apr,-),mar) | balanced |
| 5 | may | jan(feb(apr,-),mar(-,may)) | balanced |
| 6 | jun | jan(feb(apr,-),mar(jun,may)) | balanced |
| 7 | jul | jan(feb(apr,-),mar(jun(jul,-),may)) | balanced |
| 8 | aug | jan(aug(apr,feb),mar(jun(jul,-),may)) | at feb: LR: left at apr, right at feb |
| 9 | sep | jan(aug(apr,feb),mar(jun(jul,-),may(-,sep))) | balanced |
| 10 | oct | jan(aug(apr,feb),mar(jun(jul,-),oct(may,sep))) | at may: RL: right at sep, left at may |
| 11 | nov | mar(jan(aug(apr,feb),jun(jul,-)),oct(may(-,nov),sep)) | at jan: RR: left rotate at jan |
| 12 | dec | mar(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)
Related questions
- AVL tree for month names · Data Structure and Algorithm, chapter 6 · asked 2×
- AVL tree for 10 20 30 50 45 40 8 5 3 · Data Structure and Algorithm, chapter 6
- AVL tree for 10, 20, 30, 35, 50, ... · Data Structure and Algorithm, chapter 6
- AVL tree for 24, 12, 8, 15, 35, ... · Data Structure and Algorithm, chapter 6
- Heap tree insertion and heap sort · Data Structure and Algorithm, chapter 7
- Describe an AVL tree · Data Structure and Algorithm, chapter 6
- AVL tree for 3, 2, 1, 4, 5, 6, 7, ... · Data Structure and Algorithm, chapter 6
- Annual firm and secondary energy (900 m³/s, Nov 650) · Hydro Power, chapter 3 · asked 2×
- Annual firm and secondary energy (900 m³/s, Nov 100) · Hydro Power, chapter 3 · asked 2×
- Firm and secondary energy from average monthly flows · Hydro Power, chapter 3
- Peaking RoR: max power and wet/dry energy ratio · Hydropower Engineering, chapter 3