stacks

B-Trees
By Samira Lahmar
(edited by Nadia Al-Ghreimil)
1
B-Trees
 A B-tree is a special type of tree that has properties that
make it useful for storing and retrieving information.
 Like a binary search tree, finding an element stored in B-tree
requires searching only a single path between the root and a
leaf.
 Like an AVL tree, the insertion and deletion algorithms
guarantee that the longest path between the root and a leaf is
O(log2n). The insertion and deletion algorithms are however,
quite different from those used for an AVL tree.
 Unlike a binary tree, each node may contain many elements
and may have many children. Because of this and because it
is well balanced (like an AVL tree), a B-tree provides the
possibility of very short path lengths for accessing very large
collections of elements.
Samira Lahmar
2
B-Tree Example
Samira Lahmar
3
Insertion illustrated
 Each node in a a B-tree contains between m/2 and m-1
elements where m is the order of the tree.
 The root node is the only exception to this rule, and it may
contain between 1 and m-1 elements.
 The number of children of a B-tree node either is 0 or one
greater than the number of elements in that node.
 Keep in mind that we use a small order 5 so as to make
illustrations feasible.
 In practice, B-trees of order 100 or more are common.
 We observe that the elements in the node appear in sorted
order. This is always true of a B-tree node and is part of the
reason that finding an element only requires searching a single
path from the root to a leaf node.
Samira Lahmar
4
Algorithmic Description of insertion
 The first step is always to find the leaf node where the element
would be if it were in the tree.
 After the correct leaf node is found, a sequence of insertion
processes begins.
 Each process adds one element to a leaf of the tree and, if a
node split is required, passes an element to the level above.
(we call these processes insert1, insert2 and insert3).
 An insertion in a B-tree always begins by adding an element to
a leaf node.
 The height of tree increases only if the nodes along the path
are full.
 Since the height of tree is increased by adding a new root, the
tree remains well balanced
Samira Lahmar
5
Insertion Algorithm
 Insert1: The node contains fewer than m-1 elements and so
has room to accommodate an additional element. The element
is added so as to maintain sorted order, and the insertion
process terminates.
 Insert2: The node is full ( it contains m-1 elements) so a node
split is required. A new node is created that receives the largest
m/2 elements from the node that is being split. The smallest
m/2 elements remain in their original node, and the middle
element is passed to the parent of the split node. The insertion
process continues.
 Insert3: The root node is split, a new root node is created,
and the element passed up becomes its only element. The
insertion process terminates.
Samira Lahmar
6
Insertion Example (I)
Samira Lahmar
7
Insertion Example (II)
Samira Lahmar
8
Insertion Example (III)
Samira Lahmar
9
Deletion Algorithm
 If the element to be deleted is not in a leaf node, it is replaced
by the smallest element in the leftmost node in its right subtree
(or the largest element in the rightmost node in its left
subtree). This replacement requires removing an element from
a leaf node, so we are back to starting with a leaf node.
 As with insertion, there are three cases to consider. The
process begins with a leaf node, but these processes apply to a
node at any level.
 We refer to the node from which the element is being removed
as the target node.
Samira Lahmar
10
Deletion Algorithm
Delete1: The target node contains more than m/2 elements. The target
element is removed and the process terminates.
Delete2: The target node contains exactly m/2 elements, so removing
one causes an underflow.
•
•
•
If either the right or left sibling of the target node contains more than m/2
elements, then elements are borrowed to prevent the underflow. This
causes the element in the parent that separates these two children to
change. This is usually done so as to balance the number of elements in
the two children. The process then terminates.
If both the right and left siblings contain exactly m/2 elements, then the
target node is combined with one of them to form a new node containing
m-1 elements one of these is the element from the parent node that
separated the two nodes that are combined.
The parent node becomes the target node and the process continues.
Samira Lahmar
11
Deletion Algorithm
Delete3: The target node is the root node. As long as at least one
element remains, the height of the tree does not change. If the
last element was removed, then the one remaining child
becomes the root of the tree.
Samira Lahmar
12
Deletion Example (I)
Samira Lahmar
13
Deletion Example (II)
Samira Lahmar
14