B+ Trees
B+ Trees
B+ Trees
B+ Trees
nC
previous chapter, B-Tree was used to organize index
ch
In the sets. The B-Tree
and leletion as easy as structure
insertion
arching, but, B-Tree has certain
makes
c there will be a need to access data both shortcomings.
most e to data stored in files can be
sequentially and randomly. The
accomplished through B-Tree. But, it
rando
IMSO7ISO10 IMS07IS045
Figure 8.1 Blocks for index list
The
keys design in Figure 8.1 assumes that block can contain four a
nablec the nged in the sorted order and the blocks are
enables keys. In the blocks, the
chained. The chaining of blocks
the address is quential access of recor It is observed that while
The pro also stored along with storing keys in blocks,
vith keys. But that is not shown in
Figure 8.1.
maiDeratntaioinnsinglike maintaining
the
les of
B-Tree.
a
the block sets (also called as sequence set) is similar to
These blocks
the nodes of B-Tree. also undergo split, merge and redisS
210 File Structures
Example:
Consider inserting key IMS04IS050 into the sequence set. The first block
block is
is
full.Therefore, it split
candidate for insertion. But, it is already and keys ar the
between new and old blocks. Now that there is space in the first block
the key 1s tributrigehj
redisinserte
IMS04IS050| IMS041SO73 IMS041S088
IMS04IS091 IMS051S047
IMSO7IS010 1MS07IS045
Figure 8.2 Inserting into sequence set
Procedure for deleting an element from the secquence set is same as the deletion Drocedh
re
followed in B-Tree and after deleting a node, the merge and redistribution functions s are
also applied.
If the index is understood as a separator, then many possibilities emerge. One interesne
inference is that the index element need not be a candidate of either of two lists
ov
which it operates. Consider the following two blocks from Figure 8.2.
B2 IMS04IS091 IMSOSIS047
Figure 8.3 Separators as index
Between the two blocks BI and B2, there is paralon
separator. Consider the sequence sets given in Figure 8.2. Between the normaly
of
sequence
set. If the index is
chosen in suchhaving
havingpossibility of
a
between two nsecutive a variable size
way that
Figure 8.4 illustrates suchsequence they are theindex set for a
separator
8.3 B+ Trees
Trees where the minimal separator is not
considered as the clement for index is
ac B+ Tree. Normally B+ Iree Is also characterized by its order. But since B+ called
Tree consists
af a sequence set and an index set, it 1s ident1fied by two orders. Let us assume that
the
arder of sequence set is n and order of index set is m. The properties of B+ Tree are as
follows.
easily Theprocess.
8.3.1 Creation of B+ Tree
c o n s t r u c t e d
can
be an
item at
a
time. Thepro
index set is already index list one
from the
reading
by
creating seq has
be
built
(Figure
8.5a).
it till
explained
Tree is inte
i nto
involved to create a B+ Push
nts
elements
node.
set
(0) Create a sequence
212 File Structures
If
(Ne m )
f (key
Insert (key)
Goto step 4
= m) u
Else if (N
Split (Node)
Insert (key)
<m)
Step3 I (N,
Promote (Lowest (Node))
Else if (N,
= =
m)
NewNode
= CreateNode (Node)
Promote (Lowest(NewNode))
Repeat Step 3 for all higher level indexes till the Root is reached.
Step4: Exit
() Splitting
Tis a process where a parent
node is split nto two child nodes (Figure 8.6a) by evenl
distributing the elements of the parent among its (two) children (Figure 8.6b).
AANELLI
E EG
Figure 8.6 (a) Splitting a Node
Node
Figure 8.6 (b) Splitting a
6.3.2
Deletion from B+ Tree
Algorithm
Let,
to delete akey from aB+ Tree is as follows:
N be the number of elements in a node
N in a node
elements of a node,
Node represent the
represent the elements ofthe sibling.
Node+1 - a function that deletes the key from the noda
Delete(key)
Lowest (Node) a function that returns the lowest element t
of the node
Delete-small (key) - a function that deletes the lowest element.
ot a node
and makes the required changes in the index
Merge (Node,, Node,1) a function that merge the elements of a nod.de
elements of the sibling making required change the
nodes index
Redistribute (Node,, Node,,1)-a function that redistributes the element
elements ot a
and its sibling making require nod
ode
changes index
in
nodes.
Stepl: Start from Root
If (key < separator)
Go left
else it (key = = separator
go right
else if (key >separator)
go right
Step2: If found Node,
If(N> Nmin and key =lowest (Node))
Delete (key)
Goto Step3
Else if (N.> Nmin and key = = lowest (Node))
Delete-small (key)
Goto Step3
Else if (N = =
Nmin and N+1= Nmin =
Delete (key)
Merge (Node; , Node,+ 1)
Goto Step 3
Else if (N.= =
Nmin and N,1> Nmin
Delete (key)
Redistribute (Node,, Node1)
Goto Step3
Step3: EXIT
0) Merging
t is a process where the elements of a node are merged with its sibling proviu
sibling has atleast one element less than the order of the sequence set.
B+ Tree 215
F o re x a m p l e :
(1) (2)
(3)
A BCD-TE
Figure 8.7 (a) Merging of Nodes
In the B+ shown in Figure 8.7a, if the element F is
making the required changes in the root as deleted, node (2) is
with
de (3) shown in Figure 8.7b. merged
(i) Redistribution
AE
(2) (3)
(1)
Redistribution of Keys
Figure 8.8 (a)
Redistribution
of Keys
8.8 (b)
Figure
216 File Structures
b)
Inserting T
E O
c)
ERLD Inserting YY
RTWY
d)
ER W Inserting U
e)
Inserting I
EC R w Inserting O
DENO R N Inserting P
E -O P MIU
TH
B+ Tree 217
Inserting A
RTUE
AO RN Inserting S
AL E
R SD 0
LAo R Inserting D
k)
AO D Inserting FF
oPQh Y|U
LR ST0
218 File Structures
CANOLLL Inserting G
R IS Tu
m)
CIAHOI Inserting KK
n)
Inserting L
A DTTTI
B Tree 219
OAO LO Inserting Z
Inserting X
Inserting CC
AME ICO
File S t r u c t u r e s
220
Inserting V
ANOLLLD
Inserting B
CANOILD
CAN O Inserting N
B+ Tree 221
Inserting M
TU-LD -ADMEMDUT
| LKLIIII RO MT
E G
CD-TE BT
A E
B-+ Tree
Figure 8.9 Building
Implementation
of B+ Tree
8.4 C++
IO Buffer class
an of
using file are
managed
8.4.1 Problem Statement They are
fields in the
data file. The constructed
stored in a is fixed. is
The Student file B' Tree
1n
records are
record in the
data The
The
limiters.
delimiters.
B
atelyseparalels
packed
into are
Set
variable length. They re
Set and
Index