0% found this document useful (0 votes)
6 views17 pages

B+ Tree

Indexing is a data structure technique that enables efficient retrieval of records from databases using key fields. There are various types of indexing, including primary, secondary, and clustering indexes, as well as dense and sparse indexing. B+ trees are a specific type of balanced tree structure used for indexing, allowing efficient insertion, deletion, and search operations, with data stored only in leaf nodes and linked for efficient access.

Uploaded by

verginjose12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views17 pages

B+ Tree

Indexing is a data structure technique that enables efficient retrieval of records from databases using key fields. There are various types of indexing, including primary, secondary, and clustering indexes, as well as dense and sparse indexing. B+ trees are a specific type of balanced tree structure used for indexing, allowing efficient insertion, deletion, and search operations, with data stored only in leaf nodes and linked for efficient access.

Uploaded by

verginjose12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Indexing

• Data is stored in the form of records.

• Every record has a key field, which helps it to be recognized uniquely.

• Indexing is a data structure technique to efficiently retrieve records from the


database files based on some attributes on which the indexing has been done.

• Index records contain search key value and a pointer to the actual record on the
disk.
Indexing is defined based on its indexing attributes.

Indexing can be of the following types −

• 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

• Index records comprise search-key values and data pointers.


• As the size of the database grows, so does the size of the indices.
• There is an immense need to keep the index records in the main memory so as to speed up the
search operations.
• If single-level index is used, then a large size index cannot be kept in memory which leads to
multiple disk accesses.
• Multilevel index is stored on the disk along with the actual database files.
Multi-level Index helps in breaking down the index into several smaller indices in order to make the
outermost level so small that it can be saved in a single disk block, which can easily be accommodated
anywhere in the main memory.
B+ Tree

• A B+ tree is a balanced binary search tree that follows a multi-level index format.

• The leaf nodes of a B+ tree denote actual data pointers.

• 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

• Records 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 B+ Tree

Search keys can not be repeatedly Redundant search keys can be present.
stored.
Data can be stored in leaf nodes as well Data can only be stored on the leaf
as internal nodes nodes.

Searching for some data is a slower Searching is comparatively faster as data


process since data can be found on can only be found on the leaf nodes.
internal nodes as well as on the leaf
nodes.
Deletion of internal nodes are so Deletion will never be a complexed
complicated and time consuming. process since 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 1: Insert the new node as a leaf node

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 1: Delete the key and data from the leaves.

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

You might also like