B-Tree data structure is a self-balancing tree data structure that is commonly used in databases and file systems. It is designed to store large amounts of data on disk and perform efficient insertions, deletions, and lookups.

How B-Trees work

B-Trees are binary search trees that can have more than two children per node. They are also balanced, which means that all the leaves are at the same depth, and the difference between the number of keys in the left and right subtrees of any node is at most one.

Each node in a B-Tree can contain multiple keys and multiple child pointers. The number of keys and child pointers that a node can have is determined by the order of the B-Tree. The order of the B-Tree is the maximum number of keys that a node can have, which is usually denoted by m.

The root node of a B-Tree can have between one and m child pointers, and all the other nodes in the tree can have between ⌈m/2⌉ and m child pointers.

Insertion and Deletion in B-Trees

Insertion and deletion in B-Trees follow a similar process. When a key is inserted or deleted from a B-Tree, the tree is traversed from the root to the leaf node where the key should be inserted or deleted.

If the leaf node is full when a key is being inserted, the node is split into two nodes, and the middle key is moved up to the parent node. If the parent node is also full, it is split, and the process continues up to the root node.

When a key is deleted from a leaf node, and the node has less than ⌈m/2⌉ keys, the key is merged with one of its siblings, or one of its siblings is borrowed a key to balance the node.

Python Implementation of B-Tree

Here is a simple implementation of a B-Tree in Python that supports insertion, deletion, and lookup operations:

class BTreeNode:
    def __init__(self, leaf=False):
        self.keys = []
        self.child_pointers = []
        self.leaf = leaf

    def add_key(self, key):

    def remove_key(self, key):

    def add_child(self, child):

    def remove_child(self, child):

class BTree:
    def __init__(self, order):
        self.root = BTreeNode(leaf=True)
        self.order = order

    def insert(self, key):
        node = self.root
        if len(node.keys) == self.order - 1:
            new_node = BTreeNode()
            self.root = new_node
            self.split_child(new_node, 0)
            self.insert_non_full(new_node, key)
            self.insert_non_full(node, key)

    def insert_non_full(self, node, key):
        i = len(node.keys) - 1
        if node.leaf:
            while i >= 0 and key < node.keys[i]:
                i -= 1
            i += 1
            if len(node.child_pointers[i].keys) == self.order - 1:
                self.split_child(node, i)
                if key > node.keys[i]:
                    i += 1
            self.insert_non_full(node.child_pointers[i], key)

    def split_child(self, parent, i):
        node = parent.child_pointers[i]

    new_node = BTreeNode(leaf=node.leaf)
    mid = self.order // 2
    new_node.keys = node.keys[mid+1:]
    node.keys = node.keys[:mid]
    if not node.leaf:
        new_node.child_pointers = node.child_pointers[mid+1:]
        node.child_pointers = node.child_pointers[:mid+1]

def delete(self, key):
    node, index = self.search(key)
    if not node:
    if node.leaf:
        predecessor = node.child_pointers[index-1]
        if len(predecessor.keys) >= self.order:
            pred_key = self.get_max_key(predecessor)
            node.keys[index-1] = pred_key
        elif len(node.child_pointers[index+1].keys) >= self.order:
            successor = node.child_pointers[index+1]
            succ_key = self.get_min_key(successor)
            node.keys[index] = succ_key
            self.merge_children(node, index)

def merge_children(self, node, index):
    child1 = node.child_pointers[index]
    child2 = node.child_pointers[index+1]
    key = node.keys[index]

def search(self, key):
    node = self.root
    while node:
        i = 0
        while i < len(node.keys) and key > node.keys[i]:
            i += 1
        if i < len(node.keys) and key == node.keys[i]:
            return node, i
        elif node.leaf:
            return None, None
            node = node.child_pointers[i]
    return None, None

def get_min_key(self, node):
    if node.leaf:
        return node.keys[0]
        return self.get_min_key(node.child_pointers[0])

def get_max_key(self, node):
    if node.leaf:
        return node.keys[-1]
        return self.get_max_key(node.child_pointers[-1])

Example Usage of B-Tree

Here’s an example of how to use the B-Tree implementation we created above:

btree = BTree(3)
print(btree.search(6)) # prints: (<__main__.BTreeNode object at 0x7fcf123c7a00>, 2)
print(btree.search(6)) # prints: (None, None)

In this example, we create a B-Tree with an order of 3 and insert the keys 5, 3, 7, 2, 4, 6, and 8 into the tree. We then search for the key 6 and print the result, which shows that the key is found at index 2 in the leaf node. We then delete the key 6 from the tree and search for it again, which shows that the key is no longer in the tree.


B-Trees are a powerful data structure that can efficiently store large amounts of data on disk or in-memory. They are commonly used in databases and file systems to index and organize large amounts of data. B-Trees are balanced trees that maintain a balance between height and the number of keys per node, ensuring efficient search, insertion, and deletion operations.

In this article, we discussed the structure and properties of B-Trees and provided an implementation of a B-Tree in Python. We explained the process of inserting and deleting keys from a B-Tree and provided an example of how to use the implementation we created.

We hope this article has provided you with a clear understanding of B-Trees and their use cases. If you have any questions or comments, please feel free to reach out in the comments section below.


  1. What is the difference between a B-Tree and a Binary Tree?
  1. How is the order of a B-Tree determined?
  1. What is the worst-case time complexity of search, insertion, and deletion in a B-Tree?
  1. Can a B-Tree be used for in-memory data structures?
  1. What are some common use cases for B-Trees?

Leave a Reply

Your email address will not be published. Required fields are marked *