1. Order of B-Tree (M)
the maximum number of children, or 1 + maximum number of keys stored in
nodes
2. Minimum keys for interior nodes (t)
t = (M - 1) / 2 (e.g. t = 1 for M = 3, t = 2 for M = 5)
intuition: nodes in B-Tree have to be half full
3. Root has at least two children if it is not a leaf node
4. Leaves appear at same level
5. None-leaf nodes with K children have K - 1 keys
(1) Insert into b-tree
start with a single node
add keys until the node fills, i.e. contains M - 1 keys
in adding the Mth key, split the node in two
pull one key up to the parent node
(2) Delete from b-tree (the example shows a M=5 t=2 B-Tree)
case - 1: If x is a leaf node and x has > t keys, then just delete the key
from node x
DG
/ | \
ABC EF HI
Delte key 'B'.
DG
/ | \
AC EF HI
case - 2: If x is a leaf node and x has == t keys, then we cannot delete the
key directly
case - 2(a): If x has a sibling with > t keys, then we can adjust the B-Tree
by "borrowing" a key from this sibling.
JM
/ | \
GH KL OPR
Delete Key 'L': borrow key from right sibling, i.e. put 'O' to the
parent and put 'M' to the current node. Similar operation as we borrow
from the left sibling.
JO
/ | \
GH KM PR
case - 2(b): If none of x's sibling has > t keys, merge x with one of its
sibling by bringing down the parent key on the median key. Then delete the
target key.
QT
/ | \
OP RS WX
Delete Key 'S':
Q
/ \
OP RTWX
case - 3: If x is an internal node
case - 3(a): If x has a child with > t keys, then we can "borrow" a key
from this child
QU
/ | \
OP RST WX
Delete Key 'U': borrow key from 'U''s left child. Use the largest key
'T' to replace 'U'. Similar Operation as we borrow from the right
child.
QT
/ | \
OP RS WX
case - 3(b): If none of the children have > t keys, then the children must
be merged into one and the key could be removed.
RUX
/ | | \
PQ ST VW YZ
Delete Key 'U': merge node 'ST' and 'VW', then delete 'U'
RX
/ | \
PQ STVW YZ