next up previous
Next: Up: Previous:

Insert: B-Tree-Insert(T, k)

B-Tree-Insert(T, k)
$\;\;\;\;\;$r = root(T)
$\;\;\;\;\;$if n(r) = 2t-1 $\;\;\;\;\;$ $\;\;\;\;\;$ $\;\;\;\;\;$ $\;\;\;\;\;$; full
$\;\;\;\;\;$then allocate empty node s pointing to r
$\;\;\;\;\;$ $\;\;\;\;\;$B-Tree-Split-Child(s, 1, r)
$\;\;\;\;\;$ $\;\;\;\;\;$B-Tree-Insert-Nonfull(s, k)
$\;\;\;\;\;$else B-Tree-Insert-Nonfull(r, k)



B-Tree-Insert-Nonfull(x, k)
$\;\;\;\;\;$if leaf(x)
$\;\;\;\;\;$then shift keys of x higher than k one to the right
$\;\;\;\;\;$ $\;\;\;\;\;$put k in appropriate spot
$\;\;\;\;\;$ $\;\;\;\;\;$n(x) = n(x) + 1
$\;\;\;\;\;$ $\;\;\;\;\;$DiskWrite(x)
$\;\;\;\;\;$else find smallest i such that k < keyi(x)
$\;\;\;\;\;$ $\;\;\;\;\;$DiskRead(childi(x))
$\;\;\;\;\;$ $\;\;\;\;\;$if n(childi(x)) = 2t - 1 ; full
$\;\;\;\;\;$ $\;\;\;\;\;$then B-Tree-Split-Child(x, i, childi(x))
$\;\;\;\;\;$ $\;\;\;\;\;$ $\;\;\;\;\;$if k > keyi(x)
$\;\;\;\;\;$ $\;\;\;\;\;$ $\;\;\;\;\;$then i = i + 1 $\;\;\;\;\;$; adjust due to new node entry from child
$\;\;\;\;\;$ $\;\;\;\;\;$B-Tree-Insert-Nonfull(childi(x), k)



Disk Accesses: O(h)
Run Time: O(th) = O(t $\log_t n$) = O(lg n), if t constant


next up previous
Next: Up: Previous: