0% found this document useful (0 votes)
17 views

Lesson 04

Uploaded by

pramuapex
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Lesson 04

Uploaded by

pramuapex
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 58

ADVANCED DATABASE MANAGEMENT

SYSTEMS
ICT3273

B+ Tree

Nuwan Laksiri
Department of ICT
Faculty of Technology
University of Ruhuna Lecture 04
WHAT WE DISCUSS TODAY ……..
• RECAP INDEXING

NEXT WEEK

• QUERY PROCESSING
RECAP

• INDEXING
TREE
TREE
TREE
BINARY TREE
SELF BALANCING TREE
m-WAY SEARCH TREE
• The m-way search trees are multi-way trees which are
generalized versions of binary trees where each node contains
multiple elements.
• In an m-Way tree of order m, each node contains a maximum
of m – 1 elements and m children.
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.
• Every node in a B-Tree contains at most m children.
• Every node in a B-Tree except the root node and the leaf node contain
at least m/2 children.
• The root nodes must have at least 2 nodes.
• All leaf nodes must be at the same level.
• It is not necessary that, all the nodes contain the same number of
children but, each node must have m/2 number of nodes.
B TREE OPERATIONS
• Search
• Searching in B Trees is similar to that in Binary search tree.
• Insert
• Traverse the B Tree in order to find the appropriate leaf node at which
the node can be inserted.
• If the leaf node contain less than m-1 keys then insert the element in the
increasing order.
• 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.
• Delete
B TREE OPERATIONS
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.
• Locate the leaf node.
• If there are more than m/2 keys in the leaf node then delete the desired key from the
node.
• If the leaf node doesn't contain m/2 keys then complete the keys by taking the element
from right or left sibling.
• If the left sibling contains more than m/2 elements then push its largest element up
to its parent and move the intervening element down to the node where the key is
deleted.
• If the right sibling contains more than m/2 elements then push its smallest element up
to the parent and move intervening element down to the node where the key is
deleted.
• If neither of the sibling contain more than m/2 elements then create a new leaf node by
joining two leaf nodes and the intervening element of the parent node.
• If parent is left with less than m/2 nodes then, apply the above process on the parent too.

If 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.
INTRODUCTION: B+ TREE
• B+ tree is a storage method in a tree like structure
• B+ trees are used in DBMS for accessing and organizing data
more efficiently
• B+ tree is a balanced binary search tree. It follows a multi-
level index format
• B+ trees are dynamic. The height of the tree grows and
contracts as records are added and deleted
• The keys in the tree are ordered
• It supports efficient equality and range search
BASICS OF B+ TREE
• B+ tree has one root, internal nodes and leaf nodes
• All leaf nodes will have the actual records stored. If we have to
search for any record, they are all found at leaf node
• Internal nodes will have only pointers to the leaf nodes; it not
has any data
• Every path from the root of the tree to a leaf is of the same
length
• Every leaf node of the B+ tree contains one block pointer P to
point to next leaf node
• It contains index pages and data pages
BASICS OF B+ TREE
STRUCTURE OF A B+ TREE
STRUCTURE OF A B+ TREE
STRUCTURE OF A B+ TREE (PAGE FORMAT)
STRUCTURE OF A B+ TREE (PAGE FORMAT)
STRUCTURE OF A B+ TREE
EXAMPLE B+ TREE
B+ TREE PROPERTIES
• Balanced tree
• Every node except root must be at least ½ full

• Order: the number of keys/pointers in a non-leaf node


• Fanout of a node: the number of pointers out of the node
• Fill Factor: the value that determines the percentage of space
on each leaf-level page to be filled with data. Fill factor is
used to control the growth and shrinkage
OPERATIONS IN B+ TREE
• Search
• Insertion
• Deletion
B+ TREE: SEARCH OPERATION
• Two cases
• Successful search
• Unsuccessful search
B+ TREE: SEARCH OPERATION
B+ TREE: SEARCH OPERATION
Searching for Key Value 6
• Read block B3 from disc
• Is B3 a leaf node? No
• Is 6 <=5? No
• Read block B2
• Is B2 a leaf node? No
• Is 6 <=7? Yes
• Read block L2
• Is L2 a leaf node? Yes
• Search L2 for the key value 6 Successful search
B+ TREE: SEARCH OPERATION
Searching for Key Value 5
• Read block B3 from disc
• Is B3 a leaf node? No
• Is 5 <=5? Yes
• Read block B1
• Is B1 a leaf node? No
• Is 5 <=3? No
• Read block L3
• Is L3 a leaf node? Yes
• Search L3 for the key value 5 Successful search
B+ TREE: SEARCH OPERATION
Searching for Key Value 2
• Read block B3 from disc
• Is B3 a leaf node? No
• Is 2 <=5? Yes
• Read block B1
• Is B1 a leaf node? No
• Is 2 <=3? Yes
• Read block L1
• Is L1 a leaf node? Yes
• Search L1 for the key value 2 Unsuccessful search
EXAMPLE: SEARCHING
• Since no structure change in a B+ tree during a searching
process, so just compare the key value with the data in the tree,
then give the result back
• For example: find the value 45, and 15 in below tree
EXAMPLE: SEARCHING
• Result:
• For the value of 45,
not found
• For the value of 15,
return the position where the pointer located
B+ TREE: INSERTION OPERATION
• Insert a record when
• Case 1:
If both leaf node and index node are not full
• Case 2:
If leaf node is full and index node is not full
• Case 3:
If both leaf node and index node are full
B+ TREE: INSERTION OPERATION
Rule to insert a record
1. If the node has an empty space, insert the key/reference pair
into the node
2. If the node is already full, split it into two nodes, distributing
the keys evenly between the two nodes
• If the node is a leaf, take a copy of the minimum value in the
second of these two nodes and repeat this insertion algorithm to
insert it into the parent node
• If the node is a non-leaf, exclude the middle value during the
split and repeat this insertion algorithm to insert this excluded
value into the parent node
INSERTION: CASE 1
• Insert 28 into the below tree
INSERTION: CASE 1
• Result:
INSERTION: CASE 2
• Insert 70 into below tree
INSERTION: CASE 2
• Process: split the tree
INSERTION: CASE 2
• Result: chose the middle key 60, and place it in the index page
between 50 and 75.
INSERTION: CASE 3
• Insert a key value 95 to the below tree
INSERTION: CASE 3
• Insert a key value 95 to the below tree
INSERTION: CASE 3
• Result: again put the middle key 60 to the index page and
rearrange the tree.
B+ TREE: DELETION OPERATION
Deleting a record from B+ tree may result in,
• Case 1:
If both leaf node and index node does not go below the
fill factor
• Case 2:
If leaf node goes below fill factor and index node does
not go below the fill factor
• Case 3:
If both leaf node and index node go below the fill factor
B+ TREE: DELETION OPERATION
Rule to delete a record
1. 1.Perform the search process on the key of the record to be
deleted
• This search will end at a leaf L
2. If the leaf L contains more than the minimum number of
elements (more than m/2 - 1)
• Then the index entry for the record to be removed can be safely
deleted from the leaf with no further action
3. If the leaf contains the minimum number of entries, then the
deleted entry is replaced with another entry that can take its
place while maintaining the correct order
DELETION: CASE 1
• Same as insertion, the tree has to be rebuild if the deletion
result violate the rule of B+ tree.
• Delete 70 from the tree
DELETION: CASE 1
• Result
DELETION: CASE 2
• Delete 25 from below tree, but 25 appears in the index page
DELETION: CASE 2
• Delete 25 from below tree, but 25 appears in the index page
DELETION: CASE 2
• Result: replace 28 in the index page
DELETION: CASE 3
• Delete 60 from the below tree
DELETION: CASE 3
• Delete 60 from the below tree
DELETION: CASE 3
• Result: delete 60 from the index page and combine the rest of
index pages
EXAMPLE
Construct a B+ tree with 2, 3, 5, 7 and 11 values,
fanout = 4,
1. Insert 17, 19, 23, 29, 31, 9, 10 and 8 respectively
to the tree, created above.

2. Delete 23, 19, 17 values from the above tree.


EXAMPLE
• After insertion
EXAMPLE
• After deletion
HOME WORK
• Duplicate handling B+ Tree
• Key Compression B+ Tree
• Bulk Loading B+ Tree
SUMMARY

• Recap INDEXING
• B+ Tree
• B+ Tree Operations
• Search
• Insert
• Delete
REFERENCES

• Fundamentals of database systems


(6th edition) by remez elmasri & shamkant B. Navathe )

• Database Management Systems


(3rd edition) - by Raghu Ramakrishnan and Johannes Gehrke, McGraw Hill,
2003.

• Advanced Database Management Systems


by Rini Chakrabarti, Shibhadra Dasgupta
THANK YOU

You might also like