Practice questions on B and B+ Trees
Last Updated : 04 May, 2019
In this article, we will discuss different types of problems based on B and B+ trees. Before understanding this article, you should understand basics of B and B+ trees (see:
Introduction,
Insert,
Delete). These are the types of questions asked in GATE based on B and B+ trees.
Type 1. Based on order and number of keys in B and B+ tree - These are the few key points related to order and number of keys:
- A B/B+ tree with order p has maximum p pointers and hence maximum p children.
- A B/B+ tree with order p has minimum ceil(p/2) pointers and hence minimum ceil(p/2) children.
- A B/B+ tree with order p has maximum (p - 1) and minimum ceil(p/2) - 1 keys.
Que - 1. Consider a B+-tree in which the maximum number of keys in a node is 5. What is the minimum number of keys in any non-root node? (GATE CS 2010) (A) 1 (B) 2 (C) 3 (D) 4
Solution: Assuming order of B+ tree as p, maximum number of keys will be (p - 1). As it is given that, p - 1 = 5 => p = 6 Therefore, minimum number of keys: ceil(p/2) - 1 = 2
Type 2. Based on inserting a key in B/B+ tree - Given the order of B/B+ tree and keys to be inserted, it can be asked to find resultant B/B+ tree or height of B/B+ tree.
Que - 2. Consider the following 2-3-4 tree (i.e., B-tree with a minimum degree of two) in which each data item is a letter. The usual alphabetical ordering of letters is used in constructing the tree.

What is the result of inserting G in the above tree? (A)

(B)

(C)

(D) None
Solution: Since the given B tree has minimum degree as 2, the maximum degree or order will be 2*2 = 4. Therefore, it will have at most 4 pointers or 3 keys. We will traverse from root till leaf node where G is to be inserted. As G is less than L, it will be inserted in leaf node with elements BHI. After insertion of G, the leaf node in sorted order will be BGHI which leads to overflow. It will be split into two parts BG and I and middle element H will be sent to its parent node as:

Now root node with keys H, L, P, U is overflowed which leads to splitting of root node into two parts HL and U and middle element P will be root node which matches option B. Note:
- There occur 2 splits for insertion of G.
- The height of B tree is 1 (path from root node to leaf node) before insertion of G. After insertion of G, the height of B tree reaches 2.
Type 3. Based on searching a key in B/B+ tree - These are the key points related to searching in B/B+ trees:
- For searching a key in B tree, we start from root node and traverse until the key is found or leaf node is reached.
- For searching a key in B+ tree, we start from root node and traverse until leaf node is reached as every key is present in leaf nodes. Also, leaf nodes are connected to each other which help in faster access of data for range queries.
Que - 3. With reference to the B+ tree index of order 1 shown below, the minimum number of nodes (including the root node) that must be fetched in order to satisfy the following query: “Get all records with a search key greater than or equal to 7 and less than 15” is ____. (GATE-CS-2015)

(A) 4 (B) 5 (C) 6 (D) 7
Solution: First we will search for key equal to 7. For finding 7, we will start from root node and move to node with key 5 and then move to leaf node with keys 5 and 7. So for searching 7, we need to access 3 nodes. Once 7 is searched, we can go to next leaf node containing keys 9 and 11. From this, we can go to leaf node with keys 13 and 15. As we want keys less than 15, we can stop here. Therefore total nodes accessed = 3(for searching 7) + 2(for finding keys less than 15) = 5.

Note: If we use B tree, then we need to search 7, 8, 9, 10, 11 individually due of which node access will be higher. Therefore, B+ tee is preferred for range queries.
Type 4. Count of node splitting in B/B+ tree - Que - 4. A B-tree of order 4 is built from scratch by 10 successive insertions. What is the maximum number of node splitting operations that may take place? (GATE CS 2008) (A) 3 (B) 4 (C) 5 (D) 6
Solution: A B tree having order 4 can have maximum 3 keys.

First 3 insertions will not have any split as shown in Figure (a). On inserting 4th element, there will be 1 split as shown in Figure (b). On inserting 5th element, there will be no split but we will insert in that leaf node having maximum element to produce more splits in further insertions as shown in Figure (c). On inserting 6th element, there will be 1 split as shown in Figure (d). On inserting 7th element, there will be no split but we will insert in that leaf node having maximum element to produce more splits in further insertions as shown in Figure (e). On inserting 8th element, there will be 1 split as shown in Figure (f). On inserting 9th element, there will be no split but we will insert in that leaf node having maximum element to produce more splits in further insertions as shown in Figure (g). On inserting 10th element, there will be 2 split as shown in Figure (h). Total number of splits = 5.
Similar Reads
Introduction of B-Tree A B-Tree is a specialized m-way tree designed to optimize data access, especially on disk-based storage systems. In a B-Tree of order m, each node can have up to m children and m-1 keys, allowing it to efficiently manage large datasets.The value of m is decided based on disk block and key sizes.One
8 min read
Insert Operation in B-Tree In this post, we'll discuss the insert() operation in a B-Tree. A new key is always inserted into a leaf node. To insert a key k, we start from the root and traverse down the tree until we reach the appropriate leaf node. Once there, the key is added to the leaf.Unlike Binary Search Trees (BSTs), no
15+ min read
What is B-Tree? | B-Tree meaning A B-tree is a self-balancing tree where all the leaf nodes are at the same level which allows for efficient searching, insertion and deletion of records. Because of all the leaf nodes being on the same level, the access time of data is fixed regardless of the size of the data set. Characteristics of
3 min read
Need for B-Trees in Databases and File Systems A B-Tree is a self-balancing tree data structure widely used in database and file systems for its efficiency in storing and retrieving massive quantities of data. B-Trees are great for handling enormous datasets that are too big to store fully in memory since they are optimized to operate with stora
5 min read
B-Tree Insert without aggressive splitting B-Tree Insert without aggressive splittingThis algorithm for insertion takes an entry, finds the leaf node where it belongs, and inserts it there. We recursively insert the entry by calling the insert algorithm on the appropriate child node. This procedure results in going down to the leaf node wher
15+ min read
Delete Operation in B-Tree A B Tree is a type of data structure commonly known as a Balanced Tree that stores multiple data items very easily. B Trees are one of the most useful data structures that provide ordered access to the data in the database. In this article, we will see the delete operation in the B-Tree. B-Trees are
15+ min read
Introduction of B+ Tree B + Tree is a variation of the B-tree data structure. In a B + tree, data pointers are stored only at the leaf nodes of the tree. In this tree, structure of a leaf node differs from the structure of internal nodes. The leaf nodes have an entry for every value of the search field, along with a data p
8 min read
Insertion in a B+ tree Prerequisite: Introduction of B+ treesIn this article, we will discuss that how to insert a node in B+ Tree. During insertion following properties of B+ Tree must be followed:Â Each node except root can have a maximum of M children and at least ceil(M/2) children.Each node can contain a maximum of M
15+ min read
What is B+ Tree | B+ Tree meaning The B+ tree is similar to the B-tree data structure in that it is a tree structure with a fixed number of keys per node, and it is balanced so that all leaf nodes are at the same level. However, in a B+ tree, all keys are stored in the leaf nodes, while the internal nodes only contain pointers to ot
5 min read
Difference between B tree and B+ tree B-Tree: B-Tree is known as a self-balancing tree as its nodes are sorted in the inorder traversal. In B-tree, a node can have more than two children. B-tree has a height of logM N (Where âMâ is the order of tree and N is the number of nodes). And the height is adjusted automatically at each update.
3 min read