B-Trees

B-trees are balanced search trees designed to work well on magnetic disks or direct-access secondary storage devices. B-trees differ significantly from red-black trees in that B-tree nodes may have many children, from a handful to thousands.
images/BTree.png
Figure 1: B-Tree where t=2 and the sequence 9,0,8,1,7,2,6,3,5,4 inserted.
It often takes more time to access a page of information and read it from a disk than it takes for the computer to examine all the information read. For this reason, we look at the two principal components of the running time:
We model these disk operations in our pseudo-code as follows. Let x be a pointer to an object. DISK-READ( x) to read object x into main memory; DISK-WRITE( x) to write object x to the disk.

1  Definition

A B-tree is a rooted tree having the following properties.
  1. Every node x has the following fields:
  2. If x is an internal node, it also contains n[x]+1 pointers c1 [x], c2 [x],, cn[x]+1 [x] to its children. Leaf nodes have no children, so their ci fields are undefined.
  3. The keys keyi [x] separate the ranges of keys stored in each subtree: if ki is any key stored in the subtree with root ci [x], then

    k1 key1 [x] k2 key2 [x] keyn[x] [x] kn[x]+1 .

  4. Every leaf has the same depth, which is the tree's height h.
  5. There are lower and upper bounds on the number of keys a node can contain. These bounds can be expressed in terms of a fixed integer t2 called the minimum degree of the B-tree:
THEOREM If n1, then for any n-key B-tree T of height h and minimum degree t2, then
h logt n+1 2 .

2  Operations

The operations for B-trees include B-TREE-SEARCH, B-TREE-CREATE, B-TREE-INSERT, B-TREE-DELETE, etc. We assume that:

2.1  Search

B-TREE-SEARCH (x,k)
 1     i   1
 2    while  in[x] and k> keyi [x]
 3                  do  i   i+1
 4    if  in[x] and k= keyi [x]
 5          then return  (x,i)
 6    if  leaf[x]
 7          then return  NIL
 8          else   DISK-READ ( ci [x])
 9                      return  B-TREE-SEARCH ( ci [x],k)

2.2  Creation

B-TREE-CREATE (T)
 1     x   ALLOCATE-NODE ()
 2     leaf[x]   TRUE
 3     n[x]   0
 4     DISK-WRITE (x)
 5     root[T]   x

2.3  Insertion

A fundamental operation used during insertion is the splitting of a full node y (having 2t-1 keys) around its median key keyt [y] into two nodes having t-1 keys each. The median key moves up into y's parent, which must be nonfull prior to the splitting of y.
images/BTreeSplitChildArguments.png
Figure 2: The arguments for the B-TREE-SPLIT-CHILD algorithm.
images/BTreeSplitChildResult.png
Figure 3: The result of child splitting.
B-TREE-SPLIT-CHILD (x,i,y)
 1     z   ALLOCATE-NODE ()
 2     leaf[z]   leaf[y]
 3     n[z]   t-1
 4    for  j   1 to  t-1
 5             do  keyj [z]   keyj+t [y]
 6    if not leaf[y]
 7          then for  j   1 to  t
 8                               do  cj [z]   cj+t [y]
 9     n[y]   t-1
 10    for  j   n[x]+1 downto  i+1
 11             do  cj+1 [x]   cj [x]
 12     ci+1 [x]   z
 13    for  j   n[x] downto  i
 14             do  keyj+1 [x]   keyj [x]
 15     keyi [x]   keyt [y]
 16     n[x]   n[x]+1
 17     DISK-WRITE (y)
 18     DISK-WRITE (z)
 19     DISK-WRITE (x)
Inserting a key k into a B-tree T of height h is done in a single pass down the tree, requiring O(h) disk accesses. The CPU time required is O(th)=O(t logt n).
B-TREE-INSERT (T,k)
 1     r   root[T]
 2    if  n[r]=2t-1
 3          then  s   ALLOCATE-NODE ()
 4                       root[T]   s
 5                       leaf[s]   FALSE
 6                       n[s]   0
 7                       c1 [s]   r
 8                       B-TREE-SPLIT-CHILD (s,1,r)
 9                       B-TREE-INSERT-NONFULL (s,k)
 10          else   B-TREE-INSERT-NONFULL (r,k)
Figure 4: See Fig 18.6 (page 446) - create a new node for root if root is 2t-1
B-TREE-INSERT-NONFULL (x,k)
 1     i   n[x]
 2    if  leaf[x]
 3          then while  i1 and k< keyi [x]
 4                                    do  keyi+1 [x]   keyi [x]
 5                                           i   i-1
 6                       keyi+1 [x]   k
 7                       n[x]   n[x]+1
 8                       DISK-WRITE (x)
 9          else  while  i1 and k< keyi [x]
 10                                    do  i   i-1
 11                       i   i+1
 12                       DISK-READ ( ci [x])
 13                      if  n[ ci [x]]=2t-1
 14                            then  B-TREE-SPLIT-CHILD (x,i, ci [x])
 15                                        if  k> keyi [x]
 16                                              then  i   i+1
 17                       B-TREE-INSERT-NONFULL ( ci [x],k)
images/BTreeInsert.png
Figure 5: Inserting the sequence 9,0,8,1,7,2,6,3,5,4 into a B-Tree.

2.4  Deletion

  1. If the key k is in node x and x is a leaf, delete the key k from x.
  2. If the key k is in node x and x is an internal node , do the following:
  3. If the key k is not present in internal node x, determine the root ci [x] of the appropriate subtree that must contain k, if k is in the tree at all. If ci [x] has only t-1 keys, execute step 3a or 3b as necessary to guarantee that we descend to a node containing at least t keys. Then, finish by recursing on the appropriate child of x.
    1. If ci [x] has only t-1 keys but has a sibling with t keys, give ci [x] an extra key by moving a key from x down into ci [x], moving a key from ci [x]'s immediate left or right sibling up into x, and moving the appropriate child from the sibling into ci [x].
    2. If ci [x] and both of ci [x]'s immediate siblings have t-1 keys, merge ci with one sibling., which involves moving a key from x down into the new merged node to become the median key from that node.



File translated from TEX by TTM, version 3.67.
On 31 Mar 2006, 18:12.