Week 4 B Tree Slide 3
Week 4 B Tree Slide 3
p1.
Multiway search tree
A tree was defined as either an empty structure or a structure
whose children are disjoint tree t1,t2,…tk. Each node of this kind
of tree can have more than two children. This tree called a
multiway tree of order m, or an m-way tree.
A Multiway search tree of order m, or an m-way search tree, is
a Multiway search tree in which
Each node has m children and m-1 keys
The keys in each node are in ascending order
The keys in the first i children are smaller the ith key
The keys in the last m-i children are larger than the ith key
M-way search tree m-way tree
Binary search tree binary tree
p2.
Introduction of B-tree
p3.
Thm :
If n,then
1 t 2,
for any n-key B-tree T of height h and minimum degree
n 1
h logt 2 .
Proof :
1
1
t- t- 2
1 1
t t
t- t- t- t- 2t
1 1 1 1
t t t t 2t2
h
n 1 (t 1) 2hi 1
i 1
th 1
1 2(t 1) 2t h 1.
t 1
n 1 h n 1
t . logt 2 h.
2
A B-tree is not a binary tree because B-tree has
many more than two children
B-trees may be formulated to store a set of
elements or a bag of elements. (a given elements
can occur many times in a bag but only once in a
set)
A B-tree is balanced.
Every leaf in a B-tree has the same depth
2-3-4 tree (discussed by Rudolf Bayer): a B-tree of
order 4 (min degree=2)
p5.
The elements in a B-tree node
Rule 1:the root may have as few as one elements (or even no
elements if it also has no children); every other node has at least
minimum elements
Rule 2: the maximum number of elements in a node is twice the
value of minimum
Rule 3: the elements of each B-tree node are stored in a partially
filled array, sorted from the smallest elements (at index 0) to the
largest elements (at the final used position of the array)
Rule 4: the number of subtrees below a nonleaf node is always one
more than the number of the elements in the node.
Rule 5: for any leaf node: (1) an element at index i is greater than
all the elements in subtree number i of the node, and (b) an
element at index i is less than all the elements in subtree number
i+1 of the node.
p6.
66 88
p7.
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.
p8.
B-Tree-Search(x,k) :
Algorithm :
B-Tree-Search(x,k)
{ i 1
while i n[ x] and k keyi [ x]
do i i 1
if i n[ x] and k keyi [ x]
then return( x, i )
if leaf [ x] then return NULL
else DISK - READ(Ci [ x])
return B - Tree - Search(Ci [ x], k )
}
B-Tree-Create(T)
{ x Allocate Node()
Leaf [ x] TRUE
n[x] 0
DISK - WRITE(x)
root[T] x
}
time : O (1)
p10.
B-Tree-Split-Child(x,i,y) :
Splitting a node in a B-Tree :
Keyi-1[x]
x keyi 1 keyi keyi 1
t=4 x Keyi [x] …N S W
…N …
W… y=Ci[x] z=Ci+1[x]
y=Ci[x]
P Q R T U V
P Q R S T U
full V
T1 T2 T3 T4 T5 T6 T7 T8
T1 T2 T3 T4 T5 T6 T7 T8
Splitting a full node y ( have 2t-1 keys ) around its median key key t[y] into
2 nodes having (t-1) keys each.
p11.
Algorithm :
B-Tree-Split-Child(x,I,y)
{ z Allocate - Node()
leaf[z] leaf[y]
n[z] t 1
for j 1 to t - 1 do key j[ z ] key jt [ y ]
if not leaf[y] then
for j 1 to t do C j[ z ] C jt [ y ]
n[y] t 1
for j n[ x] 1 downto i 1 do C j1[ x] C j[ x]
C j1[x] z
for j n[ x] downto i do Key j1[ x] Key j[ x]
Keyi [ x] Key t [ y ]
n[x] n[ x ] 1
DISK - WRITE(y)
DISK - WRITE(z)
DISK - WRITE(x)
} p12.
B-Tree-Insert(T,k) :
Insert a key in a B-Tree :
Root[T]
t=4 S
Root[T]
H
r
r
A D F H L N
P A D F L N P
T1 T2 T3 T4 T5 T6 T7 T8
T1 T2 T3 T4 T5 T6 T7 T8
p13.
Algorithm :
B-Tree-Insert(T,k)
{ r root[T ]
if n[r] 2t - 1 then
{ S Allocate - Node()
root[T ] S
leaf [S] FALSE
n[S] 0
Ci [S] r
B-Tree-Split-Child(S,l,r)
B-Tree-Insert-Nonfull(S,k)
}
else B - Tree - Insert - Nonfull(r, k)
}
p14.
B-Tree-Insert-Nonfull(x,k) :
Algorithm :
B-Tree-Insert-Nonfull(x,k)
{ i n[ x]
if leaf[x] then
{ while i 1 and k keyi [ x]
do { keyi1[ x] keyi [ x]
i i 1 }
keyi1[ x] k
n[ x] n[ x ] 1
DISK - WRITE(x) }
else
{ while i 1 and k keyi [ x]
do i i - 1
i i 1
DISK - READ(Ci [x])
if n[Ci [x]] 2t - 1
then B - Tree - Split - Child(x, i, Ci [x])
if k keyi [x] then i i 1
B-Tree-Insert-Nonfull(Ci[x],k) }
}
Example : Inserting keys into a B-Tree.
t=3
(a) Initial tree
G M P
X
A C D J K N R S T U Y
E O V Z
(b) B inserted
G M P
X
G M P T
A B C J K N R S T U Y X
D E O V Z
R U
S V
(c) Q inserted
G M P T
X
A B C J K N Q R S U Y
D E O V Z
(d) L insert
P
G T
M X
A B C J K N Q R U Y
D E L O S V Z
(e) F insert
P
C G T
M X
A B D E J K N Q R U Y
F L O S V Z
Deleting a key from a B-Tree :
( x has t keys )
1. K is in x and x is a leaf :
x
K
delete k from x.
2. K is in x and x is an internal node :
x
a.
K Recursively delete k’ and replace k by k’ in x.
y
k’ t keys
x
b.
K
z
k’
t keys
x
c. if both y,z has t-1 keys.
K
Merge y,z and k into y.
z
y
Recursively delete k from y.t+ t+ 2t-1 K
1 1
3. If K is not in internal node x :
x
K
Ci[x]
k is in this subtree.
a. If Ci[x] has only t-1 keys but has a sibling with t keys
x
• Move a key from x down to Ci[x].
K
Ci[x] • Move a key from Ci[x]’s sibling to x.
t-1 t
• Move an appropiate child to C i[x] from its siblin
b. If Ci[x] and all of Ci[x]’s siblings have t-1 keys, merge ci with one sibling.
x
0
Ci[x]
t-1 t-1 2t-1
key 0
key keys
s s
Example : Deleting a key from a B-Tree.
t=3
(a) Initial tree
P
C G T
M X
A B D E J K N Q R U Y
F L O S V Z
(b) F delete : case 1
P
C G T
M X
A B D E J K N Q R U Y
L O S V Z
(c) M delete : case 2a
P
C G T
L X
A B D J K N Q R U Y
E O S V Z
(d) G deleted : case 2c
P
C L T
X
A B D E J N Q R U Y
H O S V Z
(e) D deleted : case 3b
C L P T
X
A B E J K N Q R U Y
O S V Z
(e’) tree shrinks in height
C L P T
X
A B E J K N Q R U Y
O S V Z