B-tree Data Structure | Baeldung on Computer Science (2024)

Last updated: March 18, 2024

B-tree Data Structure | Baeldung on Computer Science (1)

Written by: Subham Datta

B-tree Data Structure | Baeldung on Computer Science (2)

Reviewed by: Michal Aibin

  • Data Structures
  • Trees

    1. Overview

    In this tutorial, we’ll discuss a self-balancing tree data structure: B-tree. We’ll present the properties and various operations of the B-tree.

    2. Introduction

    B-tree is a tree data structure. In this tree structure, data is stored in the form of nodes and leaves. B-tree is known as a self-balanced sorted search tree. It’s a more complex and updated version of the binary search tree (BST) with additional tree properties.

    The main difference between a binary search tree and a B-tree is that a B-tree can have multiple children nodes for a parent node. However, a binary search tree can contain only two child nodes for a parent node.

    Unlike a binary search tree, a B-tree can have multiple keys in a node. The number of keys and the number of children for any node depends on the order of the B-tree. Keys are the values that are stored at any node. Let’s take a look at an example of B-tree:

    B-tree Data Structure | Baeldung on Computer Science (3)

    3. Properties

    Some properties of the B-tree are similar to BST. The parent node’s key is always greater than the key values of the left subtree nodes. Similarly, the parent node’s key is always less than the key values of the right subtree nodes.

    It can have multiple keys for a single node. A single node can have more than two children.

    All the leaf nodes must be at the same level. A leaf node is a node that doesn’t have any child nodes.

    Suppose the order of a B-tree is B-tree Data Structure | Baeldung on Computer Science (4). It means every node can have a maximum of B-tree Data Structure | Baeldung on Computer Science (5) children. Therefore, every node can have maximum B-tree Data Structure | Baeldung on Computer Science (6) keys and minimum B-tree Data Structure | Baeldung on Computer Science (7) keys except the root node.

    Let’s take a B-tree of order B-tree Data Structure | Baeldung on Computer Science (8). According to the properties of the B-tree, any node can have a maximum of B-tree Data Structure | Baeldung on Computer Science (9) keys. Hence B-tree Data Structure | Baeldung on Computer Science (10) keys in this case. Furthermore, any node can have minimum B-tree Data Structure | Baeldung on Computer Science (11) keys. Hence, B-tree Data Structure | Baeldung on Computer Science (12) keys.

    4. B-tree Operations

    Like any other tree data structure, three primary operations can be performed on a B-tree: searching, insertion, and deletion. Let’s discuss each operation one by one.

    4.1. Searching

    The structure of the B-tree is similar to the binary search tree, with some added properties. Hence, the searching operation in a B-tree works the same as a BST. Initially, we start searching from a root node.

    We either go to the left or right subtree from the root node, depending on the key value of the node we want to search. On top of this, in a B-tree, we have several decisions, as there can be more than two branches for a node.

    The searching time complexity of a B-tree in the best, as well as the worst-case, is B-tree Data Structure | Baeldung on Computer Science (13), where B-tree Data Structure | Baeldung on Computer Science (14) denotes the total number of elements in a B-tree.

    4.2. Insertion

    Insertion is the operation in which we insert or add elements in a B-tree. During this process, we need to keep some points in mind. We can’t insert any element at the root node of a B-tree. We have to start inserting elements from the leaf node.

    Additionally, while inserting a node in a B-tree, we must check the maximum number of keys at any node and add elements in ascending order.

    Let’s take the example of inserting some nodes in an empty B-tree. Here, we want to insert B-tree Data Structure | Baeldung on Computer Science (15) nodes in a empty B-tree: B-tree Data Structure | Baeldung on Computer Science (16). Let’s assume the order of the B-tree is B-tree Data Structure | Baeldung on Computer Science (17). Hence, the maximum number of children a node can contain is B-tree Data Structure | Baeldung on Computer Science (18). Additionally, the maximum and the minimum number of keys for a node, in this case, would be B-tree Data Structure | Baeldung on Computer Science (19) and B-tree Data Structure | Baeldung on Computer Science (20) (rounded off).

    While inserting any element, we must remember that we can only insert values in ascending order. Moreover, we always insert elements at the leaf node so that the B-tree always grows in the upward direction.

    Let’s start the insertion process with nodes B-tree Data Structure | Baeldung on Computer Science (21) and B-tree Data Structure | Baeldung on Computer Science (22):

    B-tree Data Structure | Baeldung on Computer Science (23)

    Next, we want to insert node B-tree Data Structure | Baeldung on Computer Science (24). But there’s no space for inserting new elements in that leaf node as the maximum key a node can contain is B-tree Data Structure | Baeldung on Computer Science (25) here. Therefore, we need to split the node. To split the node, we take the median key and move that key upward:

    B-tree Data Structure | Baeldung on Computer Science (26)

    Now we want to insert the next element B-tree Data Structure | Baeldung on Computer Science (27). As B-tree Data Structure | Baeldung on Computer Science (28) is greater than B-tree Data Structure | Baeldung on Computer Science (29), we first go to the right subtree. In the right subtree, there’s a node with key B-tree Data Structure | Baeldung on Computer Science (30). The key of the incoming node is greater than B-tree Data Structure | Baeldung on Computer Science (31). Hence, we’ll add this node to the right of B-tree Data Structure | Baeldung on Computer Science (32):

    B-tree Data Structure | Baeldung on Computer Science (33)

    Let’s insert the next node with key B-tree Data Structure | Baeldung on Computer Science (34). We first check the root, and as B-tree Data Structure | Baeldung on Computer Science (35) is greater than B-tree Data Structure | Baeldung on Computer Science (36), we go to the right subtree. In the right subtree, we see B-tree Data Structure | Baeldung on Computer Science (37) is greater than B-tree Data Structure | Baeldung on Computer Science (38) and B-tree Data Structure | Baeldung on Computer Science (39). Hence, we insert B-tree Data Structure | Baeldung on Computer Science (40) on the right side of B-tree Data Structure | Baeldung on Computer Science (41). But again, there’s not enough space. Therefore, we split it and move the median of B-tree Data Structure | Baeldung on Computer Science (42) that is B-tree Data Structure | Baeldung on Computer Science (43), to the upper node:

    B-tree Data Structure | Baeldung on Computer Science (44)

    Similarly, we insert our next node with key B-tree Data Structure | Baeldung on Computer Science (45):

    B-tree Data Structure | Baeldung on Computer Science (46)

    Finally, let’s add all the remaining nodes one by one, following the properties of the B-tree:

    B-tree Data Structure | Baeldung on Computer Science (47)

    The time complexity of the insertion process of a B-tree is B-tree Data Structure | Baeldung on Computer Science (48).

    4.3. Deletion

    Deletion is the process in which we remove keys from a B-tree. During this process, we need to maintain B-tree properties. After deleting any key, the tree must be arranged again to follow B-tree properties. Additionally, we need to search that key in the B-tree before deletion.

    There can be two possible situations while deleting a key from a B-tree. Firstly, the key we want to delete is in a leaf node. Secondly, the key we want to delete is in the internal node.

    Let’s take an example of deletion from the B-tree: Here, we’re taking a B-tree of order B-tree Data Structure | Baeldung on Computer Science (49). The maximum number of children a node can contain is B-tree Data Structure | Baeldung on Computer Science (50). Additionally, the maximum and the minimum number of keys for a node, in this case, would be B-tree Data Structure | Baeldung on Computer Science (51) and B-tree Data Structure | Baeldung on Computer Science (52):

    B-tree Data Structure | Baeldung on Computer Science (53)

    If the target key’s leaf node contains more than the minimum required keys, we don’t have to worry about anything. We need to delete the target key. After deletion, it’ll still be a complete B-tree.

    If the target key’s leaf node contains only the minimum number of keys, we can’t simply delete the key. Because if we do so, it’ll violate the condition of the B-tree. In such a case, borrow the key from the immediate left node if that node has more than the minimum required keys.

    We’ll transfer the maximum value of the left node to its parent node. After that, the key with greater value will be transferred to the borrower node. Moreover, we can remove the target key from the node. Let’s say we want to remove a node with key B-tree Data Structure | Baeldung on Computer Science (54):

    B-tree Data Structure | Baeldung on Computer Science (55)

    Another possible way is to borrow the key from the immediate right node if that node has more than the minimum required keys. We’ll transfer the minimum value of the right node to its parent node. The smaller key value will be transferred to the borrower node. After that, we can remove the target key from the node. Let’s say we want to remove a node with key B-tree Data Structure | Baeldung on Computer Science (56):

    B-tree Data Structure | Baeldung on Computer Science (57)

    Thus, if we remove the key B-tree Data Structure | Baeldung on Computer Science (58) from node 3, it’ll left with one key. However, the minimum number of keys one node should contain is B-tree Data Structure | Baeldung on Computer Science (59). Hence, as soon as we delete the key B-tree Data Structure | Baeldung on Computer Science (60), we need to add one key to node 3:

    B-tree Data Structure | Baeldung on Computer Science (61)

    Here, we perform three operations. First, we replace the highest key of the parent with the lowest key from node 4. Furthermore, we filled node 3 with the highest key of the parent node. Finally, the keys of node 3 are swapped. Let’s take a look at the B-tree structure after we delete the key B-tree Data Structure | Baeldung on Computer Science (62):

    B-tree Data Structure | Baeldung on Computer Science (63)

    Now, let’s discuss the scenario when neither the left sibling nor the right sibling of the target key’s node has more than the minimum required keys. In such a case, we need to merge two nodes. Out of two nodes, one node should contain our target node’s key. While merging, we also need to consider the parent nodes as well.

    Suppose we want to delete a node with a key of B-tree Data Structure | Baeldung on Computer Science (64):

    B-tree Data Structure | Baeldung on Computer Science (65)

    As we can see, its sibling nodes don’t contain more than the minimum number of keys. Hence, to delete the key B-tree Data Structure | Baeldung on Computer Science (66), the first step is to merge the nodes 1 and 2. Additionally, when merging the nodes, we need to remove the key B-tree Data Structure | Baeldung on Computer Science (67) from the parent node and add it to the merged node:

    B-tree Data Structure | Baeldung on Computer Science (68)

    After merging the nodes, we delete our target node:

    B-tree Data Structure | Baeldung on Computer Science (69)

    Now, let’s discuss the case when the target key is at an internal node:

    B-tree Data Structure | Baeldung on Computer Science (70)

    In such a case, the first possible option is to replace the target key with its in-order predecessor. Here, we take the left node of the target key, select the highest value key, and replace that with the target key. Here we want to delete node B-tree Data Structure | Baeldung on Computer Science (71):

    B-tree Data Structure | Baeldung on Computer Science (72)

    If the left doesn’t have more than the minimum required keys, we replace the target key with its in-order successor. Here, we’ll take the right node of the target key, select the lowest value key and replace that with the target key:

    B-tree Data Structure | Baeldung on Computer Science (73)

    If the target key’s inorder successor node and inorder predecessor node don’t have more than the minimum number of the required key, we need to merge two adjacent nodes. For example, deletion of B-tree Data Structure | Baeldung on Computer Science (74):

    B-tree Data Structure | Baeldung on Computer Science (75)

    After the deletion of 77:

    B-tree Data Structure | Baeldung on Computer Science (76)

    The time complexity of the deletion process of a B-tree is B-tree Data Structure | Baeldung on Computer Science (77).

    6. Conclusion

    B-Tree is a widely used data structure for storing a large amount of data. In this tutorial, we discussed the B-tree in detail. We presented the properties and operations with examples.

    B-tree Data Structure | Baeldung on Computer Science (2024)
    Top Articles
    Latest Posts
    Article information

    Author: Jeremiah Abshire

    Last Updated:

    Views: 5707

    Rating: 4.3 / 5 (74 voted)

    Reviews: 81% of readers found this page helpful

    Author information

    Name: Jeremiah Abshire

    Birthday: 1993-09-14

    Address: Apt. 425 92748 Jannie Centers, Port Nikitaville, VT 82110

    Phone: +8096210939894

    Job: Lead Healthcare Manager

    Hobby: Watching movies, Watching movies, Knapping, LARPing, Coffee roasting, Lacemaking, Gaming

    Introduction: My name is Jeremiah Abshire, I am a outstanding, kind, clever, hilarious, curious, hilarious, outstanding person who loves writing and wants to share my knowledge and understanding with you.