You are on page 1of 8

B+ TREES Examples

B Trees. B Trees are multi-way trees. That is each node contains a set of keys and pointers. A B Tree with
four keys and five pointers represents the minimum size of a B Tree node. A B Tree contains only data
pages.
B Trees are dynamic. That is, the height of the tree grows and contracts as records are added and deleted.
B+ Trees A B+ Tree combines features of !A" and B Trees. t contains inde# pages and data pages. The
data pages always appear as leaf nodes in the tree. The root node and intermediate nodes are always inde#
pages. These features are similar to !A". $nlike !A", overflow pages are not used in B+ trees.
The inde# pages in a B+ tree are constructed through the process of inserting and deleting records. Thus,
B+ trees grow and contract like their B Tree counterparts. The contents and the number of inde# pages
reflects this growth and shrinkage.
B+ Trees and B Trees use a %fill factor% to control the growth and the shrinkage. A &'( fill factor would
be the minimum for any B+ or B tree. As our e#ample we use the smallest page structure. This means that
our B+ tree conforms to the following guidelines.
)umber of *eys+page ,
)umber of -ointers+page &
.ill .actor &'(
"inimum *eys in each page /
As this table indicates each page must have a minimum of two keys. The root page may violate
this rule. The following table shows a B+ tree. As the e#ample illustrates this tree does not have a
full inde# page. 01e have room for one more key and pointer in the root page.2 n addition, one
of the data pages contains empty slots.
B+ Tree with four keys
3
Adding Records to a B+ Tree
The key value determines a record4s placement in a B+ tree. The leaf pages are maintained in se5uential
order A)6 a doubly linked list 0not shown2 connects each leaf page with its sibling page0s2. This doubly
linked list speeds data movement as the pages grow and contract.
1e must consider three scenarios when we add a record to a B+ tree. 7ach scenario causes a different
action in the insert algorithm. The scenarios are8
The insert algorithm for B+ Trees
Leaf Page
Full
Index Page
FULL
Action
)9 )9 -lace the record in sorted position in the appropriate leaf page
:7! )9
3. !plit the leaf page
/. -lace "iddle *ey in the inde# page in sorted order.
;. <eft leaf page contains records with keys below the middle key.
,. =ight leaf page contains records with keys e5ual to or greater
than the middle key.
:7! :7!
3. !plit the leaf page.
/. =ecords with keys > middle key go to the left leaf page.
;. =ecords with keys ?@ middle key go to the right leaf page.
,. !plit the inde# page.
&. *eys > middle key go to the left inde# page.
A. *eys ? middle key go to the right inde# page.
B. The middle key goes to the ne#t 0higher level2 inde#.
. the ne#t level inde# page is full, continue splitting the inde#
pages.
/
Illustrations of the insert algorithm
The following e#amples illlustrate each of the insert scenarios. 1e begin with the simplest scenario8
inserting a record into a leaf page that is not full. !ince only the leaf node containing /& and ;' contains
e#pansion room, we4re going to insert a record with a key value of /C into the B+ tree. The following
figures shows the result of this addition.
Add =ecord with *ey /C
Adding a record hen the leaf page is full !ut the index page is not
)e#t, we4re going to insert a record with a key value of B' into our B+ tree. This record should go in the
leaf page containing &', &&, A', and A&. $nfortunately this page is full. This means that we must split the
page as follows8
Left Leaf Page Right Leaf Page
50 55 60 65 70
The middle key of A' is placed in the inde# page between &' and B&.
The following table shows the B+ tree after the addition of B'.
Add =ecord with *ey B'
;
Adding a record hen !oth the leaf page and the index page are full
As our last e#ample, we4re going to add a record containing a key value of D& to our B+ tree. This record
belongs in the page containing B&, C', C&, and D'. !ince this page is full we split it into two pages8
Left Leaf Page Right Leaf Page
75 80 85 90 95
The middle key, C&, rises to the inde# page. $nfortunately, the inde# page is also full, so we split the
inde# page8
Left Index Page Right Index Page "e Index Page
25 50 75 85 A'
The following table illustrates the addition of the record containing D& to the B+ tree.
Add =ecord with *ey D&
,
Rotation
B+ trees can incorporate rotation to reduce the number of page splits. A rotation occurs when a leaf page
is full, but one of its sibling pages is not full. =ather than splitting the leaf page, we move a record to its
sibling, adEusting the indices as necessary. Typically, the left sibling is checked first 0if it e#ists2 and then
the right sibling.
As an e#ample, consider the B+ tree before the addition of the record containing a key of B'. As
previously stated this record belongs in the leaf node containing &' && A' A&. )otice that this node is full,
but its left sibling is not.
Add =ecord with *ey /C
$sing rotation we shift the record with the lowest key to its sibling. !ince this key appeared in the inde#
page we also modify the inde# page. The new B+ tree appears in the following table.
llustration of =otation
&
#eleting $e%s from a B+ tree
1e must consider three scenarios when we delete a record from a B+ tree. 7ach scenario causes a
different action in the delete algorithm. The scenarios are8
The delete algorithm for B+ Trees
Leaf Page
Belo Fill
Factor
Index Page
Belo Fill
Factor
Action
)9 )9
6elete the record from the leaf page. Arrange keys in ascending order
to fill void. f the key of the deleted record appears in the inde# page,
use the ne#t key to replace it.
:7! )9
Fombine the leaf page and its sibling. Fhange the inde# page
to reflect the change.
:7! :7!
3. Fombine the leaf page and its sibling.
/. AdEust the inde# page to reflect the change.
;. Fombine the inde# page with its sibling.
Fontinue combining inde# pages until you reach a page with
the correct fill factor or you reach the root page.
As our e#ample, we consider the B+ tree after we added D& as a key. As a refresher this tree is printed in
the following table.
Add =ecord with *ey D&
A
#elete &' from the B+ Tree
1e begin by deleting the record with key B' from the B+ tree. This record is in a leaf page containing A',
A& and B'. This page will contain / records after the deletion. !ince our fill factor is &'( or 0/ records2
we simply delete B' from the leaf node. The following table shows the B+ tree after the deletion.
6elete =ecord with *ey B'
#elete () from the B+ tree
)e#t, we delete the record containing /& from the B+ tree. This record is found in the leaf node
containing /&, /C, and ;'. The fill factor will be &'( after the deletionG however, /& appears in
the inde# page. Thus, when we delete /& we must replace it with /C in the inde# page. The
following table shows the B+ tree after this deletion.
6elete =ecord with *ey /&
B
#elete *' from the B+ tree
As our last e#ample, we4re going to delete A' from the B+ tree. This deletion is interesting for several
reasons8
3. The leaf page containing A' 0A' A&2 will be below the fill factor after the deletion. Thus, we must
combine leaf pages.
/. 1ith recombined pages, the inde# page will be reduced by one key. Hence, it will also fall below
the fill factor. Thus, we must combine inde# pages.
;. !i#ty appears as the only key in the root inde# page. 9bviously, it will be removed with the
deletion.
The following table shows the B+ tree after the deletion of A'. )otice that the tree contains a single inde#
page.
6elete =ecord with *ey A'
C

You might also like