Open In App

Practice questions on B and B+ Trees

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:

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:

Type 3. Based on searching a key in B/B+ tree –
These are the key points related to searching in B/B+ trees:

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.

Article Tags :