You are on page 1of 39

ARu bELVI )

DNIT- E21-A (csE)


TREES KALAMAVUR||

PaNnRIES. 1S a finite bet of one

A bree des (gnated


tere is a, peciall desgnated
moe nod es 8uch hat
and ero
or more nen. empi
node
ode caled he Root
each afcshose
subtveas T,T T
directed edge om
Conneeted by a

a perent Tnie
Rcot A node whieh doesnt have
ig A
above tree Reot

Node -Tteim oinfomahion


wnich doesnl have ehildven i6 callecd
A node
Leaf Yes
B,k,LS1, H,I,T
ore

oY lerminal node. Here


aid to be
of the Same parents
are
8:bings- Children
& k,L
7 F,G1
S1blngs, Heve B, G,D ate siblingg.
are Bibiings
os a sEquunt
Path A path rom node. n, to ng is deftned
is he
suhthat i;
of nodes 0,,g3 k
44Lek. heve is exactl ony
pavent n1 for
to mot
ene
pat#h rom each noale
to L is
In he abeve t*ee, patn pm
T
is
uheve Ais he prent forC,
A,e,F,L.
the poent of Fand 19
the pavent of L
Length: The leng th is deined a9 the number 1 edes
the path
In the above tree, the 9eng th for he paih A to
is 3
Degree The urmber of ublrees of a ncde is callmd i
degee
3 3
Deree
evel: The level 'of a node is delined b initi

detting the Yoot be at Sevel one, 1{ a node ig


at level L then fts chihen are at level L+i.
Level of A is
Leve of B,C, p iS and So on

Height For any node n, the height o the nade nfs


fhe ength of e longest patß tom nto e
eat
The Height o the Sea is e

Tn the above tree, Height o nede F is i.


L i9
Height o} o.

BINARY TREE
Binary tree is a tvee in which no node Ci
Binory
have mote than
than two chiidren

aximum number o4 nodes a level i e a bina

trce is .
E

18
be tween Gieneral Tree k OLLE
Pfexence
aKALAMAVUR

i Tree
A Binary Ivee
*Greneral ree has
more than
has n o t
number o f ehildren
and tuoo childven

(1)

FoL BinARY REE


has
A full binary
brée of height.h
nodes Here height is .
No of nodes

binavy tree

# N o d e s

CoMPLETE BINARY TREE


tree of heighth bas.
A Complete binary leve .*
thhe
e

and Hnodes In
the bottom . leve
between a
.

elements should be iled rom Reft to right

LINKED EPRESENTATION OF A BNARY REE


The elements are represen ted
ug
ing pointers
Each nade in inked representahion has tree felds
he ett subtree
*Pointer to
*DaBa field
Poinler to the 1ghtBubbree

Tneaf nodes, beth he pointer ftelds ase a8aia


eiqnec
as NULL

EPRE8s10N REE
EXpression ee 18 a
binany tree n tohteh
The leaf odes are operands and e interior nedes ar

opera toTS. Like binary tree, expression


vee can als o
be traveTsed by inovder, preovder and postovder

raversal.

CoNSTRIUCTING AN ExPRESS1ON TRER


teps, an
pexform in
g he follouoing
expression tree Can be Cons truetedl

1 Kead .one symbol. at a time trom the postfix


expres8ion
. eheek twheher he symbol is an operand
b 0perator.

Cad I Be Symbol is an operand, create aa


..*

one node bree and' push a'pojnter onto the


stacl .

Cb) Tthe Symbo!is an


operatoy pop Fwo
pointer rom the 8tack
namely T, and 2 and
orm a new tree otth he soot as he
and T as lelt child and.
opesa
Ta as a tight ehild
A pointer to this new tsee is hen pushed onta
onto

the stack
Cab) Ccxld4e) )
Etample
abt c (KACAMAVOR
The tuoo Simbols are operandy, Bo
frst
Create One, nade tree and zush the pointer onto he

stack

NNert + 6ymbo is read, Bo too poirters are


poped,
a new ree is formed and a pointerto his is pushed
onto Re Btaak.

node tree
Next the opeand C is Tad, Eo a one
is rea ted and he pointer to t is pushed onto the

stack.

merged and he
so twó trees ase
Nous isread,
he
pointer to hefinal tvee is bus hed onto he_Btaak.

.:
'
SEARCHREE ADT BINARY SEARCH REE ,
DEFINITION
tsee is a binas ree in
n tohi
pia
Binary Keareh
Binary
forexe
jor exer node Xn he tree, the values of all e
Keye in its lett subtree ave Bnaller an *he key value
in x, and the alues of al he keys in its' righbt Bubtree
e lavger than e key value in X.

(22.)

(20

Declaration Routine For Binay Search Tree


trucb ceNode

int Elem en
SearchTree left;
8eorehTree Kight;
3
MAKE EMPTY tohen
tnitialization
This operatron ís for
mainl
as
to iniialize he tiret elament
the progammer prefer

one-node tree

IRoubinhe T Make An Emp Tree


MakeEmpty C8earch
Tree T
Bearch Tree
iCT=No)

Make Empty CT> lelt);


Make Empty (T>Riglt)
fyeeCT isALAMAVUR

eturn NUL;

IN&ERT element X into he tree,


insert he
To node T
T.
with he rot node
*Check
1s less than tRe vot,
* IfX subbree. Tecursive
Taverse the keft
aals to NoLl.Then
reaches the TRefb
untrit

X is paced in Tef
is. greater tRe reob,
an Re roob u
unnh
h il
T'x subbyee
recursively

Traverse tRe 11ht Then x-is placeol


NULL.
uals o
tYeoches he Tghb
io T>Riah Tree
Binany Seaveh
Tnto A
to Tnsert
/Roubine
Seareh Tree TnsertC int X, Siareh Tee TD
FCT==NuLL)

aslSkruct.TzeeNode))
Ta malloesice
Firsk element is placed in te mob
ifCT!=NoL 1

TElement =X,
Tleft = NuLL
TRight NoLL,

else
Cx T> Element
T TepInoertCx, T- lelt)
14 Cx.>T> Elernent)
T might Tnsert , T-RiahE );
Else x is in he tree aready
return T
3
EXamplee
To insert 8,5,10,15
irsElement 8 is Considered ás Roo

Taverse touoarda. left


As 548,

toards. ghe
l6>8 T 3avevse
(lo)

hethe Temaining0 element8


(io) are avered .

FIND OPERATION
* Check whetta he Too is NuLL 14 so tRen retmn
NuLL
Otherwt se, check heva lue x wi th he Toot node yalue
Cr.e, Tdata)
Ci) Tx is to T
cajual data, ve.turn T
a)TXis Sess Ra Tdara , lraverse the lept of
T reuTsvely
3) Tf x is qveater han T>data, TtaveTee fe right ANT

of T ecursively
Routine For Find Operahton
Search ree T)D
Tnt Find CInt X,

4CT-NoL)
Reurn NuLL
FCXT>Element)
Yeturn Find Cx,T->left),

else
i f x T>ERement)
vecurn Find x,T>Right)
else.
veurn T;

To Find an element 10

(t0
1o is checked oi tos Groko the righe
Dot
child of

(
child
1015, Goto he left
o is checked uoih
of 15
oith wot 1oCFound).
lo is che cked Mi
T
FIND MN 4

*This operation reburns the pasion ef he Aml C

element in Re hee. FI
* lo
pexform FindMin, start at the yoot and.go,keft
Long as thexe i
ong a left ehikd.The sopping point heinin
fhe
Smalest element
/Recursive Routine For FiNDMIN
int Find Min CSeavah Tree T)
E
CTeNu
Yeturn NuLL;
else 1fCT->left== NuLL)
Teturn T,
else
Tekurn p d i n (T-left);

Example
Root o

ca)T! NULL and T>left!=nuLL, C6)T!=NvLL and T{t!=NUL


Traverse left Traverse left )
w

T
CeTleft is No, eruTn T o minimum element

FiNDMAx
* Find Max Youine return he positfonf largest elements

in he byee
To pefortn a indhlax, tarst a ro ot and 9o nght as

(s
as
ag here i6 a nght child The sopping"po
ng
Ravgest element
For FINDNTAX
Roukine
Recursive
T
int Find MaxC SearohTree
i4C T NvLL)
return NuLL

else 1CT> Right == NuLi)

seturn T
else RindMaxCT->Right );
3
Example

Koot T
T
8
(b) T=NULL ano
T-Right != NuLL
T=NvLL and T>Right)=
NuLu,
Traver4e igh
Max
CaoT
eurn Tag
as aa maximu
maximu
is NoLL,
Ce Sinee 1Right
element
DEETE
To delete an etement, 'consider tRe oooing, E
possibilittes

Cose >Node to be delebed


is a Qeaf nade Gre)
chilhren
Case Node with one ehild

Cage 3 Node wih too dhtl4ken .

Delebio Koutine For Binayewdh Tvees.


Seavch ee DeleteCint x, Seareh Tree T)

int Tmpcell;
i T Nou)
EswrElement 0ot Found
else
ifCXT->Element)/ Travevhe tauards est
TleftDelete (x,T-Lep+),
elge
ifCX> T>Element) N Traverse touwavds vight
right

T Right Delete (x,T-> Right);


Found ement o be delebe.ol
else '
Too "iLdren
iACCTLeft k* T>Right)! =NuLL)
6ubtree
6malhst data in mght
wth
Replacee
Trnpeell-FidMin T>Right>,
Element Tmpc~tl>Elament,

TRight);
Right Delete (T> Element,

else One ar Zero Children

Tmpeell T,
ifCT>lest -*NoL):
T T- Riqht;
'

else if CT>Right - =NoLL)

TT
-see CImpcell);

Yeturn T .

Example
no children CLeaf nod1)
ASE 1> Noderh' :.

10
After delckion .
LeafNada(6
Before delting
Node with Ohe child'
CASE 2
be deleted
T theriode has one, ahild, it aan

to iks child node.


adjugbing its pavent pointer thatpoints
Velete 5

.
to
-havirg ene
chYAd

After TDelebiin
Befove delebion
The pointer Cusrently pointing the nade 5 is noio

made to its child noole b.

CASE 3 Node Jth Two Children


i
To delete. a node wbich has tuebildrer is
to eplace Re data of the node to be deleked wiA
Smallst data af the right subtree and vedunsiVely dee
that noode
To Delele 5

having The mioimum elunent at he


hild
nodes (15 right Subbree is 7.

(3 8

*Nouo the value + i6, repaced in

the posibion of 5.

( Since he
posikion of 41#s He
Leaf node' delebg. immediateld,
he nde 55
Afler deletng
uwO

10
C5

(3.i and Landis)


AVL TREECAdelson
Velski
hee exeept ha
savch
* An Av hee 4 ' a binayse the left a ght:
tree, the height of
node in the
fox evexy
gor ee atmost4

6ubtrees
can differ by bree. i6
is defined. to
defin ed. to
be
be 1.
the emply ubree
The height of 1he left
subree

is Re heighe oof 1Re


heighe
balanee. facor
A
height of the right subbree hould be
balarioe aetor
minus
bree all an AV
AVL
anAwL in
For node
he balance fackor f any ree has
H,0, or -1 .T than 1, he
or 9reatev
less, han 1
ree becomes either.
by making
to be balaneed . Cov
bions
ci Single Rota
Rotahons
Ca) Double
Ex
3F-

BF 0
aF-1

BF 0
Causes imbalane, when
ahy one
of he
ollocoing conditions occuT

An inserhion into the Re subbree of the left ahj

An insertion into the igh &ubbree o he. e# ehila

3 An inser tion into ha, Reft, subtree af Re nght bhild


ehild
4. Ani inseskiçn .igto he nght subtree of Re right

SINGLE KoTATION
( Singte hwtabian to ix Case 4
Gteneral Representation

Exashpkg

Tnser

Be.fore Rotahon After Rota hion

Example
O-
After Rotakion
Beore Kotation
C6al ances
CImbalaneed
Rotakion to ix Case 4
Single
Gtene ral epresentahon

Afle Rotation
Lnsera
Before Rotakon

EXomple

A)
Atler Rotahion
CBalanced)
Belore Rotaion. '.

CTmbalaneed
.

PoUBLE KoTHtioN
i DoubleRotakioto fix Case 2

(ki)

Besore Roation
(Ki R

Rotalion
After Dogbla
Example

(Tmbalaned) Aser, Rotal


Before Rotalion LBalanced
CN Double Rotahon To fx Case 3:
'.

ka

(ke)

Before Rotahion

()
After Rrotation
KALAMAVUR) 9

armple

(8 (A
Apler Rotakon

CBalanced)
B efoxe Rotaion

CIm balanced)
AVL node

/Routine To Compute height an


P)
statie int Height (sihon

ifC P = NuLL)
etun4
else
Yeturn P>Heigbt;

AVL Tree.
.lnsertion into an

TnsertCEkementlype: X, AveTee
AvL Tree
fCT = NULL)

AVENöde))
T= malloc CsireofCstruct
iS(T NULL
Fabal EvorOut of Space
else
T>Elenent - X;
THeight = 0;

TLeft T>Right= NoLl


3
ifCxTElement
TLett- TnsertCX, T>Left);
P HeightT->lept) -Height (T->Right)==*,
PC xT-> Left> Elment)
T Single Kotabe NihleHCT);
else
T- Double Robate lih Lef#(T);
else
f CX>T>ElementE

1Right Tneert, T>Right);


FCHaight CTRight) - Heigh+(T->lef)=a 2)
ifCxT> Right >Element)
T
SinglRolabe eiRight CT),;
else
Te Double RotarkithRightCT);

e x is in the bree aeady


T- Height Max(Heigbt CT-Lest), HeightCT->Right))+1
'

return T

/Roubine to
pexform ingle otahion
stabie Hsition Single Rotate hi thleft (Posihon la2)

Position k1;
Kl k2 Left;

N2-left k1->Risht
KIRight =ka
CO
CLEG
EKALAMAVUR
kaHeight =MaxC Height CkRLeft), +1; ooW
a Right))
HesahtC )+
k2> +eight
kl>Herght Max CHeig ht Ck1->left),
rekan kl;

to perfovm
Double Rotaion .
/Roubine
CRsihon k3)
Double Rotate Withleft
staie Pasikion **

(k3 Lept)j
ks->lef t SinglaRotath WitRight
Teturn SingleRoakei thLeftCk3),
3
TREE RAVERSALs ,

Yavessing means wisibina


each node on oo nce
ony nce

Travessing all tBe


hodes

sa method for visiking


Yee Trayergal
0se
ree
e e tpes
types of
05
once. There
n he
the tree exaäty
bvee braversa
tahniqu, narmel
Inovder Taver8al

2. Preovdes saversal

3. Postorder raversak
TNoRDER TRAYERSAL
inorder
Re lep subbree in
lraverse

*Visit the oot


*raverse theight subtree.

Example 20, 30
riovder l o , :

(30)
lsaversal.
oubine for Tnovder

Void Inorder CTree T

fCT NoLL)

In order T> left),


Print ElementCT>Element),
InorderCT rght);

REORDER RAVERSAL
Visit the Dot
lsaverse he e subtree
Kaverse the ight Bubbree.
Exam plei

reorder : 20, 1030.


3(90
Roubine for PreorderTaversal
Void Preovder (Tree
T).
fCT! = NuLL)

Pint Element(T>Elament)
Preovder CT>Les#);
Preorder CTright);

roSTORDER RAVERSAL
Tayevse he Subree
Isavevse he ight subbree
Visit the oot
xample
co

KALAMAVUR
Fastorder l0, 30, 20.
(o
Fov PRstovder Taversal
Routine

Void Pastorder (Tee T


iCT NoLL)
RasorderCT> ast);
Posovrder T->Right);
PrintEment T>Elenment)

HABHINGL35,36
Somne
The hash tabke daka skruchure (8 an avray of
assosiated*
key is a value
xed size, conrainoa the keys.A
with each TeLord
Locahen

q2
43.,

85

Hashing Funchon
addvess
hunahion is akey-bo-
A hashing to compue
angormation, which aeks upon a give key
Dhe Yelative pogition o h e key in an aTray
he
Advantages
BE doesnt uires pointers

Disadvantag es he perr
It forms clusles, which degades
shoring and
relievi
sormone
vebrievioo
of the hash table for a dal,
TRIORITY UEUES
MoDE L
is a special kind o
pionty e u
data strueture which wil have precedence over jobg

Ba sie operahion8 pertormed by prony quaue ove;

Insert. Opesation
*DeleteMin Operahon
Tnsertlt
Deleleflin) Pionhy uu.H
) Tnsert operahon i6 similar to Enaunue where we
Ingestan element in the priomH. aua
(&)Dele temin operation is ecuirlant:of aus yeay
operako, where we delatz at minimum cume
rom the prionhy uua.
SinmPLE LmPLEMENTATION
There are
sevexal ivay9 for implementing
pnonty qu
inked ist
8 Binary Seayeh
tree
3. Bina heap.
Advantages
EL doesnt uires pointers
Disadvantag
* I forms
es clusBevs, dhich degvades the perfora

hagh table for and


shoring xelievi og
ofthe dal
TRIORITY UEUES.
MoDE L
A ueue is a specíal Kind ofae
prionty
data strueture which wi have preaedence over jobs

operahion8 petormed by piony quaue


ore:
Basic

*Insert Opesation
Deletein Operahion
:i

Delelefin() Pionhy Quue.4 k Tnseitl


we
Insert operahion is Aimilar to Emuuu, where

Inse an element in theprion. 4uaua


(a)Deletemin operation i6 eairlànt of auuis
'

operaions, whee we deletz at minim um ekmen


om the prionhy uu.

SimPLE TmPLEMENTATION
Ihere are several way9 or
implementing
pnonty quu
Linked st
Binany Seareh tree
3. Binany heap
inked List COLLE
This implamentation of prion hy auau KALAMAYUR

Hon
vequnes OlD ime pecom the inger
o
minímum
at the font and oCN) hime to dalti a

e lement

Seavch Tree
Binam THSimplsmentahion lves an
avesage unnfnq

ingestion and deletemin


time
of oCs loa N) for bot6
Operahions

BiNAR:Y HEP
ofimplementhing piony
prion t
The 'ettijent woy
is merey Yeferred
refered
Binar heap
ueue 1 Binqry Heap.
have two ppor hes .namely
as heapg,heap
Struekure propet
*Heapovder ppperty
Stuekure opes
A heap
A heaP be complute binasu
should

ree, ohieh is a Compete d binany tree


he botom vel,
with the pos sible exeephon of
whieh is 4i ld fwm t to right
.9 has:
A eompute binaqee of
height
wetuoeen aand -|nodes
element in avay poai tion i
Fot
an
an:
in t6e posikon at
the et chitd
child is in position +
the right
the parent
is io i.
12

Comple bindoy Tuu Not a complat b


Heap Osdes Koperty

na hea p, for node X,


evey Hhe key in
in
he parent of xis waler than (or equal bo) he
'

ey In x,
This prbperty alloas the delekemin operahio
fo be
pexormed quichly hase minimum ekament
cn always be found at h e Dat Ih«s, e q e t the

h'nd Min opesahon in constant time.

EXample

(16)
(5
Binany tree wth
Bin reanriM stmuetere
Structure. and beap
butioLahing. heap orday
Order propery Propexty
Declasa ion for Pionh Gus *|| (14
Struct Heapatruet cOL

KALAMAVUR
int capacits 9

int síe,
int *Ements

/Tni ialization
uua Tnitialize Cint Max Elaments)
Priorb
H
H;
Pariom tq u
E Heapsbuet ) ;
= m a l l o c (sizeot
CStrnct
HCapaciby =slaElements,

Hsie0;
HelementsLo] =fioData

return H;

BAsie HEnP OPERATtONS

INSERT OPERATION
we
heap,
element X into he o cahion, ecioise
oer toise
insest an o
To available
9o cahon,
the next
a hole in
Cxeate
be com plebe
com plete
not violating
the tree wil
in t e
hole oihviolotin
ith
be placed
there itselp
XCan
*Tf placehe
eement X
orde,
hen
he
h e elament
elmént that is
hat in the
is in
eap we
ide
Dtherwise,
hole, thus peraolaig up
node
ino the unil
u X e
nil x ean
an
parent podess Conttnuas
C ontinuos
hoies
This prodes
ot.
owad he
thie
towad hole In whieh
i n he Percoati up,
be pRaCad
as
a s
be 8is Knaon
strateqy
This percolated
he heap
element
is
he ne
iNOu Cine Lngert Tnto A Binamy Heap.
Void ingertCint x, iovity Quauu #).
int i
14 CTsuRLCH

Esror C priorty qua isus!"),


return
for Ci++size; H>Elenents L/2] >X; t/#2)
/*T4 the poent Value is qreate han X, the
pace the element of parent noda into the hol
hole

HElements DJ =HElementsDi/27,
HElmenis[r] =X, {/othesun1se, Placa it în
Re hole

Example

(28 To Insert 1o

A hole is Created at the nex lo catim.


.

A (

Pexcolate the hole up o Percola he hole up


6atigfy heap order. to &aio haap ovder
15COL
KALAMAVUR

(16 OW

in ite eurent lcahion .

he
ne
VaJue 1o is pkaced
Delebe Min Opexaon a
minímum
eement
fhe
*Delete Mín Opesaion is delehing
From the Heap
1
*n biínory heap he minirmum demen i« ound
,
the rootNhen hig. minimum is xem @re d, a
hole

bome
Crealadat the voot Sinee he heap
X in he beap
Onesmaller, makes the last elemet
to move Someohere in the heapP
Can be placed in hole without violahing haaperdes
XX
propert ploce it
we slido. the mall of he holes
*Otherwise, one

thus pushing the holo. dou


hole,
childven into he
Leve Binary Heap.
Delebehin in a

Rvoutine To Pextovm
int Deletemin(Priorib@u H)

int i, child,
Last Element,
MinElument,
int
igcTaEmpty 4)
qua
s Emphy
Eor Prionky
veturn HElements[o1;
' rhiuttiLE H2EXemet 3L,
LastElemet= H-Elamests[H>size j

or(l; i*a l=H>size; ie cdhild)

Find Smalle Child *


child =i*a,
FCchild = H6ize kk H>Elament3 child +1J
HElenentsLehild
child ++
Percolate One evel doon

CLas&Elam ertH->ElumenksLchil4)
.HElementsCi= HElenentsTchild)
else
bsea ki,
HElementei J= LastElament
veurn MioElement

To delete the minimun elaman 10


Example
o ) - i n Elument .

15

(28
Dekbe Mninum Elument lo, cseatas. hol
atthe wot.

komewhere
The aet element o mugt be moved
in the haapP
holes mallst into h e
he children 12 i placed

nole by pus hin the hole douon One leve

2 reat
hole.
i6 placed in the
The ast element O

(2D

THER HeaP OPERATIONS


a
arre
e
operations
The o ther heap
Ci Deerease -key
Gi Tnerease -Key
CiR) Delete

0 Build Heap

DecREASE - KEy opevation deevease


deaveases

H) pevation
P,A,
Deeveasekey
The a
oBi Hon by
o f h e key
aaF po
value
violate 1e heap
the heaP
he may
may
violate

A m o u n n t
.A. This
positve
Loheh Can be fixed by
Popesy,
o ther

pereolat up
(O
5
12

(o 30

Decrease key L, 7, H
Element a pasitron 2 is 5.Decrease that
2lemet

(30
(D (
Violates the Peraolau up
heap Osder pope
heap
Lnerease -Kiey_
Jncreas
The
The inCrease-key (P. A, H) opexation
F by a
he
the Yakue of the key at posihion
Posi ive amount . This may vio late heap

oTde propery, Lohfth Can be iked by pereola


oon
Example

20) (30
Inerease key C2, 4, H)
ECment at position2is5 Inereas e
hat element
(1o
/ 2) 20)

20

Violais the
excolald down
Poperty

Delete
5e
The Delete (P H) operation emoves

node at he heap hiss


the Poition P om
can be done by.
CI.Perfom he decxeae Ke operation

Deercaseke P, co, 4)
cii Pexfom Delekenin Operation
Deletemin CH)
H)
Exam ple Delete , c0,

in fnt
Ci Deereagin9 N

(12)
(30)
val at position 2
Decveasse the
to
uwhich is e
Nalue chan ges
The
east element
n heap
/
o 20 (30

is Satiafie
Heap popest
DeleteMin N

deheing he inimum eemént.


Ater
he Loast elemeot ill 4e hole. Then
occu

ceornge the
he heap i l Satishes the heap
oTder
Ppes
(-00 DelehTMin (a

10 (1n) --

(20
(20 (30 .

(10
30
(30

Build Heap.
The Build HeaplH) opevatíons takes
as input N keys and places fhem into an

empty heap by maintainin strueture propety


Order
and heap propety
B-Trees LM-way Searah Trea

beareh ree Bhat


B-Tree is a
popula
bree
osder M 8 a
is not binary A B-bree of
oith he jolowing stuetura poper ies
or has
The Toot iS eihera Deaf
children.
between 2 and M
have
*ALL
* nonleat nodes (except bhe oot)

between [M/,and M ehildren


*AUL uaves are a he Aame depth

AL doata axe
a ve skored a t h e Leaves.

Conboined in eoch inteior node are poiner


Pa Pm to he chiadren and value

K,,ka ,M- presenbing the


Ehe smalles E
P,Ps, PM
k found in e
6ubbrees . ,

might bee
respecively.
Some hese poin ters
e in Subtree
NULL For
even
evey node, au
keys
T are &malle han he keys
in in Subree P
and 30 On

A B - t r e e ofbrder 4 i s move Popularly


known as 2-3-4 byee and a B - e e of ovdar 3
Known
2-3 ree.
8 Knouon

22.)

(4158
16
17 934,52 21,41
8,1,12
Ex2:A Btze Oder t.

Searehing or an Elenent
Similar to that in binm

59 in the above
seareh trees. 1o
Bearah for
7oot node.
The To ot node
root nodee
he
Eree, egin a So taverse in the
has a value 45 5
he
sub-tree 0f
The right
aht sub- tree
.

and 63. Since


two key 4
TOO node. has
travense vight Bubtree O 49
41 5a <b3,
ubbree of b23. his s u b b r e has
r
Reft s
On findin he
Lhvee v a l s , 54, 59 and 61
hree
.

he Seareh is Auceess.jul.
VaRue 59,
element In a B trees
Insering =

a inseshons are done


In B ree,
valu inserted
Leyel. A he is
at he leaf
in Bree. using Bhe algoi thm ven beloo
Seareh he B ree o find he
node here he neus Key value
Leaf
&hould be 1nserted
I} he Reaf nodé is not Jul), that i8,
iContains leks than m-1
key values
he inbert the hew in he
element n
node. keeping he node
elements 9rde 9rdeved.
(1
T he Reaf node & Jull, (ie) he leaf
Naluss, hen
Dode aeady contauns m-1 key
Ca) insert value in Order into
he neo

the exishing set o ke tuoo


tnto
6)SplLE he node ak it& median

nodes
element pto i parents
median
Cc) puah he

he parenE node is already


node.
TI
full, hen Aplit the pavent node by
steps
follostn a he Bamee

O Insert &

1s 45

3
39,42
Sosted sdes 2),21,3,
Insert 39
medion
Push to ita
Paren bs noda)
Tnsert

8|18

3 4 s4
Deleting n element rom a BTree
There are bwo cases af deleion.

Case 1 A leaf node ha8 to be delated


Case 26 An in ternal nade has o be deleted.

You might also like