0% found this document useful (0 votes)
32 views

B Trees

1) B-trees are self-balancing search trees that allow efficient retrieval of records in large datasets. 2) B-tree nodes can contain multiple child nodes and data elements. This allows B-trees to balance search operations across multiple pages for faster retrieval compared to traditional binary search trees. 3) B-trees maintain the property that all leaf nodes are at the same depth, keeping search operations efficient during insertions and deletions by local rebalancing.

Uploaded by

Ricky Naaz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

B Trees

1) B-trees are self-balancing search trees that allow efficient retrieval of records in large datasets. 2) B-tree nodes can contain multiple child nodes and data elements. This allows B-trees to balance search operations across multiple pages for faster retrieval compared to traditional binary search trees. 3) B-trees maintain the property that all leaf nodes are at the same depth, keeping search operations efficient during insertions and deletions by local rebalancing.

Uploaded by

Ricky Naaz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

7

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 .

1.1 Binary Search Tree


Consider the primary index given in Figure 5. 7 and if the binary search algorithm is used,
then it creates a Binary Search Tree as shown in Figure 7 .1.

111MS04IS073I !
1MS06IS038

Figure 7.1 Binary Search Tree

lfanew . S001 · · t d it will


he th record havmg University Seal Number (USN) 1MS041 ts mser e '
large~ left child of node having USN 1MS04IS073. The creation of binary search tree
Y depends h · p· 7 I is written after
consid enng
• th on •t e .order of arrival of keys . The tree . Ill
• igure d · d .
the Btnnn
S'"ch Tree e enl!re list at once. But if the keys arnve m a sorte or er, ·
appears as given in Figure 7 .2 :
166 File Structures

l11S041S073

of
al
11

1MS06IS010

Figure 7 .2 Binary Search Tree

Generally in an ordered list, the Binary Search Tree is very imbalanced. Therefore,
binary search is not preferred for data files.

7.2 Height Balanced Trees


In general, a perfect binary tree gives an average performance of log (N+ 1) for Nkeys.
Though it is quite impressive compared to linear search, there are 2problems with sucl
trees as discussed in the previous section i.e., unbalanced growth. Therefore, there is~
need for height balanced trees.
If a tree is a height-balanced(x) tree it means that the maximum allowable differences
between · nodes of any' parent is x In general height-balance d'D~
] the leve]s of child
1 d AV ' hn
.
~re ~a e as 1. L Trees. Th_e tree given . F 1gure
m . Al'Ls r~•·
7.· l is an A VL' Tree; b~t, the tree 0''
In Figure 7:2 s not. Now, tf the tree in Figure 7 .2 has to be converted into an
then there 1s a need for manipulation i
. , are n(
Though height bal.anced trees give performance I O(l .4 f-logi(N f- 2))1, the~ 11ee a~
preferred b.ecause , ,t is qu_ue expensive. The other problem with binary searc efore, 111
AVL trees is that at any given point of lime, only one node is processed. Tber
total time required to search lhc entire l . h
ree 1s muc 1onger.
B-Tree /6 7

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

Figure 7 .3 Paged Binary Tree

[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.

IMS04IS073 IMS04IS088 IMS04IS091 IMS05IS047


300 800 900 1000

F'igure 7·5 B-Tree Node for Student Index

Every record inside the .


adccd~ts_s of records from B-TrneoedeBcon~1s_ts of both key and address This enables direi'
a 1 1onal b d · Y storm ·
index file wi~; b:n of _searching index fileg
copied into B-Tr d.
rsl · ·11 be t
the USN in the B-Tree, there WI dr~•
or th e address . Therefore each recor
ee irectly al . '
7.4.1 Creatio ong with the address .
If n of B-Tree
the index set is. alread
Y present, B-T
ree can be created in the following waY,.
B-Tree 169

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

IMS04IS073 IMS04IS088 IMS04IS091

300 800 900


Figure 7.6 (a) Creation of B-Tree

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

Figure 7.6 (b) Creation of B-Tree

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

d) Th"' in ,1.'Jl~(,·1 of lr\1S0 6 IS0l 3 i s simp le . Tt is in se rt ed .


tn to th
• I .
I.
n.)ot notk Smee 1t 1s th e hi ghe st e cment of i ts nod e ' . . c righ
ll IS p l Ch·
node. rornot lld
Cd to or t~
the c
too,
IMS04IS088 IMS06IS0 0I

L\IS04IS073 IMS05IS0 47

300 900 1000

Figure 7.6 (c) Creation of B-Tree

e) The ne,,· key I MS06IS03 8 is directed towards the right child of r .


. . . oot for I
But the nght child node already contains four el ements which i th nse~ion
limit. Therefor e, the node is split and the new node share the avai~abl: :axunulll
The highest elements from all the three n ode s are promote d to the elllents.
l MS O6ISO 3 8 as then be inserted as the third child node of the root P~_ent1 11
n~de
the highest element of its node, the key i s promote d to root (Figure. 7_6;;e, is

~-1 S041S073 IMS04IS09 1


300 900

Figure 7.6 (d) Creation of B-Tree

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 /

· t ke, in se rt e d 1s ll MS0 71S0 45 . C' o111pa 1in gth '


.
. d . .
I ne~ ro, kc '1s .~,1lh th e c l em en ts ,n the
f1~t- the four th no ~ is t le pct fee t in atc h the
Th) . ~1n cc lh c fo urlh node has
ro ,· three kc) s .. th_is Ot~c can be acco mmo date d c toot has lo be mod1 f1ed t o
(Fig ure 7.6c ) . .
onl. . this kc) mil .
contain
re kc, h a, rng USN 1 MS0 7IS0 99 and a dd rcss 2000 1. .
10 . - I 1 . s inser ted. thi s kc} ,s
i.) If one ured with t 1e e eme nts 111 the root nod e . Tl:us .
key sh Id b
•on1pa d . . 1 y full. Ther efore th· . ou e in serte d 10
i•
~ tl child node an it·r 1s. . a read , lS node lS spl't
I
four 1,, elem ent. B ut 1 1t ts sp it direc tly the n b 1 to ac comm odate
the ne the orde r of B Tree is four this is 'no t
poui~b~r of child node s become fi ve
Sinctee. e orow s by one mor e leve l'. ssi e . In Such cases . the le, el of
the r o . . .
i th f .
fourt h no de 1s spht mto htwo dnode s . The keys n e ourth node is eq ua 11 Y
Tlie . th f an the newl y crea ted f'fth
. tribu ted betw een e ourt
dis d Th . l node . Thes e t\\ o. node s
e fust , seco nd and th · d d f
.·n
1 oet a new
h
pare nt
1
no e .
f Ir no

e orm their own
.
s is prom oted t 0 th eir
0
''
rent. The h1g est e emednt rom
·
all node . respe ctive pare nt
pa . t th
nodes. The tw· o pare nt no es , m turn , will prom ote their high est ke ys o e new
7 6£) .
root node (Fig ure .

Figu re 7 .6 (f) Creation of B-Tree

ess of crea ting B-Tr ees . The orde r


Anoth er exam ple is cons ider ed to illus trate the proc
of the B-Tree is assu med to be four .
Input Keys - Q, W, E, R, T, Y, U , I , 0 , P, A , S , D
, F, G, H , J, K , L , Z, X, C, V, B, N , 11

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

ur. In the Figure


t occents 7. 6e,. it is observed th at first
.
ocs 110 less than the1r
. maximum capacity Tl f '. secon d and third nodes are
• lT ele!Jl
~3,111::i ,,evs in the no e cou
d ld be shared with the . th·
1ered ore ' tn stea d of sphttmg
. . the fourth
",ode· th••·ts 'split to a later stage. S1m1larly,
d · · if the th'ud noded ·. Th.is makes th.e fourth node
' e1 h · ir no e 1s f 11 d
stP d there it can s are its key with the second d T u an an element has to
1
b' ,n0tere
11 shown ' 1n. Figure
. 7.8. no e. he B-Tr ee after redtstnbution
. .
0
P
aPPears as

Figure 7.8 Redistribution in B-Tree

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

node underflow, just delete X.


3. If X is not the maxim um element of that node and if deleting X does make the
node underflow then delete X and merge the node with ony of its available sibling
node. Make appropriate changes in the parent nodes .
4. If X is the maximum ele me nt of that node and deleting X does not make the node
underflow, then delete X and update the parent node .
5. If X is the maximum element of that node and deleting X does make the node
underflow, then delete X and merge the node with its sibling node and make

changes to the parent node .

. 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

You might also like