0% found this document useful (0 votes)
38 views10 pages

Btrees

A B-tree is a tree data structure that keeps data sorted and allows searches, sequential access, insertions, and deletions in logarithmic time. The key properties of a B-tree are: (1) nodes can have between m/2 and m children, (2) all leaves are at the same depth, and (3) internal nodes divide the key space among their children. The document provides examples of constructing a B-tree by inserting keys in order and shows how nodes split as they become full. It also summarizes that a B-tree of order m and height h can contain up to mh+1 - 1 keys.

Uploaded by

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

Btrees

A B-tree is a tree data structure that keeps data sorted and allows searches, sequential access, insertions, and deletions in logarithmic time. The key properties of a B-tree are: (1) nodes can have between m/2 and m children, (2) all leaves are at the same depth, and (3) internal nodes divide the key space among their children. The document provides examples of constructing a B-tree by inserting keys in order and shows how nodes split as they become full. It also summarizes that a B-tree of order m and height h can contain up to mh+1 - 1 keys.

Uploaded by

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

B-Trees

B-Trees

Definition of a B-tree
A B-tree of order m is an m-way tree (i.e., a tree where each
node may have up to m children) in which:
1. the number of keys in each non-leaf node is one less than the number
of its children and these keys partition the keys in the children in the
fashion of a search tree
2. all leaves are on the same level
3. all non-leaf nodes except the root have at least m / 2 children
4. the root is either a leaf node, or it has from two to m children
5. a leaf node contains no more than m 1 keys

The number m should always be odd

B-Trees

Constructing a B-tree
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 first four items go into the root:
1

12

To put the fifth item in the root would violate condition 5


Therefore, when 25 arrives, pick the middle key to make a
new root
B-Trees

Constructing a B-tree (contd.)


8

12

25

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


8

B-Trees

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

12

14

25

28

7,52,16,48getaddedtotheleafnodes
8

B-Trees

12

17

14

16

25

28

48

52

Constructing a B-tree (contd.)


Adding68causesustosplittherightmostleaf,promoting48tothe
root,andadding3causesustosplittheleftmostleaf,promoting3
totheroot;26,29,53,55thengointotheleaves

12

14

Adding 45 causes a split of

17

16
25

48

25
26

26
28

28

29

52

53

55

68

29

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


B-Trees

Constructing a B-tree (contd.)


17

B-Trees

28

12

14

16

25

26

29

48

45

52

53

55

68

Inserting into a B-Tree


Attempt to insert the new key into a leaf
If this would result in that leaf becoming too big, split the leaf
into two, promoting the middle key to the leafs parent
If this would result in the parent becoming too big, split the
parent into two, promoting the middle key
This strategy might have to be repeated all the way to the top
If necessary, the root is split in two and the middle key is
promoted to a new root, making the tree one level higher

B-Trees

Exercise in Inserting a B-Tree


Insert the following keys to a 5-way B-tree:
3, 7, 9, 23, 45, 1, 5, 14, 25, 24, 13, 11, 8, 19, 4, 31, 35, 56

B-Trees

Analysis of B-Trees

The maximum number of items in a B-tree of order m and height h:


root
level 1
level 2
. . .
level h

m1
m(m 1)
m2(m 1)
mh(m 1)

So, the total number of items is


(1 + m + m2 + m3 + + mh)(m 1) =
[(mh+1 1)/ (m 1)] (m 1) = mh+1 1

When m = 5 and h = 2 this gives 53 1 = 124

B-Trees

10

You might also like