B+ Tree

Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

B Tree & B+ Tree

B - Tree
• B-Tree is m way search tree (m is odd number)
• It is a self-balanced search tree in which every node
contains multiple keys and has more than two children.
• Large degree B-trees used to represent very large
dictionaries that reside on disk.
• Smaller degree B-trees used for internal-memory
dictionaries to overcome cache-miss penalties
Properties of B-Tree
Operations on a B-Tree

• Search
• Insertion
• Deletion
Search Operation in B-Tree
Insertion Operation in B-Tree
(Same as construction)
Example Construct a B-Tree of Order 3 by inserting numbers from 1 to 10.
Constructing a B-tree : Example 2
• Suppose we start with an empty B-tree and keys arrive in the
following order:
1 12 8 2 25 5 14 28 17 7 52 16 48 68 3 26 29 53 55 45
• We want to construct a B-tree of order 5
• The Prst four items go into the root:

1 2 8 12

• To put the Pfth item in the root would violate rule (maximum m-1
nodes)
• Therefore, when 25 arrives, pick the middle key to make a new root
Constructing a B-tree (contd.)

1 2 12 25

6, 14, 28 get added to the leaf nodes:


8

1 2 6 12 14 25 28
Constructing a B-tree (contd.)
Adding 17 to the right leaf node would over-fill it, so we take the
middle key, promote it (to the root) and split the leaf
8 17

1 2 6 12 14 25 28

7
,52
,16
,48g
eta
dde
dtot
hel
eafno
des
8 17

1 2 6 7 12 14 16 25 28 48 52
Constructing a B-tree (contd.)
Adding68c a
usesust ospl
itt
heri
ghtmostlea
f,pr
omoti
ng48tot
he
r
oot,andadding3c ause
sustospl
itt
heleftmostl
eaf
,pr
omoti
ng3
t
other oo
t;26,29,53,55thengoint
otheleav
es
3 8 17 48

1 2 6 7 12 14 16 25 26 28 29 52 53 55 68

Adding 45 causes a split of 25 26 28 29

and promoting 28 to the root then causes the root to split


Constructing a B-tree (contd.)

17

3 8 28 48

1 2 6 7 12 14 16 25 26 29 45 52 53 55 68

B-Trees 17
Removal from a B-tree
During insertion, the key always goes into a leaf. For deletion we wish to
remove from a leaf. There are three possible ways we can do this:

• 1 - If the key is already in a leaf node, and removing it doesn’t cause that
leaf node to have too few keys, then simply remove the key to be deleted.

• 2 - If the key is not in a leaf then it is guaranteed (by the nature of a B-


tree) that its predecessor or successor will be in a leaf -- in this case we
can delete the key and promote the predecessor or successor key to the
non-leaf deleted key’s position.
Removal from a B-tree (2)
• If (1) or (2) lead to a leaf node containing less than the minimum number of keys
then we have to look at the siblings immediately adjacent to the leaf in question:
• 3: if one of them has more than the min. number of keys then we can promote
one of its keys to the parent and take the parent key into our lacking leaf
• 4: if neither of them has more than the min. number of keys then the lacking leaf
and one of its neighbours can be combined with their shared parent (the opposite
of promoting a key) and the new leaf will have the correct number of keys; if this
step leave the parent with too few keys then we repeat the process up to the root
itself, if required
Type #1: Simple leaf deletion

Assuming a 5-way
B-Tree, as before... 12 29 52

2 7 9 15 22 31 43 56 69 72

Delete 2: Since there are enough


keys in the node, just delete it
Type #2: Simple non-leaf deletion

Delete 52
12 29 56
52

7 9 15 22 31 43 56 69 72

Borrow the predecessor


or (in this case) successor
Type #4: Too few keys in node and
its siblings

12 29 56

Join back together

7 9 15 22 31 43 69 72
Too few keys!
Delete 72
Type #4: Too few keys in node and
its siblings

12 29

7 9 15 22 31 43 56 69
Type #3: Enough siblings

12 29
Demote root key and
promote leaf key

7 9 15 22 31 43 56 69

Delete 22
Type #3: Enough siblings

12 31

7 9 15 29 43 56 69
Advantages of B tree

• Ordered sequential access over the key value on O(n) time.


• O(log n) insert time, while maintaining the ordering of the items.
• O(log n) delete time of items within the B-Tree.
• If sequential access is handled through the B-Tree then O(log n)
delete time is provided for the underlying table as well.
Comparison

Basis for comparison B-tree


Binary tree

A node can have at max M


number of child A node can have at max 2
Essential constraint
nodes(where M is the order number of subtrees.
of the tree).
Used It is used when data is It is used when records and
stored on disk. data are stored in RAM.
logM N (where m is the order
Height of the tree log2 N
of the M-way tree)
Code indexing data Code optimization, Hueman
Application
structure in many DBMS. coding, etc
What is a B+ Tree?
– A variation of B trees in which
– internal nodes contain only search keys (no data)
– Leaf nodes contain pointers to data records
– Data records are in sorted order by the search key
– All leaves are at the same depth

A B+ tree is a balanced tree in which every path from the root of


the tree to a leaf is of the same length, and each non-leaf node of
the tree has between [M/2] and [M] children, where n is Pxed for
a particular tree.
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.
B+ Tree insertion
• Insert at bottom level
• If leaf page overjows, split page and copy middle element to next index
page
• If index page overjows, split page and move middle element to next
index page
Insertion – Example 2
Consider the following tree.
B+ Tree Deletion
• Delete key and data from leaf page
• If leaf page underjows, merge with sibling and delete
key in between them
• If index page underjows, merge with sibling and move
down key in between them

You might also like