B Trees
B Trees
B-Trees
ch for a perfect method to access data in files started with Indexing. Index is
The sear d to be a good too l f or searc h.mg records ma
.d · file.
· But indexing assumes a sorted
. s1 ere
con . t. Cosequentla
· l processing
· met h o d s are used for sortmg · large and very large files.
index. 1isue of using
. bmary
. h . d 1·
searc over m ex 1st was emphasized m . . the previous chapter. It
. issr that binary searc h is
The . use f u l an d e ff.1cienl
. . . required data in an index file.
for fmdmg
isButc1ea . d . h . d . .
there are problems associate wit 11, an ts discussed in the following section .
111MS04IS073I !
1MS06IS038
l11S041S073
of
al
11
1MS06IS010
Generally in an ordered list, the Binary Search Tree is very imbalanced. Therefore,
binary search is not preferred for data files.
einary Tree
age d
of Binary Tree .
called Paged Binary Tree , 1· s use d to ov
J
·12 P ·
-ariat1° d AVL trees. Here , the nodes of the tree a crcome the problem
1Ai•'()"
I es
11 an .
tr• ed into the memory, the entire page containing the re
re arranged ·
rn pages . Whenever
ofbde is toa_d d·aaraDl of the paged structure is given in Fi· g 7 q3uued node 1s loaded.
po ,. c 1 ::::, ure
' ch•Jll• 0 . .
fbC 5
[n Figure 7.3, each page contains three nodes. Every root with its child nodes form a
separate page which is loaded _together. Consider searching the node named A in the tree .
Without the paging structure, 1t would require four access to reach the node A. But, with
paged structure, the node is reached in two access. In general, the number of access
required in paged binary tree is log k+ 1 (N + I) where k is the number of nodes per page and
Nis total number of records in the tree .
The paging mechanism used in Figure 7. 3 is not easy to design because it is difficult to
decide the position of the node in a page . If all the elements at same level is stored in a
single page, paging scheme becomes ineffective . Maintaining all the child nodes of a root
in a single page will be expensive, if it is not a perfect binary tree. The idea of using paged
bmary tree as a solution to the problem of file organization is D.ot useful. Therefore, the
char ac t enshcs
· · of ideal solution should be as follows:
(i) The tree should be balanced
(ii) Maintaining the structure of .the tree after insertion and deletion should be as easy
as searching
(iii) Inst ea d
f accessmg
. · one record (node) at a time, ii is better to rea d more no d es.
O
7,4 B-Trees
ee (also k
B-Tr f · l tion
mentioned . nown as Balanced Tree) fulfills all the requirements of an e_f oct " ·o so."
(i) E sn th e ealier section. The main characteristics of B-Trec are given belo\\
.. Each
(u) ach nod e contams
. more than one element (Record).
node has the same maximum number of records .
-~W~ d
J6S fi/e~r . vnYs ba lance t alter the ba1ance d status of h
,,,stl'1 ddocsno l et
f the t1cv . ran tee
~ h.:-1ght o . n is caste . . .
(iii) Th1. . \ ,111d dclctto T ee of order m s1gmf1es that
. ) Inset t iot
(t' · t or
det' · B- r
. -1iaracterized bl 1 s ents in one node.
B-Trc< ,s J h, a, \~e 01a~iJllun1 ,n el e:Jll of ,rt child nodes.
a) There 1ave max1lll 1 .Id nodes.
b) All nodes ca~ lminilllUlll of two c 11 hould have m/2 child nodes .
oot can ha"e pt leaf nodes, s
c) R d exce 1
d) All other no d ses,
are at the salll e leve .• qual to the number of child node
Allleafnoe •nanode1se s
e) b of elements 1
f) The nutn er ve a minimum of two elements and ma .
ot shou ld h a · · / XUnu111
erh' implies that ro f des) should have a mm1mum m 2 element
The last prop •1 nodes ( exc ept lea no de of order 4 1s
The other . sh own m· p·1gure 7 4 san~
melements. A typical B-Tree no ··
a maximum m elements. _ ___,r777,,r-1W/2r-~~-~~
Child Child
Node 1 Node 2
Figure 7.4 B-Tree Node
The elements inside the nodes are arranged in a sorted order. Consider building the B-
Tree for the index given in Figure 5. 7 and the first node created will be as shown in Fig1m
7.5.
1nsert eYerY
. element
1· from the list to the nod .
(i)
.. one e it s full, sp 1l the node into t c unttl it · r
1 d WO nod . " u\1
01) the ne:d no es. es. Dtstribute th
.·· froJII each node, promote the highest elem e keys equally among
(111) C ntinue the process until the list is ent lo the parent node
. ·) o T f complete
rne
('
1 er•
ation of B- ree or the index list given
. . Figure
m · . as r ll·
5 .7 1S
. . O ows
o) nsert first four records mto a new no de (Figure
. 7.6a) ·
1
b) The is full.
nodevalue
higher fromNext
the insertion trigersisth e sp rit. This
two new nodes . node is split and key with
promoted to the
are promote and not the (key-address) comb.mat10n
. (Figure
. parent node . Only keys
7.6b)
d
IMS05IS047
IMS04IS073 1000
300
c) The parent node (root node) just acts as pointers. Only nodes at the leaf level will
contain the address of a record. After the node is split, new record is inserted
The next record to be inserted has the key value of I MS06IS00 l. This key is
compared with the first element of root (IMS O41SO88) . Since the new key is
11 The
larger than the first key the right child of the root is considered for insertion.
11
nght child node has t~o elements. Since the order of B Tree is fo ': can
accommodate two more elements. The new record with key JMS06!SOOl ". mserted
~s t_he last element of this node as the elements inside the node are considered to
Th · · d · B Tree bccnusc
e m sorted order 00
• e msertion of I MS06IS001 triggers the updation proce ure tn ; O ; 1 node
I!( e highest element of its node, it should be protllotcd to t pa
18 th
Figure 7.6c).
, ...o File St111cturos
L\IS04IS073 IMS05IS0 47
f) The nex t element IMS 07IS001 can b e directly entered as the third child nodeof
root. The parent is updated since it is the hi ghest element in that node_- is
g) Insertion of 1MS07ISO 10 again causes a split . This time the third child
6
~~d\d
split into two . The third node now h as t w o element s , (I MS0 ~~~~ wil~b(
(]MS06IS013). The next tw o elements (1MS06 IS038) and (IMS07IS also b(
1
the mem hers of the fo urth node . Th e n ew e lem ent (I MS07ISO O) cant nodell'
. d . d . the roo
rn serte rnto the four th nod e. Appropr iate changes are one tn
accomm odate the new insertion .
B- Trco 17 /
a)
bJ Inserting T
1w1 I I
r=,·10 scrucrures
~ lnserlingY
1-: ,,
Q y
oIJfClwl IYl
Inserting U
u w y
Inserting I
w y
u
e)
I
Inserting 0
y
T
w y
f) u
T
E I
Inserting P
g) w y
u
E I
Inserting A
h)
u w y
A E
i)
Inserting S
A E u w y
8-r,_ l7J
Insening D
lnsening F
R S T
Inserting J
Inserting K
H I
p
c.J StftlC ,u,oS
1•, r,, l
I nc;ert1ng I.
-rr_1_}ii_l_l.TI
q)
nl
[
p Q
R S T
E F [
Inserting z
o)
I y z
A D
E F p Q S T
p) Inserting X
J~ J1
w
B- rree 17~
Inserting c
y z
Inserting V
y z
V W
Inserting B
y z
V W
, ..(t r=I·io -~ tr oc tu re
I
s
\)
ln seni ng N
V W
L 0
u)
Inserting M
K
y z
H I
V W
L N 0
Figure 7 .7 Build
ing B-Tre e
7.4.2 R e d is tr ib u ti o n
Keys of
Consider the st
atu s o r B -T rce
full and in se rt io giv cn in F i g u rc
7 . 6e. T h e fo u rt
h ,.,ond " ':
n o f any c le m en h n o d e of~ .°one 1
T h er e ca n b e
a sm a ll ch an g t into th at n o d
e c a u se s tr in•''
\<I#
e in th e met h
o d s th e k ey s ar e e to grow ibal freq•"1
e in se rt ed so
B-Tree 177
oeletion
.4.Jprocess
Th• . of an element from the B-Tree is as follows
of deletion
1
X p element to be deleted
I. Start with root and reach the node containing x.
2. If Xis not the maximu m element in that node and if deleting X does not make the
. for example, in the B-Tree shown in Figure 7.6f, consider deleting !MS07IS045. Since
d " not e maximum element of its node it can be deleted directly. If JMS06!S00l is
· should be merge d10wit· h
aneleted ' thhen the node containing ii suffers' from underflow. So 11
11
0
Y one 1of its sibling nodes . If JMS07IS099 is deleted, then the parent node has be
del •1ed Since
mod1f · it
. .IS the highest element of its node. The process of merging
· '"· case_ f
ret•o_n follows the same procedure as explained in Section 7.5. 2 while discuss111g0
ins;s;nbution of keys in B-Tree The summary vi<"' of maintaining B-Trco is thnt dun~g
Yi,1: ;~n and deletion, it should ·be noted that none of the properties of B-Trccs should
1