00:00

B Tree in Data Structure

Last updated:

Binary search trees, AVL trees and red-black trees store one key per node and give each node at most two children. A B-tree drops that limit: a node can hold many keys and have many children. The result is a very short, wide tree, which is exactly what is needed when every node visit means reading a block from disk.

Definition: a B-tree is a self-balancing search tree in which every node holds several sorted keys and has one more child than it has keys, and all leaves are at the same depth.

How many keys and children a node may have is set by the order m of the B-tree: a node has at most m children (and so at most m − 1 keys), and every internal node other than the root has at least ⌈m/2⌉ children. (CLRS describes the same limits with the minimum degree t, where m = 2t.)

B-trees were invented by Rudolf Bayer and Edward M. McCreight at Boeing Scientific Research Labs (published in 1972) to manage index pages for large random-access files.

Key Characteristics of B-Tree Data Structure

  • Perfectly height-balanced: all leaves are at the same depth, so every search visits the same number of levels.

  • Many children per node: a node with k keys has k + 1 children; the keys separate the key ranges of the children, just as one key separates the two subtrees of a BST node.

  • Many keys per node: because a node can hold dozens or hundreds of keys, the tree's height is only O(logm n). A B-tree of order 100 with three levels can already index about a million keys.

  • Built for block storage: a node is usually sized to fill one disk page, so each level costs one disk read. That makes B-trees the standard index structure in databases and file systems.

  • Grows at the root: insertion splits a full node and pushes its middle key up; deletion borrows from or merges with a sibling. The tree only becomes taller when the root splits, which keeps all leaves at the same depth.

Operations on B-Tree Data Structure

A B-tree supports the usual search-tree operations, all in O(log n) time, while keeping the tree balanced:

  • Searching: in each node, find the first key that is greater than or equal to the target (linear or binary search inside the node); either it is the target, or the search continues in the child to its left.
  • Insertion: insert the key into the right leaf; if the leaf overflows, split it and move the middle key up to the parent (see FAQ Q4).
  • Deletion: remove the key; if a node underflows, borrow a key from a sibling or merge with it (see FAQ Q5).
  • Traversing: an inorder walk (child, key, child, key, …) visits all keys in sorted order.

Application of B-Tree Data Structure

B-trees, and especially their B+ tree variant, are used wherever large amounts of ordered data live on block storage:

  • Database indexes (most relational databases use B+ trees).
  • File systems, for directories and file metadata (for example NTFS, HFS+, Btrfs and XFS use B-tree variants).
  • Key-value storage engines and embedded databases.
  • Multi-level indexes for large files.

Advantages of B-Tree Data Structure

  • Very few disk reads: the high branching factor keeps the tree only a few levels deep.
  • Guaranteed O(log n) search, insertion and deletion; the tree never degenerates.
  • Keys stay sorted, so ordered traversal and range scans are supported.
  • Scales to very large data sets that do not fit in memory.
  • Good use of each disk block, because nodes are kept at least about half full.

Disadvantages of B-Tree Data Structure

  • Complex to implement, especially deletion with borrowing and merging.
  • Overhead for small, in-memory data: a balanced binary tree or a hash table is simpler and usually faster there.
  • Space inside nodes can be unused, since nodes may be only half full.
  • Range scans are slower than in a B+ tree, because keys and data are spread over internal nodes and leaves are not linked together.