# B Tree Visualization

In the following tutorial, we will learn about the B Tree data structure and consider visualizing it.

So, let's get started.

## What is a B Tree?

The B Tree is a special type of multiway search tree, commonly known as the M-way tree, which balances itself. Because of their balanced structure, these trees are commonly utilized to operate and manage immense databases and simplify searches. In a B Tree, each node can have at most n child nodes. B Tree is an example of Multilevel Indexing in a Database Management System (DBMS). Leaf and Internal nodes will both have record references. B Tree is known as Balanced Stored Tree because all the leaf nodes are at the same level.

The following diagram is an example of a B Tree:

Figure 1. A B Tree with order 3

## Understanding the Rules of the B Tree

The following are the important properties of a B Tree:

1. All the leaf nodes are at the same level.
2. The B Tree data structure is defined by the term minimum degree 'd'. The value of 'd' depends on the size of the disk block.
3. Every node, excluding the root, must consist of at least d-1 keys. The root node may consist of a minimum of 1 key.
4. All nodes (including the root node) may consist of at most (2d-1) keys.
5. The number of children of a node is equal to the addition of the number of keys present in it and .
6. All keys of a node are sorted in ascending order. The child between two keys, k1 and k2, consists of all the keys ranging between k1 and k2, respectively.
7. Unlike the Binary Search Tree, the B Tree data structure grows and shrinks from the root. Whereas the Binary Search Tree grows downwards and shrinks downward.
8. Similar to other Self-Balanced Binary Search Trees, the Time complexity of the B Tree data structure for the operations like searching, insertion, and deletion is O(log?n).
9. The Insertion of a Node in the B Tree happens only at the Leaf Node.

Let us consider the following example of a B Tree of minimum order 5.

Figure 2. A B Tree of order 5

#### Note: The value of the minimum order is much more than 5 in a practical B Trees.

In the above diagram, we can observe that all the leaf nodes are at the same level, and all non-leaf nodes have no empty subtree and consist of keys one less than the number of their children.

The set formulation of the B Tree rules:

Every B Tree depends upon a positive constant integer known as MINIMUM, which is utilized in order to determine the number of data elements that can be held in a single node.

Rule 1: The root can have as few as only one data element (or even no data elements if it is also no children); every other node has at least MINIMUM data elements.

Rule 2: The maximum number of data elements stored in a node is twice the value of MINIMUM.

Rule 3: The data elements of each node of the B Tree are stored in a partially filled array, sorted from the smallest data element (at index 0) to the largest data element (at the final utilized position of the array).

Rule 4: The total number of subtrees below a non-leaf node is always one more than the number of data elements in that node.

• subtree 0,subtree 1,...

Rule 5: With respect to any non-leaf node:

1. A data element at index is greater than all the data elements in subtree number i of the node, and
2. A data element at index is less than all the data elements in subtree number i+1 of the node.

Rule 6: Every leaf in a B Tree has the same depth. Thus, it ensures that a B Tree prevents the problem of an unbalanced tree.

## Operations on a B Tree data structure

In order to ensure that none of the properties of a B Tree data structure are violated during the operations, the B Tree may be split or joined. The following are some operations that we can perform on a B Tree:

1. Searching a data element in B Tree
2. Insertion of a data element in B Tree
3. Deletion of a data element in B Tree

### Searching Operation on a B Tree

Searching an element in a B Tree is similar to that in a Binary Search Tree. But instead of making a two-way decision (Left or Right) like a Binary Search Tree, a B Tree makes an m-way decision at each node where m is the number of children of the node.

Steps to search a data element in a B Tree:

Step 1: The search begins from the root node. Compare the search element, k, with the root.

Step 1.1: If the root node consists of the element k, the search will be complete.

Step 1.2: If the element k is less than the first value in the root, we will move to the leftmost child and search the child recursively.

Step 1.3.1: If the root has only two children, we will move to the rightmost child and recursively search the child nodes.

Step 1.3.2: If the root has more than two keys, we will search the rest.

Step 2: If the element k is not found after traversing the whole tree, then the search element is not present in the B Tree.

Let us visualize the above steps with the help of an example. Suppose that we wanted to search for a key k=34 in the following B Tree:

Figure 3.1. A given B Tree

1. Firstly, we will check whether the key k = 34 is at the root node. Since the key is not at the root, we will move on to its child nodes. We can also observe that the root node has two children; therefore, we will compare the required value between the two keys.

Figure 3.2. The key k is not present at root
2. Now that we can notice that the key k is greater than the root node, i.e., 30, we will proceed with the right child of the root.

Figure 3.3. The key k > 30, move to right child
3. We will now compare the key k with the values present on the node, i.e., 40 and 50, respectively. Since the key k is less than the left key, i.e., 40, we will move to the left child of the node.

Figure 3.4. The key k < 40, move to left child
4. Since the value in the left child of 40 is 34, which is the required value, we can conclude that the key is found, and the search operation is completed.

Figure 3.4. The key k = 34, the left child of 40

We compared the key with four different values in the above example until we found it. Thus, the time complexity required for the search operation in a B Tree is O(log?n).

### Inserting Operation on a B Tree

While inserting a data element into a B Tree, we must first check whether that element is already present in the tree or not. If the data element is found within the B Tree, then the inserting operation does not occur. However, if that's not the case, we will proceed further with the insertion. There are two scenarios required to be taken care of while insertion an element in the leaf node:

1. Node does not consist of more than the MAXIMUM number of keys - We will insert the key in the node at its proper place.
2. A node consists of a MAXIMUM number of keys - We will insert the key to the full node, and then a split operation will take place, splitting the full node into three parts. The second or median key will move to the parent node, and the first and the third parts will act as the left and right child nodes, respectively. This process will be repeated with the parent node if it also consists of a MAXIMUM number of keys.

Steps to insert a data element in a B Tree:

Step 1: We will start by calculating the maximum number of keys in the node on the basis of the order of the B Tree.

Step 2: If the tree is empty, a root node is allocated, and we will insert the key that acts as the root node.

Step 3: We will now search the applicable node for insertion.

Step 4: If the node is full:

Step 4.1: We will insert the data elements in ascending order.

Step 4.2: If the data elements are greater than the maximum number of keys, we will split the full node at the median.

Step 4.3: We will push the median key upwards and split the left and right keys as left and right child.

Step 5: If the node is not full, we will insert the node in ascending order.

Let us visualize the above steps with the help of an example. Suppose that we are required to create a B Tree of order 4. The data elements needed to be inserted into the B Tree are 5,3,21,11,1,16,8,13,4,and 9.

1. Since m is equal to 3, the maximum number of keys for a node = m-1 = 3-1 = 2.
2. We will start by inserting 5 in the empty tree.

Figure 4.1. Inserting 5
3. We will now insert 3 into the tree. Since 3 is less than 5, we will insert 3 to the left of 5 in the same node.

Figure 4.2. Inserting 3
4. We will now insert 21 into the tree, and since 21 is greater than 5, it will be inserted to the right of 5 in the same node. However, as we know that the maximum number of keys in the node is 2, one of these keys will be moved to a node above in order to split it. Thus, 5, the middle data element, will move up, and 3 and 21 will become its left and right nodes, respectively.

Figure 4.3. Inserting 21
5. Now it is time to insert the next data element, i.e., 11, which is greater than 5 but less than 21. Therefore, 11 will be inserted as a key on the left of the node consisting of 21 as a key.

Figure 4.4. Inserting 11
6. Similarly, we will insert the next data element 1 into the tree, and as we can observe, 1 is less than 3; therefore, it will be inserted as a key on the left of the node consisting of 3 as a key.

Figure 4.5. Inserting 1
7. Now, we will insert data element 16 into the tree, which is greater than 11 but less than 21. However, the number of keys in that node exceeds the maximum number of keys. Therefore, the node will split, moving the middle key to the root. Hence, 16 will be inserted to the right of the 5, splitting 11 and 21 into two separate nodes.

Figure 4.6. Inserting 16
8. The data element 8 will be inserted as a key to the left of 11.

Figure 4.7. Inserting 8
9. We will insert 13 into the tree, which is less than 16 and greater than 11. Therefore, data element 13 should be inserted to the right of the node consisting of 8 and 11. However, since the maximum number of keys in the tree can only be 2, a split will be occurred, moving the middle data element 11 to the above node and 8 and 13 into two separate nodes. Now, the above node will consist of 5, 11, and 16, which again exceeds the maximum key count. Therefore, there will be another split, making the data element 11 the root node with 5 and 16 as its children.

Figure 4.8. Inserting 13
10. Since the data element 4 is less than 5 but greater than 3, it will be inserted to the right of the node consisting of 1 and 3, which will exceed the maximum count of keys in a node again. Therefore, a spill will occur again, moving the 3 to the upper node beside 5.

Figure 4.9. Inserting 4
11. At last, the data element 9, which is greater than 8 but less than 11, will be inserted to the right of the node consisting of 8 as a key.

Figure 4.10. Inserting 9

In the above example, we have performed different comparisons. The first value was directly inserted into the tree; after that, every value had to be compared with the nodes available in that tree. The time complexity for the Inserting Operation in a B Tree depends on the number of nodes and .

### Deleting Operation on a B Tree

Deleting a data element on a B Tree contains three primary events:

1. Searching the node where the key to be deleted exists,
2. Deleting the key, and
• Balancing the tree, if necessary.

While deleting an element from the tree, a condition known as Underflow may occur. Underflow occurs when a node consists of less than the minimum number of keys; it should hold.

The following are some terms required to be understood before visualizing the deletion/removal operation:

1. In-order Predecessor: The most significant key on the left child of a node is known as its in-order predecessor.
2. In-order Successor: The minor key on the right child of a node is known as its in-order successor.

The following are three prominent cases of the deletion operation in a B Tree:

Case 1: The Deletion/Removal of the key lies in the Leaf node - This case is further divided into two different cases:

1. The deletion/removal of the key does not violate the property of the minimum number of keys a node should hold.

Let us visualize this case using an example where we will delete key 32 from the following B Tree.

Figure 4.1: Deleting a leaf node key (32) from the B Tree

As we can observe, deleting 32 from this tree does not violate the above property.

2. The deletion/removal of the key violates the property of the minimum number of keys a node should hold. In this case, we must borrow a key from its proximate sibling node in the order of Left to Right.

Firstly, we will visit the proximate Left sibling. If the Left sibling node has more than a minimum number of keys, it will borrow a key from this node.

Else, we will check to borrow from the proximate Right sibling node.

Let us now visualize this case with the help of an example where we will delete 31 from the above B Tree. We will borrow a key from the left sibling node in this case.

Figure 4.2: Deleting a leaf node key (31) from the B Tree

If both the proximate sibling nodes already consist of a minimum number of keys, then we will merge the node with either the left sibling node or the right one. This process of merging is done through the parent node.

Let us visualize again by deleting the key 30 from the above B Tree.

Figure 4.3: Deleting a leaf node key (30) from the B Tree

Case 2: The Deleting/Removal of the key lies in the non-Leaf node - This case is further divided into different cases:

1. The non-Leaf/Internal node, which is removed, is replaced by an in-order predecessor if the Left child node has more than the minimum number of keys.

Let us visualize this case using an example where we will delete the key 33 from the B Tree.

Figure 4.4: Deleting a leaf node key (33) from the B Tree

2. The non-Leaf/Internal node, which is removed, is replaced by an in-order successor if the Right child node has more than the minimum number of keys.

If either child has a minimum number of keys, then we will merge the Left and the Right child nodes.

Let us visualize this case by deleting the key 30 from the B Tree.

Figure 4.5: Deleting a leaf node key (30) from the B Tree

After merging, if the parent node has less than the minimum number of keys, one can look for the siblings, as in Case 1.

Case 3: In the following case, the tree's height shrinks. If the target key lies in an Internal node, and the removal of the key leads to fewer keys in the node (which is less than the minimum necessitated), then look for the in-order predecessor and the in-order successor. If both the children have a minimum number of keys, then borrowing can't occur. This leads to Case 2(3), i.e., merging the child nodes.

We will again look for the sibling to borrow a key. However, if the sibling also consists of a minimum number of keys, then we will merge the node with the sibling along with the parent node and arrange the child nodes as per the requirements (ascending order).

Let us visualize this case with the help of an example where we will delete the data element 10 from the given B Tree.

Figure 4.6: Deleting a leaf node key (10) from the B Tree

The time complexity in the above examples vary dependent on the location from where the key needs to be deleted. Thus, the time complexity for the Deleting Operation in a B Tree is O(log?n).

## The Conclusion

In this tutorial, we have learned about the B Tree and visualised its different operations with different examples. We have also understood some fundamental properties and rules of the B Tree.