Professional Documents
Culture Documents
Introduction to B-Tree
B-trees are balanced search tree.
More than two children are possible.
B-Tree, stores all information in the leaves
and stores only keys and Child pointer.
If an internal B-tree node x contains n[x]
keys then x has n[x]+1 children.
Example of B-Tree
Application of B-Tree
Properties of B-Tree
Height of B-tree
Theorem:
If n 1, then for any n-key B-tree T of height h and minimum degree
t 2,
h logt (n+1)/2
Proof:
The root contains at least one key
All other nodes contain at least t-1 keys.
There are at least 2 nodes at depth 1, at least 2t nodes at depth 2, at
least 2ti-1 nodes at depth i and 2th-1 nodes at depth h
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Complexity=
O(logt n)
INSERT
B-TREE-SPLIT-CHILD
ALGORITHM
B-TREE-SPLIT-CHILD(x,i,y)
1. z ALLOCATE-NODE()
2. leaf [z] leaf [y]
3. n[z]t-1
4. for j 1 to t-1
5.
do keyj [z] keyj+t [y]
6. if not leaf [y]
7.
then for j1 to t
8.
do cj [z] cj+t [y]
9. n[y] t-1
cont.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
B-TREE-INSERT ALGORITHM
B-TREE-INSERT(T,k)
1. r root[T]
2. If ( n[r] = 2t-1)
3. then s Allocate-Node()
4.
root[T] s
5.
leaf[s] FALSE
6.
n[s]0
7.
c1[s]r
8.
9.
10.
B-TREE-SPLIT-CHILD(s,1,r)
B-TREE-INSERT-NONFULL(s,k)
else B-TREE-INSERT-NONFULL(r,k)
t=3
Insert Example
GMPX
t=3
ACDE
JK
NO
RSTUV
YZ
Insert B
GMPX
ABCDE
JK
NO
B-Trees - 21
RSTUV
YZ
ABCDE
JK
NO
RSTUV
YZ
Insert Q
GMPTX
ABCDE
JK
NO
QRS
B-Trees - 22
UV
YZ
ABCDE
JK
NO
QRS
UV
YZ
UV
YZ
Insert L
P
GM
ABCDE
Comp 750, Fall 2009
JKL
NO
TX
QRS
B-Trees - 23
ABCDE
JKL
TX
NO
QRS
UV
YZ
UV
YZ
Insert F
P
CGM
AB
DEF
JKL
NO
TX
QRS
B-Trees - 24
Constructing a B-tree
Suppose we start with an empty B-tree and
keys arrive in the following order:1 12 8 2
25 6 14 28 17 7 52 16 48 68 3 26
29 53 55 45
We want to construct a B-tree of degree 3
The first four items go into the root:
1
8 12
1
12
8
2
25
6
14
Constructing a B-tree
Add 6 to the tree
28
17
7
52
16
48
68
3
26
29
8 12 25
Exceeds Order.
Promote middle and
split.
1
12
8
2
25
6
14
28
17
7
52
12 25
16
48
68
3
26
29
2
1
6
2
12 14
25
28
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
6
2
25 28 28
12 14 17
1
12
8
2
25
6
14
28
8 17
17
7
52
16
48
68
3
26
29
76
12 14
16
25 28 52
48
1
12
8
2
25
6
14
28
17
7
52
8 17
16
48
68
3
26
29
6 7
12 14 16
25 28 48 52 68
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
2
3
6 7
12 14 16
25 28
52 68
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
8 17 48
12 14 16
25262829
52536855
1
12
8
2
25
6
14
28
17
7
52
8 17 48
Exceeds Order.
Promote middle and
split.
Exceeds Order.
Promote middle and
split.
16
48
68
3
26
29
12 14 16 25 26 28 29 45
52 53 55 68
34
Copyright The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
The Concept
You can delete a key entry from any node.
->Therefore, you must ensure that
before/after deletion, the B-Tree maintains
its properties.
When deleting, you have to ensure that a
node doesnt get too small (minimum node
size is T 1). We prevent this by
combining nodes together.
Deletion Cases
Case 1: If the key k is in node x and x is a
leaf node having atleast t keys - then
delete k from x.
x
k
t keys
x
leaf
t1 keys
Simple Deletion
Case 1: We delete F
Result: We remove F from the leaf node. No further
action needed.
x
not a leaf k
y
t keys
pred
k of k
t keys
L
Source: Introduction to Algorithms, Thomas H. Cormen
x
not a leaf k
z
t keys
succ
k of k
B-Trees - 44
t keys
x
not a leaf k
y
z
t1 keys t1 keys
x
not a leaf
y
ys keys, k, zs keys
2t1 keys
G J
B-Trees - 48
x
not a leaf k1
ci[x]
ci[x]
k2
k1
t1 keys
t keys
k
Comp 750, Fall 2009
k
B-Trees - 49
Deleting B
Before:
x
not a leaf k1
ci[x]
ci[x]
t1 keys
2t1 keys
k
ci+1[x]
t1 keys
recursively
descend x
B-Trees - 51
D E
12 29 52
15 22
31 43
56 69 72
CSCI 2720
12 29 56
52
15 22
31 43
56 69 72
CSCI 2720
15 22
31 43
69 72
Too few keys!
Delete 72
CSCI 2720
15 22
31 43 56 69
CSCI 2720
15 22
31 43 56 69
Delete 22
CSCI 2720
15 29
43 56 69
CSCI 2720