Balanced Trees
Balanced Trees
A B-tree is a tree data structure that keeps data sorted and allows searches, insertions, and deletions in
logarithmic amortized time. Unlike self-balancing binary search trees, it is optimized for systems that read
and write large blocks of data. It is most commonly used in database and file systems.
The B-Tree Rules
Important properties of a B-tree:
The set formulation of the B-tree rules: Every B-tree depends on a positive constant integer called MINIMUM,
which is used to determine how many elements are held in a single node.
Rule 1: The root can have as few as one element (or even no elements if it also has no children);
every other node has at least MINIMUM elements.
Rule 2: The maximum number of elements in a node is twice the value of MINIMUM.
Rule 3: The elements of each B-tree node are stored in a partially filled array, sorted from the
smallest element (at index 0) to the largest element (at the final used position of the array).
Rule 4: The number of subtrees below a nonleaf node is always one more than the number of
elements in the node.
o Subtree 0, subtree 1, ...
Rule 5: For any nonleaf node:
1. An element at index i is greater than all the elements in subtree number i of the node, and
2. An element at index i is less than all the elements in subtree number i + 1 of the node.
Rule 6: Every leaf in a B-tree has the same depth. Thus it ensures that a B-tree avoids the problem of
a unbalanced tree.
A binary search tree has one value in each node and two subtrees.
This notion easily generalizes to an M-way search tree, which has
(M-1) values per node and M subtrees. M is called the degree of
the tree. A binary search tree, therefore, has degree 2.
The values in a node are stored in ascending order, V1 < V2 < ...
Vk (k <= M-1) and the subtrees are placed between adjacent
values, with one additional subtree at each end. We can thus
associate with each value a `left' and `right' subtree, with the right
subtree of Vi being the same as the left subtree of V(i+1). All the
values in V1's left subtree are less than V1 ; all the values in Vk's
subtree are greater than Vk; and all the values in the subtree
between V(i) and V(i+1) are greater than V(i) and less than
V(i+1).
And here is a 5-way B-tree (each node other than the root must
contain between 2 and 4 values):