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:
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.
Rule 5: With respect to any non-leaf 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:
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
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:
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.
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:
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:
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).
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.
Next TopicProperties of AVL Trees