MULTI-WAY SEARCH TREES
We have discussed that every node in a binary search tree contains one value and two pointers, left and
right, which point to the node’s left and right sub-trees, respectively. The same concept is used in an M-
way search tree which has M – 1 values per node and M sub trees. In such a tree, M is called the
degree of the tree. Note that in a binary search tree M = 2, so it has one value and two sub-trees. In
other words, every internal node of an M-way search tree consists of pointers to M sub-trees and
contains M – 1 keys, where M > 2.
M-way search tree of order 3
B TREES
B tree is a specialized M-way tree that is widely used for disk access. B tree of order m can
have a maximum of m– 1 keys and m pointers to its sub-trees. B tree may contain a large
number of key values and pointers to sub- trees. Storing a large number of keys in a single
node keeps the height of the tree relatively small.
B tree is designed to store sorted data and allows search, insertion, and deletion operations to
be performed in logarithmic amortized time. B tree of order m (the maximum number of
children that each node can have) is a tree with all the properties of an M-way search tree.
In addition, it has the following properties:
1. Every node in the B tree has at most (maximum) m children.
2. Every node in the B tree except the root node and leaf nodes has at least (minimum) m/2 children.
3. The root node has at least two children if it is not a terminal (leaf) node.
4. All leaf nodes are at the same level.
1. Searching for an Element in a B Tree
Searching for an element in a B tree is similar to that in binary search trees. Consider the B tree
Given in Fig. To search for 59
Step 1. we begin at the root node. The root node has a value 45 which is less than 59.
Step 2. So, we traverse in the right sub-tree. The right sub-tree of the root node has two key values,
49 and 63. Since 49 < 59 < 63,
Step 3. We traverse the right sub-tree of 49, that is, the left sub-tree of 63. This sub-tree has three
values, 54, 59, and 61. On finding the value 59, the search is successful.
The running time of the search operation depends upon the height of the tree, the algorithm to search
for an element in a B tree takes O(logt n) time to execute.
2. Inserting a New Element in a B Tree
In a B tree, all insertions are done at the leaf node level. A new value is inserted in the B tree using
the algorithm given below.
Procedure:
Step 1. Search the B tree to find the leaf node where the new key value should be inserted.
Step 2. If the leaf node is not full, that is, it contains less than m–1 key values, then insert
the new element in the node keeping the node’s elements ordered.
Step 3. If the leaf node is full, that is, the leaf node already contains m–1 key values, then
(a) insert the new value in order into the existing set of keys,
(b) split the node at its median into two nodes (note that the split nodes are half full), and
(c) push the median element up to its parent’s node. If the parent’s node is already full,
then split the parent node by following the same steps
Example 1 Look at the B tree of order 5 given below and insert 8, 9, 39, and 4 into it.
Till now, we have easily inserted 8and 9 in the tree because the leaf nodes were not full. But now, the
node in which 39 should be inserted is already full as it contains four values. Here we split the nodes
to form two separate nodes. But before splitting, arrange the key values in order (including the new
value). The ordered set of values isgivenas21, 27, 36,39, and42. The median value is 36, so push 36
into its parent’s node and split the leaf nodes
Now the node in which 4 should be inserted is already full as it contains four key values. Here we split
the nodes to form two separate nodes. But before splitting, we arrange the key values in order (including
the new value). The ordered set of values is given as 4, 7, 8, 9, and 11. The median value is 8, so we
push 8 into its parent’s node and split the leaf nodes. But again, we see that the parent’s node is already
full, so we split the parent node using the same procedure.
3. Deleting an Element from a B Tree
Like insertion, deletion is also done from the leaf nodes. There are two cases of deletion. In the first
case, a leaf node has to be deleted. In the second case, an internal node has to be deleted. Let us first see
the steps involved in deleting a leaf node.
Step 1. Locate the leaf node which has to be deleted.
Step 2. If the leaf node contains more than the minimum number of key values (more than m/2
elements), then delete the value.
Step 3. Else if the leaf node does not contain m/2 elements, then fill the node by taking an
element either from the left or from the right sibling.
(a) If the left sibling has more than the minimum number of key values, push its largest
key into its parent’s node and pull down the intervening element from the parent
node to the leaf node where the key is deleted.
(b) Else, if the right sibling has more than the minimum number of key values, push its
smallest key into its parent node and pull down the intervening element from the
parent node to the leaf node where the key is deleted.
Step 4. Else, if both left and right siblings contain only the minimum number of elements, then
create a new leaf node by combining the two leaf nodes and the intervening element of the
parent
Example 1 Consider the following B-tree of order 5 and delete values 93, 201, 180, and 72 from it
Example 2 Consider the B tree of order 3 given below and perform the following operations:
(a) insert 121, 87 and then (b) delete 36, 109.
Example 11.4 Create a B tree of order 5 by inserting the following elements: 3, 14, 7, 1, 8, 5, 11, 17, 13, 6,
23, 12, 20,
26, 4, 16, 18, 24, 25, and 19
B+ TREES
A B+ tree is a variant of a B tree which stores sorted data in a way that allows for efficient
insertion, retrieval, and removal of records, each of which is identified by a key. While a B tree
can store both keys and records in its interior nodes, a B+ tree, in contrast, stores all the records at
the leaf level of the tree; only keys are stored in the interior nodes.
The leaf nodes of a B+ tree are often linked to one another in a linked list. This has an added
advantage of making the queries simpler and more efficient.
Typically, B+ trees are used to store large amounts of data that cannot be stored in the main
memory. With B+ trees, the secondary storage (magnetic disk) is used to store the leaf nodes of
trees
and the
internal
nodes of
trees are
stored in
the main memory.
B+ trees store data only in the leaf nodes. All other nodes (internal nodes) are called index nodes
or i-nodes and store index values. This allows us to traverse the tree from
the root down to the leaf node that stores the desired data item. Figure shows a B+ tree of order
3. Many database systems are implemented using B+ tree structure because of its simplicity.
Since all the data appear in the leaf nodes and are ordered, the tree is always balanced and makes
searching for data efficient.
A B+ tree can be thought of as a multi-level index in which the leaves make up a dense index and the
non-leaf nodes make up a sparse index.
The advantages of B+ trees can be given as follows:
1. Records can be fetched in equal number of disk accesses
2. It can be used to perform a wide range of queries easily as leaves are linked to nodes at the upper
level
3. Height of the tree is less and balanced
4. Supports both random and sequential access to records
5. Keys are used for indexing
1. Inserting a New Element in a B+ Tree
A new element is simply added in the leaf node if there is space for it. But if the data node in the
tree where insertion has to be done is full, then that node is split into two nodes. This calls for
adding a new index value in the parent index node so that future queries can arbitrate between
the two new nodes.
However, adding the new index value in the parent node may cause it, in turn, to split.
In fact, all the nodes on the path from a leaf to the root may split when a new value is added to a
leaf node. If the root node splits, a new leaf node is created and the tree grows by one level.
The steps to insert a new node in a B+ Tree are summarized
below Step 1: Insert the new node as the leaf node.
Step 2: If the leaf node overflows, split the node and copy the middle element to next
index node. Step 3: If the index node overflows, split that node and move the middle
element to next index page
Example 1. Consider the B+ tree of order 4 given and insert 33 in it.
2 Deleting an Element from a B+ Tree
As in B trees, deletion is always done from a leaf node. If deleting a data element leaves that node
empty, then the neighboring nodes are examined and merged with the under full node.
This process calls for the deletion of an index value from the parent index node which, in turn,
may cause it to become empty. Similar to the insertion process, deletion may cause a merge-
delete wave to run from a leaf node all the way up to the root. This leads to shrinking of the tree
by one level.
The steps to delete a node from a B+ tree are
summarized below Step 1: Delete the key and data
from the leaves.
Step 2: If the leaf node underflows, merge that node with the sibling and delete the key in between them.
Step 3: If the index node underflows, merge that node with the sibling and move down the key in between
them.
Example 1: Consider the B+ tree of order 4 given below and delete node 15 from it.