B+ tree
B+ tree
• Index records contain search key value and a pointer to the actual record on the
disk.
Indexing is defined based on its indexing attributes.
• Primary Index − Primary index is defined on an ordered data file. The data file is ordered on a key field.
The key field is generally the primary key of the relation.
• Secondary Index − Secondary index may be generated from a field which is a candidate key and has a
unique value in every record, or a non-key with duplicate values.
• Clustering Index − Clustering index is defined on an ordered data file. The data file is ordered on a non-
key field.
Ordered Indexing is of two types −
• Dense Index
• Sparse Index
1. Dense Index
• In dense index, there is an index record for every search key value in the database.
• This makes searching faster but requires more space to store index records itself.
• Index records contain search key value and a pointer to the actual record on the disk.
2. Sparse Index
• In sparse index, index records are not created for every search key.
• An index record here contains a search key and an actual pointer to the data on the disk.
• To search a record, we first proceed by index record and reach at the actual
location of the data.
• If the data we are looking for is not where we directly reach by following the index, then
the system starts sequential search until the desired data is found.
Multilevel index
• A B+ tree is a balanced binary search tree that follows a multi-level index format.
• B+ tree ensures that all leaf nodes remain at the same height, thus balanced.
• Additionally, the leaf nodes are linked using a link list; therefore, a B+ tree can support
random access as well as sequential access.
B+ Tree
Internal nodes −
• Internal (non-leaf) nodes contain at least ⌈n/2⌉ pointers, except the root node.
• At most, an internal node can contain n pointers.
Leaf nodes −
• Leaf nodes contain at least ⌈n/2⌉ record pointers and ⌈n/2⌉ key values.
• At most, a leaf node can contain n record pointers and n key values.
• Every leaf node contains one block pointer P to point to next leaf node and forms a linked list.
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.
Advantages of 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 B+ Tree
Search keys can not be repeatedly stored. Redundant search keys can be present.
Data can be stored in leaf nodes as well as internal Data can only be stored on the leaf nodes.
nodes
Searching for some data is a slower process since Searching is comparatively faster as data can only
data can be found on internal nodes as well as on be found on the leaf nodes.
the leaf nodes.
Deletion of internal nodes are so complicated and Deletion will never be a complexed process since
time consuming. element will always be deleted from the leaf nodes.
Leaf nodes can not be linked together. Leaf nodes are linked together to make the search
operations more efficient
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.
Insertion in B+ Tree
The node contains greater than the maximum number of elements i.e. 4, therefore split it and place the median node up to
the parent.
Now, the index node contains 6 children and 5 keys which violates the B+ tree properties, therefore we need to
split it, shown as follows.
Deletion in B+ Tree
Step 2: if the leaf node contains less than minimum number of elements, merge down the
node with its sibling and delete the key in between them.
Step 3: if the index node contains less than minimum number of elements, merge the node
with the sibling and move down the key in between them.
Example
Delete the key 200 from the B+ Tree shown in the following figure.
200 is present in the right sub-tree of 190, after 195. delete it.
Merge the two nodes by using 195, 190, 154 and 129.
Now, element 120 is the single element present in the node which is violating the B+ Tree properties. Therefore, we need to
merge it by using 60, 78, 108 and 120. Now, the height of B+ tree will be decreased by 1.