Advanced Data Structures
Advanced Data Structures
B Tree
B Tree is a specialized m-way tree that can be widely used for disk access. 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.
A B tree of order m contains all the properties of an M way tree. In addition, it contains the following
properties.
It is not necessary that, all the nodes contain the same number of children but, each node must have
m/2 number of nodes.
While performing some operations on B Tree, any property of B Tree may violate such as number of
minimum children a node can have. To maintain the properties of B Tree, the tree may split or join.
Operations:
Searching :
Searching in B Trees is similar to that in Binary search tree. For example, if we search for an item 49 in
the following B Tree. The process will something like following :
Inserting
Insertions are done at the leaf node level. The following algorithm needs to be followed in order to
insert an item into B Tree.
1. Traverse the B Tree in order to find the appropriate leaf node at which the node can be inserted.
2. If the leaf node contain less than m-1 keys then insert the element in the increasing order.
3. Else, if the leaf node contains m-1 keys, then follow the following steps.
Insert the new element in the increasing order of elements.
Split the node into the two nodes at the median.
Push the median element upto its parent node.
If the parent node also contain m-1 number of keys, then split it too by following the
same steps.
Example:
Insert the node 8 into the B Tree of order 5 shown in the following image.
Deletion
Deletion is also performed at the leaf nodes. The node which is to be deleted can either be a leaf node
or an internal node. Following algorithm needs to be followed in order to delete a node from a B tree.
If the the node which is to be deleted is an internal node, then replace the node with its in-order
successor or predecessor. Since, successor or predecessor will always be on the leaf node hence, the
process will be similar as the node is being deleted from the leaf node.
Example 1
Delete the node 53 from the B Tree of order 5 shown in the following figure.
53 is present in the right child of element 49. Delete it.
Now, 57 is the only element which is left in the node, the minimum number of elements that must be
present in a B tree of order 5, is 2. it is less than that, the elements in its left and right sub-tree are also
not sufficient therefore, merge it with the left sibling and intervening element of parent i.e. 49.
Application of B tree
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. However, if we use B Tree to index this database, it will be searched in O(log n) time in worst
case.
B+ Tree
B+ Tree is an extension of B Tree which allows efficient insertion, deletion and search operations.
In B Tree, Keys and records both can be stored in the internal as well as leaf nodes. Whereas, in B+ tree,
records (data) can only be stored on the leaf nodes while internal nodes can only store the key values.
The leaf nodes of a B+ tree are linked together in the form of a singly linked lists to make the search
queries more efficient.
B+ Tree are used to store the large amount of data which can not be stored in the main memory. Due to
the fact that, size of main memory is always limited, the internal nodes (keys to access records) of the B+
tree are stored in the main memory whereas, leaf nodes are stored in the secondary memory.
The internal nodes of B+ tree are often called index nodes. A B+ tree of order 3 is shown in the following
figure.
Advantages of B+ TreeRecords can be fetched in equal number of disk accesses.Height of the tree
remains balanced and less as compare to B tree.We can access the data stored in a B+ tree sequentially
as well as directly.Keys are used for indexing.Faster search queries as the data is stored only on the leaf
nodes.
B Tree VS B+ Tree
Insertion in B+ Tree
Step 2: If the leaf doesn't have required space, split the node and copy the middle node to the next
index node.
Step 3: If the index node doesn't have required space, split the node and copy the middle element to the
next index page.
Example :
Insert the value 195 into the B+ tree of order 5 shown in the following figure.