Md Seha
DS Asstgnment-4 2283|A9282
1) lhat is AVL tree 1Explatn fts opexatton with one
exannple !
AVL tYeeis a height -balanaed bnaly tree where a
balance factor balancea eoch node factor
subtYace
1s a diffexence between the hesght of the left
and the Yght Subtra FoY a node to be balancec
rt shaut
be
hey opeoati ons:
1 Insetton:- Adds a new node afteu insertions the
tree mmqht need vebal anc
2 Deletion :- Rernoves c nodeThe tree might need
Yebalancing atta a deletton
tree
3 Lookup:- peuforms a Standard binalysearch
Loolk up
EnsuYe the tree semains alanced
Rebalancrng
afte os eittone oY deletions
Rotation:- To natntarn balance, AVL trees puform
0tatron dutfng fnsutton and delettone
Single Right Rotat? on (LL Rotati on)
Sng le Lett Rotation (RR Rotatrorn)
Lett -Right Rotattorn ( LR
Right - Lett Rot ation (R L
2) Explatn the difference betuween Red Black Tee ard
Splay Tree
Red-BLack Tyee. SPLAY TREE
Balanctng methad It usee Balancng method : Tt
a Specttc set of rules fnduin Balonca ftselt by movtng
"Yed"and "black' nade
by
the recently accu ked
colorng to enure the tree elements to the root of
xematn balarced the tree utfng a proce&e
* The Yoot and leaves (NIL NOde) called' splaytng! ucbich inv
aYe black -Ve a Selre oe tree Yott
+ Red nodea cannot have Yed -ion
Cheldren (no tD redsfo a * Balance Maintenanoe i
Splaying not onty bring
Svay path from a node the acus led element tO
to ite descendant NIL Node the voot but also paticy
have the same no of blact
bal ance the trees thycugh
noda not as rrgili ty ay redblact
peatormance Guaronte tree
It ofteY, O(l0qn) time * pufornance Gucl, anta:
for fnseltion It provfdel amoYftt zed
complertty
deletren and loot up o(logn) time complex ity)
opaaBtont
3)Explatn Graph transvexSal methods ustng Suf table
Graph traver sal Te fexs to the procest of vtsrttng
each veltex a qraph
n a There. 0xe two Prmay meth
-ods at qraph travexsal Depth-rat Seavch(DEs) and
Breadtth frst Seouch
1-Oepth -frrst Seasch (DFs)
explovea as far as posstble olong cach branch
DES
behavtouY back track ing Tt inke explort ng a maze
the poth you haven't ta ken yet
and alwaye takrng
Alqorthm:
t Stat at a 3ele cted node
t Visit the node
each adjacent unvitited
foY
* Recuist vely pefor nm DEe
node
qraph ut th vartics labele A,8,
Exampl- constda a
CoD,E
A, Choose an edge Say AB vait B
1 vsit
v>rt D
2 frorm B,Cho0se an edge B0,
moYe connectiony Contine
3 formo if there were
back track to B)and then to
but Snce there orertt
E
y fyom Co you car visit
2Breadth-fyst Seauch (B)
BFL explore the nerghbout fret, befove novrng to
the next level nerghbours ItB like explorfng a
time
maze and visfting al paths one step at a
Alqorithm
* Start at a selected node
* VIsit the node and add ît to a qucue
whte the queue it not Smpty
4) wvite Heap Sort Alqorrthm and Sort the Lrst ot
numbeYS (20s (2,35,10, 80, 305 | 2,)
Aigotthm -
Heap sort (arr)
BtldMax tHeap (ar)
for f= length (arr) to 2
Swap orYCI3 wf th ayr CiT
heap-stze [avr] = heap-srze (ay]? 2
MaxHeapify (art)
End
> ButldMax Heap(arr)
BuldMox Heap (a)
heap-S12e (arr) = Lergth (axY)
length (ar)/2 to I
Max Heapffy (ars,i)
End
def heapify (arsnst)3
Largest =I
2* { t|
Y= 21+2
tt lkn and arr C <arr L;
largat
if Y< and arr [largest]s arYCrJ;
largest = Y
arYC] aY [larqst 3= arr[largat]sarI
heapity (ars,n,las get)
def heapSort (arri
nelen(ar)
for I n tange (n||2-, -,-);
heapity arY, hoi)
foY i in Yange cn-l,o-)'
arrCi9 arrLoJ- arrlol, axxn
heapify (arrsi,o)
arr =[20,12,35,
10,8,30,|, 29
heapSort (ar)
pnt (" Bortedarray es :) )arr)
Thrs Scrfpt wil Sort the aray
(r0,12, 35,l0,80, 30, (7,23 usrng teap Sort algotthm