Properties of B-Tree

  • All leaves are at the same level.
  • B-Tree is defined by the term minimum degree ‘t‘. The value of ‘t‘ depends upon disk block size.
  • Every node except the root must contain at least t-1 keys. The root may contain a minimum of 1 key.
  • All nodes (including root) may contain at most (2*t – 1) keys.
  • Number of children of a node is equal to the number of keys in it plus 1.
  • All keys of a node are sorted in increasing order. The child between two keys k1 and k2 contains all keys in the range from k1 and k2.
  • B-Tree grows and shrinks from the root which is unlike Binary Search Tree. Binary Search Trees grow downward and also shrink from downward.
  • Like other balanced Binary Search Trees, the time complexity to search, insert, and delete is O(log n).
  • Insertion of a Node in B-Tree happens only at Leaf Node.


Following is an example of a B-Tree of minimum order 5 
Note: that in practical B-Trees, the value of the minimum order is much more than 5. 
 


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

Introduction of B-Tree

The limitations of traditional binary search trees can be frustrating. Meet the B-Tree, the multi-talented data structure that can handle massive amounts of data with ease. When it comes to storing and searching large amounts of data, traditional binary search trees can become impractical due to their poor performance and high memory usage. B-Trees, also known as B-Tree or Balanced Tree, are a type of self-balancing tree that was specifically designed to overcome these limitations.

Unlike traditional binary search trees, B-Trees are characterized by the large number of keys that they can store in a single node, which is why they are also known as “large key” trees. Each node in a B-Tree can contain multiple keys, which allows the tree to have a larger branching factor and thus a shallower height. This shallow height leads to less disk I/O, which results in faster search and insertion operations. B-Trees are particularly well suited for storage systems that have slow, bulky data access such as hard drives, flash memory, and CD-ROMs.

B-Trees maintains balance by ensuring that each node has a minimum number of keys, so the tree is always balanced. This balance guarantees that the time complexity for operations such as insertion, deletion, and searching is always O(log n), regardless of the initial shape of the tree.

Similar Reads

Time Complexity of B-Tree:

Sr. No.AlgorithmTime Complexity1.SearchO(log n)2.InsertO(log n)3.DeleteO(log n)...

Properties of B-Tree:

All leaves are at the same level.B-Tree is defined by the term minimum degree ‘t‘. The value of ‘t‘ depends upon disk block size.Every node except the root must contain at least t-1 keys. The root may contain a minimum of 1 key.All nodes (including root) may contain at most (2*t – 1) keys.Number of children of a node is equal to the number of keys in it plus 1.All keys of a node are sorted in increasing order. The child between two keys k1 and k2 contains all keys in the range from k1 and k2.B-Tree grows and shrinks from the root which is unlike Binary Search Tree. Binary Search Trees grow downward and also shrink from downward.Like other balanced Binary Search Trees, the time complexity to search, insert, and delete is O(log n).Insertion of a Node in B-Tree happens only at Leaf Node....

Interesting Facts about B-Trees:

The minimum height of the B-Tree that can exist with n number of nodes and m is the maximum number of children of a node can have is:  The maximum height of the B-Tree that can exist with n number of nodes and t is the minimum number of children that a non-root node can have is:   and...

Traversal in B-Tree:

Traversal is also similar to Inorder traversal of Binary Tree. We start from the leftmost child, recursively print the leftmost child, then repeat the same process for the remaining children and keys. In the end, recursively print the rightmost child....

Search Operation in B-Tree:

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

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);} Python3 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 leafclass 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 kfunction 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:

...