B+ Trees: What Are B+ Trees Used For Whatisabtree What Is A B+ Tree Searching Insertion Deletion
B+ Trees: What Are B+ Trees Used For Whatisabtree What Is A B+ Tree Searching Insertion Deletion
B+ Trees: What Are B+ Trees Used For Whatisabtree What Is A B+ Tree Searching Insertion Deletion
Maximum branching
factor of M
Complete tree has
depth = logMN
Each internal node in
a complete tree has
M - 1 keys
Binary search tree is a
B Tree where M is 2
B Trees
B-Trees are specialized M-ary
search trees
Each node has many keys
subtree between two keys
x and y contains values v 3 7 1221
such that x v < y
binary search within a node
to find correct subtree
Each node takes one
full {page, block, line}
x<3 3<x<7 7<x<12 12<x<21 21<x
of memory (disk)
B-Tree Properties
• Properties
– maximum branching factor of M
– the root has between 2 and M children or at most M-1 keys
– All other nodes have between M/2 and M records
– Keys+data
• Result
– tree is O(log M) deep
– all operations run in O(log M) time
– operations pull in about M items at a time
What is a B+ Tree?
k0 k1 … ki-1 __ … __
0 1 i-1 M - 2
• Leaf
– (Key, DataPointer)* L in each node
– first j Keys currently in use
k0 k1 … kj-1 __ … __
0 1 L-1
Data for k0 Data for k2 Data for kj-1
Example
B+ Tree with M = 4
Often, leaf nodes
linked together 1040
3 152030 50
1 2 10 11 12 202526 4042
3 5 6 9 1517 30323336 506070
Advantages of B+ tree usage for
databases
keeps keys in sorted order for sequential traversing
uses a hierarchical index to minimize the number of
disk reads
uses partially full blocks to speed insertions and
deletions
keeps the index balanced with a recursive algorithm
In addition, a B+ tree minimizes waste by making
sure the interior nodes are at least half full. A B+ tree
can handle an arbitrary number of insertions and
deletions.
Searching
Just compare the key value with the data in
the tree, then return the result.
For example: find the value 45, and 15 in
below tree.
Searching
Result:
1. For the value of 45, not found.
2. For the value of 15, return the position
where the pointer located.
Insertion
inserting a value into a B+ tree may
unbalance the tree, so rearrange the tree if
needed.
Example #1: insert 28 into the below tree.
25 28 30
Fits inside the
leaf
Insertion
Result:
Insertion
Example #2: insert 70 into below tree
Insertion
Process: split the leaf and propagate middle key
up the tree
50 55 60 65 70
Does not fit
inside the
leaf
50 55 60 65 70
Insertion
Result: chose the middle key 60, and place it
in the index page between 50 and 75.
Insertion
The insert algorithm for B+ Tree
Leaf Index Node Action
Node Full Full
NO NO Place the record in sorted position in the appropriate leaf page
IF the next level index node is full, continue splitting the index nodes.
Insertion
Exercise: add a key value 95 to the below
tree.
75 80 85 90 95
Leaf node
full, split the
75 80 85 90 95
leaf. 25 50 60 75 85
Insertion
Result: again put the middle key 60 to the
index page and rearrange the tree.
Deletion
Same as insertion, the tree has to be rebuild if the
deletion result violate the rule of B+ tree.
Example #1: delete 70 from the tree
OK. Node
>=50% full 60 65
Deletion
Result:
Deletion
Example #2: delete 25 from below tree, but 25
appears in the index page.
But…
28 30
This is
OK.
Deletion
Result: replace 28 in the index page.
Add 28
Deletion
Example #3: delete 60 from the below tree
65
Less than
50 55 65 50% full
Deletion
Result: delete 60 from the index page and
combine the rest of index pages.
Deletion
Delete algorithm for B+ trees