Chapter 18
B-Trees
Lee, Hsiu-Hui
Ack: This presentation is based on the
lecture slides from Prof. Tsai, Shi-Chun as
• B-Tree 性質:
在 B-Tree 中若 node x 有 n[x] keys, 則 x 含有 n[x]+1 個 children.
M
D H
J K L B C F G
Q T X V W Y Z N P R S
• 典型之 B-Tree 運用情況:
資料量龐大無法全部存在 main memory
處理 B-Tree 之 algorithm 只將部分之資料 copy 至 main memory.
Memory Main Secondary Memory
(
disks )
(
RAM )
object some
pointer to
← a x
READ(x) -
DISK
of x fields the
modify and/or
access that
operations
Definition of B-trees
B-Tree T : is a rooted tree with the properties:
1
. Every node x has the following fields:
a. n[x]:# of keys currently stored in node x b.
c. leaf[x] is true if x is a leaf; false if x is an internal node.
2. Each internal node x contains n[x]+1 pointers C
1[x] , C
2[x] ,… , C
n[x]+1[x] to its children.
Leaf nodes have no children ( i.e. its C
i[x] undefined).
3. If k
iis any key stored in the subtree with root C
i[x]
] [ ]
[ ]
[ x key x key
[ ]x key
1≤
2≤ L ≤
n xkey1 key2
K3 K2
K1
C1[x] C2[x] C3[x x
] [ ]
[ ]
[ ≤ ≤ ≤ ≤ ≤ ≤
+≤ key x k key x k key x k
k L
4. All leaves have the same depth, which is the tree’s height h.
5. t: minimum degree of the B-tree.
Every node (other than root) must have at least t-1 keys.
(have at least t children) Every node can contain at most 2t-1 keys.
(have at most 2t children)
A node is FULL, if it contains exactly 2t-1 keys
.
2-3-4 tree:
When t=2, every internal node has either 2, 3, or 4 children
Theorem 18.1
• The larger the value of t, the smaller the height of the B-tree.
If , then for any n -key B-tree T of height h and minimum degree ≥ 1
n
,
≥ 2 t
. log 2
+ 1
≤ t n
h
Proof:
1t-1 t-1
t-1 t-1 t-1 t-1
t t
t t t t
1
2
2t 2t2
− ∑ +
≥
=
h − i
t i
t n
1
2
1
) 1 ( 1
. 1 1 2
) 1 1 ( 2
1 ⎟⎟= −
⎠
⎞
⎜⎜
⎝
⎛
−
⋅ −
− +
=
h t h
t t t
# of nodes
. log
. h
n t
nt
h
≥
+ ≥
+2 1
2
1
Basic operations on B-trees
• convention:
• Root of the B-tree is always in main memory.
• Any nodes that are passed as parameters must already have
had a DISK_READ operation performed on them.
• Operations:
• Searching a B-Tree.
• Creating an empty B-tree.
• Splitting a node in a B-tree.
• Inserting a key into a B-tree.
• Deleting a key from a B-tree.
B-Tree-Search(x,k)
Total CPU time
).
log (
)
( th O t n
O = t
Ex. B-Tree-Search(x,R)
Creating an empty B-tree
) 1 ( O
Total CPU time
Splitting a node
Splitting a full node y ( have 2t-1 keys ) around its median key
into 2 nodes having (t-1) keys each.
full
B-Tree-Split-Child(x, i, y)
Insert a key in a B-Tree
• Splitting the root is the only way to increase the height of a B-tree.
• Example:Inserting keys into a B-Tree.
t=3
A C D E
(a) Initial tree
J K N O R S T U V Y Z G M P X
G M P T X
(c) Q inserted
A B C D E J K N O R S T U V Y Z G M P X
(b) B inserted
R S U V G M P T X
(d) L insert
A B C D E J K L N O Q R S U V Y Z P
G M T X
(e) F insert
P
C G M T X
• Deleting a key from a B-Tree:
1. K is in x and x is a leaf:
2. K is in x and x is an internal node:
a.
b.
( x has t keys ) ≥
K x
delete k from x.
K x
k’
≥ t keys
y
Recursively delete k’ and replace k by k’ in x.
K x
k’
≥ t keys
z
3. If K is not in internal node x:
a. If Ci[x] has only t-1 keys but has a sibling with t keys
b. If Ci[x] and all of Ci[x]’s siblings have t-1 keys, merge ci with one sibling.
x
Ci[x]
k is in this subtree.
t-1
x
Ci[x] t
• Move a key from x down to C
i[x].
• Move a key from C
i[x]’s sibling to x.
• Move an appropriate child to C
i[x] from its sibling
0 x
t-1
keys 0 2t-1
keys Ci[x] t-1
keys
• Example:Deleting a key from a B-Tree.
t=3
(a) Initial tree
(b) F delete:case 1
A B J K L N O Q R S U V Y Z
P
C G M T X
D E F
A B J K L N O Q R S U V Y Z
P
C G M T X
D E
(c) M delete:case 2a
P
(d) G deleted:case 2c
A B N O Q R S U V Y Z
P
C L T X
D E J H
(e) D deleted:case 3b
C L P T X
A B E J K N O Q R S U V Y Z
(e’) tree shrinks in height
C L P T X
A B E J K N O Q R S U V Y Z
(f) B delete:case 3a
E L P T X