Introduction of B Tree (original) (raw)

Last Updated : 5 Jan, 2026

A B-Tree is a specialized m-way tree designed to optimize data access, especially on disk-based storage systems.

Following is an example of a B-Tree of order 5 .

Properties of a B-Tree

B Tree of order m can be defined as an m-way search tree which satisfies the following properties:

  1. All leaf nodes of a B tree are at the same level, i.e. they have the same depth (height of the tree).
  2. The keys of each node of a B tree (in case of multiple keys), should be stored in the ascending order.
  3. In a B tree, all non-leaf nodes (except root node) should have at least m/2 children.
  4. All nodes (except root node) should have at least m/2 - 1 keys.
  5. If the root node is a leaf node (only node in the tree), then it will have no children and will have at least one key. If the root node is a non-leaf node, then it will have at least 2 children and at least one key.
  6. A non-leaf node with n-1 key values should have n non NULL children.

We can see in the above diagram that all the leaf nodes are at the same level and all non-leaf nodes have no empty sub-tree and have number of keys one less than the number of their children.

Interesting Facts about B-Tree

1. Height when the B-tree is **completely full (i.e., all nodes have the maximum m children):

h_{\text{min}} = \left\lceil \log_{m}(n + 1) \right\rceil - 1

2. Height when the B-tree is **least filled (each node has the minimum t children):

h_{\text{max}} = \left\lfloor \log_{t} \left( \frac{n + 1}{2} \right) \right\rfloor

Need of a B-Tree

The B-Tree data structure is essential for several reasons:

Operations on B-Tree

B-Trees support various operations that make them highly efficient for managing large datasets. Below are the key operations:

Sr. No. Operation Time Complexity
1. Search O(log n)
2. Insert O(log n)
3. Delete O(log n)
4. Traverse O(n)

**Note: "n" is the total number of elements in the B-tree

**Search Operation in B-Tree

Search is similar to the search in Binary Search Tree. Let the key to be searched is k.

Start from the root and recursively traverse down.
For every visited non-leaf node

If we reach a leaf node and don't find k in the leaf node, then return NULL.

Searching a B-Tree is similar to searching a binary tree. The algorithm is similar and goes with recursion. At each level, the search is optimized as if the key value is not present in the range of the parent then the key is present in another branch. As these values limit the search they are also known as limiting values or separation values. If we reach a leaf node and don’t find the desired key then it will display NULL.

**Input: Search 120 in the given B-Tree.

The key 120 is located in the leaf node containing 110 and 120. The search process is complete.

**Algorithm for Searching an Element in a B-Tree

C++ `

struct Node { int n; int key[MAX_KEYS]; Node* child[MAX_CHILDREN]; bool leaf; };

Node* BtreeSearch(Node* x, int k) { int i = 0; while (i < x->n && k > x->key[i]) { i++; } if (i < x->n && k == x->key[i]) { return x; } if (x->leaf) { return nullptr; } return BtreeSearch(x->child[i], k); }

C

BtreeSearch(x, k) i = 1

// n[x] means number of keys in x node
while i ? n[x] and k ? keyi[x]
    do i = i + 1
if i  n[x] and k = keyi[x]
    then return (x, i)   
if leaf [x]
    then return NIL
else
    return BtreeSearch(ci[x], k)

Java

class Node { int n; int[] key = new int[MAX_KEYS]; Node[] child = new Node[MAX_CHILDREN]; boolean leaf; }

Node BtreeSearch(Node x, int k) { int i = 0; while (i < x.n && k > x.key[i]) { i++; } if (i < x.n && k == x.key[i]) { return x; } if (x.leaf) { return null; } return BtreeSearch(x.child[i], k); }

Python

class Node: def init(self): self.n = 0 self.key = [0] * MAX_KEYS self.child = [None] * MAX_CHILDREN self.leaf = True

def BtreeSearch(x, k): i = 0 while i < x.n and k > x.key[i]: i += 1 if i < x.n and k == x.key[i]: return x if x.leaf: return None return BtreeSearch(x.child[i], k)

C#

class Node { public int n; public int[] key = new int[MAX_KEYS]; public Node[] child = new Node[MAX_CHILDREN]; public bool leaf; }

Node BtreeSearch(Node x, int k) { int i = 0; while (i < x.n && k > x.key[i]) { i++; } if (i < x.n && k == x.key[i]) { return x; } if (x.leaf) { return null; } return BtreeSearch(x.child[i], k); }

JavaScript

// Define a Node class with properties n, key, child, and leaf class Node { constructor() { this.n = 0; this.key = new Array(MAX_KEYS); this.child = new Array(MAX_CHILDREN); this.leaf = false; } }

// Define a function BtreeSearch that takes in a Node object x and an integer k function BtreeSearch(x, k) { let i = 0; while (i < x.n && k > x.key[i]) { i++; } if (i < x.n && k == x.key[i]) { return x; } if (x.leaf) { return null; } return BtreeSearch(x.child[i], k); }

`

Applications of B-Trees

Advantages of B-Trees

Disadvantages of B-Trees