B Trees (M-way Trees) Data Structure (2024)

A B-Tree is a special type of M-way search tree.

Before learning about B-Trees we need to know what M-way trees are, and how B-tree is a special type of M-way tree. An M-way(multi-way) tree is a tree that has the following properties:

  • Each node in the tree can have at most m children.

  • Nodes in the tree have at most (m-1) key fields and pointers(references) to the children.

Consider the pictorial representation shown below of an M-way tree.

B Trees (M-way Trees) Data Structure (1)

The above image shows a 4-way tree, where each node can have at most 3(4-1) key fields and at most 4 children. It is also a 4-way search tree.

An M-way search tree is a more constrained m-way tree, and these constrain mainly apply to the key fields and the values in them. The constraints on an M-way tree that makes it an M-way search tree are:

  • Each node in the tree can associate with m children and m-1 key fields.

  • The keys in any node of the tree are arranged in a sorted order(ascending).

  • The keys in the first K children are less than the Kth key of this node.

  • The keys in the last (m-K) children are higher than the Kth key.

Consider the pictorial representation shown below of an M-way search tree:

B Trees (M-way Trees) Data Structure (2)

M-way search trees have the same advantage over the M-way trees, which is making the search and update operations much more efficient. Though, they can become unbalanced which in turn leaves us to the same issue of searching for a key in a skewed tree which is not much of an advantage.

Searching in an M-way Search Tree:

If we want to search for a value say X in an M-way search tree and currently we are at a node that contains key values from Y1, Y2, Y3,.....,Yk. Then in total 4 cases are possible to deal with this scenario, these are:

  • If X < Y1, then we need to recursively traverse the left subtree of Y1.

  • If X > Yk, then we need to recursively traverse the right subtree of Yk.

  • If X = Yi, for some i, then we are done, and can return.

  • Last and only remaining case is that when for some i we have Yi < X < Y(i+1), then in this case we need to recursively traverse the subtree that is present in between Yi and Y(i+1).

For example, consider the 3-way search tree that is shown above, say, we want to search for a node having key(X) equal to 60. Then, considering the above cases, for the root node, the second condition applies, and (60 > 40) and hence we move on level down to the right subtree of 40. Now, the last condition is valid only, hence we traverse the subtree which is in between the 55 and 70. And finally, while traversing down, we have our value that we were looking for.

A B tree is an extension of an M-way search tree. Besides having all the properties of an M-way search tree, it has some properties of its own, these mainly are:

  • All the leaf nodes in a B tree are at the same level.

  • All internal nodes must have M/2 children.

  • If the root node is a non leaf node, then it must have at least two children.

  • All nodes except the root node, must have at least [M/2]-1 keys and at most M-1 keys.

Consider the pictorial representation of a B tree shown below:

B Trees (M-way Trees) Data Structure (3)

Searching in a B Tree:

Searching for a key in a B Tree is exactly like searching in an M-way search tree, which we have seen just above. Consider the pictorial representation shown below of a B tree, say we want to search for a key 49 in the below shown B tree. We do it as following:

  • Compare item 49 with root node 75. Since 49 < 75 hence, move to its left sub-tree.

  • Since, 40<49<58, traverse right sub-tree of 40.

  • 49>44, move to right. Compare 49.

  • We have found 49, hence returning.

Consider the pictorial representation shown below:

B Trees (M-way Trees) Data Structure (4)

Inserting in a B Tree:

Inserting in a B tree is done at the leaf node level. We follow the given steps to make sure that after the insertion the B tree is valid, these are:

  • First, we traverse the B tree to find the appropriate node where the to be inserted key will fit.

  • If that node contains less than M-1 keys, then we insert the key in an increasing order.

  • If that node contains exactly M-1 keys, then we have two cases ? Insert the new element in increasing order, split the nodes into two nodes through the median, push the median element up to its parent node, and finally if the parent node also contains M-1 keys, then we need to repeat these steps.

Consider the pictorial representation shown below:

B Trees (M-way Trees) Data Structure (5)

Now, consider that we want to insert a key 9 into the above shown B tree, the tree after inserting the key 9 will look something like this:

B Trees (M-way Trees) Data Structure (6)

Since, a violation occurred, we need to push the median node to the parent node, and then split the node in two parts, hence the final look of B tree is:

B Trees (M-way Trees) Data Structure (7)

Deletion in a B Tree:

Deletion of a key in a B tree includes two cases, these are:

  • Deletion of key from a leaf node

  • Deletion of a key from an Internal node

Deletion of Key from a leaf node:

If we want to delete a key that is present in a leaf node of a B tree, then we have two cases possible, these are:

  • If the node that contains the key that we want to delete, in turn contains more than the minimum number of keys required for the valid B tree, then we can simply delete that key.

Consider the pictorial representation shown below:

B Trees (M-way Trees) Data Structure (8)

Say, we want to delete the key 64 and the node in which 64 is present, has more than minimum number of nodes required by the B tree, which is 2. So, we can simply delete this node.

The final tree after deletion of 64 will look like this:

B Trees (M-way Trees) Data Structure (9)

  • If the node that contains the key that we want to delete, in turn contains the minimum number of keys required for the valid B tree, then three cases are possible:

    • In order to delete this key from the B Tree, we can borrow a key from the immediate left node(left sibling). The process is that we move the highest value key from the left sibling to the parent, and then the highest value parent key to the node from which we just deleted our key.

    • In another case, we might have to borrow a key from the immediate right node(right sibling). The process is that we move the lowest value key from the right sibling to the parent node, and then the highest value parent key to the node from which we just deleted our key.

    • Last case would be that neither the left sibling or the right sibling are in a state to give the current node any value, so in this step we will do a merge with either one of them, and the merge will also include a key from the parent, and then we can delete that key from the node.

Case 1 pictorial representation:

B Trees (M-way Trees) Data Structure (10)

After we delete 23, we ask the left sibling, and then move 16 to the parent node and then push 20 downwards, and the resultant B tree is:

B Trees (M-way Trees) Data Structure (11)

Case 2 pictorial representation:

B Trees (M-way Trees) Data Structure (12)

After we delete 72, we ask the right sibling, and then move the 77 to the parent node and then push the 75 downwards, and the resultant B tree is:

B Trees (M-way Trees) Data Structure (13)

Case 3 pictorial representation:

B Trees (M-way Trees) Data Structure (14)

After deleting 65 from the leaf node, we will have the final B tree as:

B Trees (M-way Trees) Data Structure (15)

Deletion of Key from an Internal node:

  • If we want to delete a key that is present in an internal node, then we can either take the value which is in order predecessor of this key or if taking that inorder predecessor violates the B tree property we can take the inorder successor of the key.

  • In the inorder predecessor approach, we extract the highest value in the left children node of the node where our key is present.

  • In the inorder successor approach, we extract the lowest value in the right children node of the node where our key is present.

Pictorial Representation of the above cases:

  • Internal predecessor approach
    B Trees (M-way Trees) Data Structure (16)
    After deletion, our B tree:
    B Trees (M-way Trees) Data Structure (17)
  • Internal successor approach
    B Trees (M-way Trees) Data Structure (18)
    After deletion of 95, our tree will look like this:
    B Trees (M-way Trees) Data Structure (19)

Key Points:

  • The time complexity for search, insert and delete operations in a B tree is O(log n).

  • The minimum number of keys in a B tree should be [M/2] - 1.

  • The maximum number of keys in a B tree should be M-1.

  • All the leaf nodes in a B tree should be at the same level.

  • All the keys in a node in a binary tree are in increasing order.

  • B Trees are used in SQL to improve the efficiency of queries.

  • Each node in a B Tree can have at most M children.

Conclusion:

In this article, we learned what an M-way tree is, what are the differences between the M-way tree and M-way search tree. Also, we learned the constraints that are applied to an M-way tree to make it a B tree. Then we learned about the search, insert and delete operations.

B Trees (M-way Trees) Data Structure (2024)

FAQs

B Trees (M-way Trees) Data Structure? ›

B Tree is a specialized m-way tree that can be widely used for disk access. A B-Tree

A B-Tree
An (a,b)-tree has all of its leaves at the same depth, and all internal nodes except for the root have between a and b children, where a and b are integers such that 2 ≤ a ≤ (b+1)/2.
https://en.wikipedia.org › wiki › (a,b)-tree
of order m can have at most m-1 keys and m children. One of the main reason of using B tree is its capability to store large number of keys in a single node and large key values by keeping the height of the tree relatively small.

What is the M-way tree in data structures? ›

The m-way search trees are multi-way trees which are generalised versions of binary trees where each node contains multiple elements. In an m-Way tree of order m, each node contains a maximum of m – 1 elements and m children.

What is B-tree of order M in data structure? ›

A B-tree of order m is a search tree in which each nonleaf node has up to m children. The actual elements of the collection are stored in the leaves of the tree, and the nonleaf nodes contain only keys. Each leaf stores some number of elements; the maximum number may be greater or (typically) less than m.

What are B-trees in data structure? ›

In computer science, a B-tree is a self-balancing tree data structure that maintains sorted data and allows searches, sequential access, insertions, and deletions in logarithmic time. The B-tree generalizes the binary search tree, allowing for nodes with more than two children.

What is the B-tree file structure? ›

The BTREE file structure is a true variable-level recursive structure. The host automatically balances the BTREE structure as keys are added and deleted; therefore no periodic key rebuilds are necessary.

What is the difference between BST and M-way tree? ›

B tree is balanced where as m tree may be balanced or not,it could be skewed as well. B tree is balanced from bottom to top, this means all the leaf nodes will have m children/data node, except root node. Running time is guaranteed O(logN).

What is the difference between binary tree and M-way tree? ›

In summary, while binary trees and multiway trees are both types of data structures used to store and organise data, they differ in the number of children each node can have. This fundamental difference can impact their efficiency and suitability for different tasks in computer science.

What is B-tree and B-tree in data structure? ›

A B-tree is a sort of self-balancing search tree whereby each node could have more than two children and hold multiple keys. It's a broader version of the binary search tree. It is also usually called a height-balanced m-way tree.

What is M and L in B-tree? ›

First off, for typical B tree problems*, there are two types of nodes, your internal nodes and your leaf nodes. Thus it is standard to both specify a max number of internal nodes, M, and a max number of leaf nodes, L.

What is the difference between B-tree and binary tree? ›

B-tree is called a sorted tree as its nodes are sorted in inorder traversal. While binary tree is not a sorted tree. It can be sorted in inorder, preorder, or postorder traversal.

What is an example of a B-tree database? ›

In this example, the B-tree has a maximum node size of 3, meaning that each node can hold up to 3 keys and 4 children. The root node contains the keys [40,60] and has two children, which are the nodes [10,20,30] and [50,70,80,90] . The [10,20,30] node has three keys and twochildren, which are the nodes [5] , and [15] .

How do you create a B-tree in data structure? ›

Step 1 - Check whether tree is Empty. Step 2 - If tree is Empty, then create a new node with new key value and insert it into the tree as a root node. Step 3 - If tree is Not Empty, then find the suitable leaf node to which the new key value is added using Binary Search Tree logic.

Which database uses B-tree? ›

MySQL, a popular relational database management system, utilizes B-trees as its primary data structure for storing and retrieving data efficiently. Let's explore a practical example of how MySQL leverages B-trees in the context of data storage and retrieval.

Why are B trees used in database? ›

B-tree is used for indexing and is a data structure that provides sorted data and allows searches, sequential access, attachments and removals in sorted order.

Why do we use B-tree in data structure? ›

B tree is used to index the data and provides fast access to the actual data stored on the disks since, the access to value stored in a large database that is stored on a disk is a very time consuming process. Searching an un-indexed and unsorted database containing n key values needs O(n) running time in worst case.

What is the difference between B-tree and bitmap? ›

1: Syntax differences: The bitmap index includes the "bitmap" keyword. The btree index does not say "bitmap". 2: Cardinality differences: The bitmap index is generally for columns with lots of duplicate values (low cardinality), while b-tree indexes are best for high cardinality columns.

What does M stand for in the M-Way search tree? ›

The m-way search trees are self-balanced trees used to search or retrieve data efficiently. m represents the maximum number of children for a node in the tree.

What is M in binary tree? ›

An m-array tree can be described as a generalization of a binary tree in which each and every node has M or less children. In other words, a tree will be known as the m array tree if each node of the tree does not contain more than m children. A binary tree will be known as the M-ary tree if it contains M = 2.

What are the advantages of M-Way trees? ›

One of the advantages of using these multi-way trees is that they often require fewer internal nodes than binary search trees to store items. But, just as with binary search trees, multi-way trees require additional methods to make them efficient for all dictionary methods.

Top Articles
Latest Posts
Article information

Author: Wyatt Volkman LLD

Last Updated:

Views: 6204

Rating: 4.6 / 5 (46 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Wyatt Volkman LLD

Birthday: 1992-02-16

Address: Suite 851 78549 Lubowitz Well, Wardside, TX 98080-8615

Phone: +67618977178100

Job: Manufacturing Director

Hobby: Running, Mountaineering, Inline skating, Writing, Baton twirling, Computer programming, Stone skipping

Introduction: My name is Wyatt Volkman LLD, I am a handsome, rich, comfortable, lively, zealous, graceful, gifted person who loves writing and wants to share my knowledge and understanding with you.