Professional Documents
Culture Documents
a perent Tnie
Rcot A node whieh doesnt have
ig A
above tree Reot
BINARY TREE
Binary tree is a tvee in which no node Ci
Binory
have mote than
than two chiidren
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)
binavy tree
# N o d e s
and Hnodes In
the bottom . leve
between a
.
EPRE8s10N REE
EXpression ee 18 a
binany tree n tohteh
The leaf odes are operands and e interior nedes ar
raversal.
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
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
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
eturn NUL;
X is paced in Tef
is. greater tRe reob,
an Re roob u
unnh
h il
T'x subbyee
recursively
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
toards. ghe
l6>8 T 3avevse
(lo)
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
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
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
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
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
TRight);
Right Delete (T> Element,
Tmpeell T,
ifCT>lest -*NoL):
T T- Riqht;
'
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
-havirg ene
chYAd
After TDelebiin
Befove delebion
The pointer Cusrently pointing the nade 5 is noio
(3 8
the posibion of 5.
( Since he
posikion of 41#s He
Leaf node' delebg. immediateld,
he nde 55
Afler deletng
uwO
10
C5
6ubtrees
can differ by bree. i6
is defined. to
defin ed. to
be
be 1.
the emply ubree
The height of 1he left
subree
BF 0
aF-1
BF 0
Causes imbalane, when
ahy one
of he
ollocoing conditions occuT
SINGLE KoTATION
( Singte hwtabian to ix Case 4
Gteneral Representation
Exashpkg
Tnser
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
ka
(ke)
Before Rotahion
()
After Rrotation
KALAMAVUR) 9
armple
(8 (A
Apler Rotakon
CBalanced)
B efoxe Rotaion
CIm balanced)
AVL node
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;
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 ,
2. Preovdes saversal
3. Postorder raversak
TNoRDER TRAYERSAL
inorder
Re lep subbree in
lraverse
Example 20, 30
riovder l o , :
(30)
lsaversal.
oubine for Tnovder
fCT NoLL)
REORDER RAVERSAL
Visit the Dot
lsaverse he e subtree
Kaverse the ight Bubbree.
Exam plei
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
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
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
*Insert Opesation
Deletein Operahion
:i
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
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
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
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;
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
HElements DJ =HElementsDi/27,
HElmenis[r] =X, {/othesun1se, Placa it în
Re hole
Example
(28 To Insert 1o
A (
(16 OW
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
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
CLas&Elam ertH->ElumenksLchil4)
.HElementsCi= HElenentsTchild)
else
bsea ki,
HElementei J= LastElament
veurn MioElement
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
2 reat
hole.
i6 placed in the
The ast element O
(2D
0 Build Heap
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
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
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
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
AL doata axe
a ve skored a t h e Leaves.
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
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
.
he Seareh is Auceess.jul.
VaRue 59,
element In a B trees
Insering =
nodes
element pto i parents
median
Cc) puah he
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.