programs algo
TRANSCRIPT
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 1/56
Q1)Write a program to sort the array element using
1) Merge Sort2) Bubble Sort
3) Insertion Sort
4) Selection SortDraw the line graph or time comple!ity"
BUBBLE SORT
#inclu$e%iostream&#inclu$e%st$io"h&
#inclu$e%st$lib"h&
#inclu$e%time"h&
using namespace st$'
(*uthor+*,ay -a, Sri.asta.a 1411/4Bubble Sort 0n2)(
.oi$ swapint !p int yp)
int temp !p' !p yp'
yp temp'
5.oi$ bubbleSortint arr67 int n)
int i ,'
or i 8' i % n+1' i99)
or , 8' , % n+i+1' ,99) ((:ast i elements are alrea$y in place i arr6,7 & arr6,917)
swap;arr6,7 ;arr6,917)'5
int main)
cloc<=t starten$'
int n'
cout%%>?nter si@e o array>%%en$l'cin&&n'
int a6n7'
orint i8'i%n'i99)
a6i7ran$)An'
cout%%>Beore Bubble Sort+>%%en$l'
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 2/56
((
orint i8'i%n'i99)cout%%a6i7%%> >'
cout%%en$l'
startcloc<)'bubbleSortan)'
en$cloc<)'
cout%%>*ter Bubble Sort+>%%en$l'
orint i8'i%n'i99)cout%%a6i7%%> >'
cout%%en$l'
loat $i loat)en$+loat)start)'
loat secon$s $i ( C:0CS=E?-=S?C'print>FnGime ta<en A>secon$s)' ((C:=GC
return 8'
5
MERGE SORT
((*uthor+ *,ay -a, Sri.asta.a 1411/4
#inclu$e%st$io"h&#inclu$e%st$lib"h&
#inclu$e%iostream&
using namespace st$'
(( Hunction to Merge *rrays : an$ - into *"(( leCount number o elements in :
(( rightCount number o elements in -".oi$ Mergeint *int :int letCountint -int rightCount)
int i,<'
(( i + to mar< the in$e! o let aubarray :)
(( , + to mar< the in$e! o right sub+raay -)(( < + to mar< the in$e! o merge$ subarray *)
i 8' , 8' < 8'
whilei%letCount ;; ,% rightCount)
i:6i7 % -6,7) *6<997 :6i997'else *6<997 -6,997'
5
whilei % letCount) *6<997 :6i997'while, % rightCount) *6<997 -6,997'
5
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 4/56
loat $i loat)en$+loat)start)'
loat secon$s $i ( C:0CS=E?-=S?C'print>FnGime ta<en A>secon$s)'((C:=GC
return 8'5
SELECTION SORT
((*uthor+*,ay -a, Sri.asta.a 1411/4
#inclu$e%iostream&#inclu$e%st$io"h&
#inclu$e%st$lib"h&
#inclu$e%time"h&
using namespace st$'.oi$ swapint !p int yp)
int temp !p'
!p yp'
yp temp'5
.oi$ selectionSortint arr67 int n)
int i , min=i$!'
(( 0ne by one mo.e boun$ary o unsorte$ subarray or i 8' i % n+1' i99)
(( Hin$ the minimum element in unsorte$ array min=i$! i'
or , i91' , % n' ,99) i arr6,7 % arr6min=i$!7)
min=i$! ,'
(( Swap the oun$ minimum element with the irst element
swap;arr6min=i$!7 ;arr6i7)' 5
5
int main)
cloc<=t starten$'
int n'
cout%%>?nter si@e o array>%%en$l'cin&&n'
int a6n7'
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 5/56
orint i8'i%n'i99)a6i7ran$)An'
cout%%>Beore Selection Sort+>%%en$l'((
orint i8'i%n'i99)cout%%a6i7%%> >'
cout%%en$l'
startcloc<)'selectionSortan)'
en$cloc<)'
cout%%>*ter Selection Sort+>%%en$l'
orint i8'i%n'i99)
cout%%a6i7%%> >'
cout%%en$l'
loat $i loat)en$+loat)start)'loat secon$s $i ( C:0CS=E?-=S?C'
print>FnGime ta<en A>secon$s)'((C:=GC
return 8'
5
INSERTION SORT
((*uthor+*,ay -a, Sri.asta.a
#inclu$e%iostream&#inclu$e%st$lib"h&
#inclu$e%time"h&#inclu$e%st$io"h&
using namespace st$'
.oi$ insertionSortint arr67 int n)
int i <ey ,'
or i 1' i % n' i99)
<ey arr6i7' , i+1'
( Mo.e elements o arr68""i+17 that are greater than <ey to one position ahea$
o their current position (
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 6/56
while , & 8 ;; arr6,7 & <ey)
arr6,917 arr6,7'
, ,+1'
5 arr6,917 <ey'
55
int main)
cloc<=t starten$'
int n'
cout%%>?nter si@e o array>%%en$l'cin&&n'
int a6n7'
orint i8'i%n'i99)
a6i7ran$)An'
cout%%>Beore Insertion Sort+>%%en$l'((
orint i8'i%n'i99)
cout%%a6i7%%> >'
cout%%en$l'
startcloc<)'
insertionSortan)'en$cloc<)'
cout%%>*ter Insertion Sort+>%%en$l'
orint i8'i%n'i99)
cout%%a6i7%%> >'
cout%%en$l'
loat $i loat)en$+loat)start)'loat secon$s $i ( C:0CS=E?-=S?C'print>FnGime ta<en A>secon$s)'((C:=GC
return 8'
5
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 7/56
Q2)Write a program to sort the array element using
1) Counting Sort 2) Bucket Sort
COUNT SORT
((*uthor+*,ay -a, Sri.asta.a 1411/4
#inclu$e%iostream&
using namespace st$'int main)
int nma!+1'((si@e
cout%%>?nter si@e o array>%%en$l'
cin&&n' int a6n7b6n917'
cout%%>?nter elements or array>%%en$l'
orint i8'i%n'i99) cin&&a6i7'
ia6i7&ma!) ma!a6i7'((range+
5 ((counting sort
int c6ma!917' orint i8'i%ma!91'i99)
c6i78'
orint i8'i%n'i99)
c6a6i7799'
orint i1'i%ma!91'i99)c6i79c6i+17'
orint in+1'i&8'i++) b6c6a6i777a6i7'
c6a6i77++'5
((sorte$ arraycout%%>Sorte$ array is>%%en$l'orint i1'i%n91'i99)
cout%%b6i7%%> >'cout%%en$l'
5
BUCKET SORT
((*uthor+*,ay -a, Sri.asta.a 1411/4#inclu$e%iostream&
#inclu$e%math"h&
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 8/56
#inclu$e%.ector&
#inclu$e%algorithm&using namespace st$'
int main)
int n'
cout%%>?nter si@e o array>%%en$l' cin&&n'
loat a6n917'
cout%%>?nter elements or arrays>%%en$l'
orint i1'i%n91'i99) cin&&a6i7'
.ector% .ector%loat& &.n)'((.ector o .ector%int& o si@e n'
orint i1'i%n91'i99)
.6loorna6i7)7"push=bac<a6i7)'
or int i8' i%n' i99) sort.6i7"begin).6i7"en$))'
int in$e! 1'
or int i 8' i % n' i99)
or int , 8' , % .6i7"si@e)' ,99) a6in$e!997 .6i76,7'
orint i1'i%n91'i99)
cout%%a6i7%%> >'
cout%%en$l'
return 8'5
Q2-1. Given an array S of unsorte elements. !esign an
algorithm an implement that to fin a pair "# y suchthat "$y from S that minimi%es &"-y&. 'he (orst case
running time of the algorithm shoul e * +nlgn).
((*uthor+*,ay -a, Sri.asta.a 1411/4
#inclu$e%iostream&#inclu$e%time"h&
#inclu$e%set&
#inclu$e%st$lib"h&#inclu$e%.ector&
using namespace st$'
.oi$ Mergeint *int :int letCountint -int rightCount) int i,<'
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 9/56
i 8' , 8' < 8'
whilei%letCount ;; ,% rightCount)
i:6i7 % -6,7) *6<997 :6i997'else *6<997 -6,997'
5whilei % letCount) *6<997 :6i997'
while, % rightCount) *6<997 -6,997'
5
(( -ecursi.e unction to sort an array o integers".oi$ MergeSortint *int n)
int mi$i : -'
in % 2) return' (( base con$ition
mi$ n(2'
: int)mallocmi$si@eoint))'- int)mallocn+ mi$)si@eoint))'
ori 8'i%mi$'i99) :6i7 *6i7'
ori mi$'i%n'i99) -6i+mi$7 *6i7'
MergeSort:mi$)'
MergeSort-n+mi$)'Merge*:mi$-n+mi$)'
ree:)'ree-)'
5
int main)
int n'
cout%%>?nter si@e o array>%%en$l'
cin&&n'
int a6n7'
orint i8'i%n'i99)a6i7ran$)An'((generates ran$om number can be same also
cout%%>-an$omnly generate$ array>%%en$l'
orint i8'i%n'i99)
cout%%a6i7%%> >'cout%%en$l%%en$l'
MergeSortan)'
.ector%int&$i'
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 10/56
orint i1'i%n+1'i99)
ia6i7a6i+17)
$i"push=bac<a6i7+a6i+17)' ((calculating $ierence
5
int min18888888'
orint i8'i%$i"si@e)'i99) ((min $ierence
i$i6i7%min)min$i6i7'
5
orint i1'i%n+1'i99)
ia6i7a6i+17 ;; a6i7+a6i+17min)
cout%%>pairs are >%%a6i7%%> an$ >%%a6i+17%%en$l'brea<'5
5
system>pause>)'
$i"clear)'
return 8'
5
Q2-2). Given t(o arrays , 1 # , 2 of si%e n an a numer "#esign an algorithm to fin (hether there e"ists a pair
of elements one from , 1 an other from , 2 (hose sumis eual to ". ,lso fin the inices of those elements.
((*uthor+*,ay -a, Sri.asta.a 1411/4#inclu$e%iostream&
#inclu$e%st$lib"h&
using namespace st$'int main)
cout%%>?nter si@es o arrays> %%en$l'int n'
cin&&n'
int a16n7a26n7'
cout%%>?nter $ata or array 1>%%en$l'orint i8'i%n'i99)
cin&&a16i7'
cout%%>?nter $ata or array 2>%%en$l'
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 11/56
orint i8'i%n'i99)
cin&&a26i7'
int !'
cout%%>?nter .alue o !>%%en$l'cin&&!'
int lag+1'
orint i8'i%n'i99)
orint ,8',%n',99)
ia16i79a26,7!)
lag1'
cout%%>Jeswith i1>%%i%%>an$ i2>%%,%%en$l'5
5
5
ilag+1)cout%%>Ko>%%en$l'
system>pause>)'
return 8'5
Q2-. !evelop an algorithm to solve the ma"imum
segment sum prolem.
#inclu$e%iostream&using namespace st$'
int main)
((*uthor+*,ay -a, Sri.asta.a 1411/4
((a$aneLs *lgorithm use$ or ma!imum segment sumint ma!=so=ar8ma!=en$ing=here8'
int n'cout%%>?nter si@e o array>%%en$l'cin&&n'
int a6n7'cout%%>?nter elements or array>%%en$l'
orint i8'i%n'i99)
cin&&a6i7'
orint i8'i%n'i99)
ma!=en$ing=here9a6i7'
ima!=en$ing=here%8) ma!=en$ing=here8'
ima!=so=ar%ma!=en$ing=here)
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 12/56
ma!=so=arma!=en$ing=here'
5
cout%%>Ma!imum segment sum >%%ma!=so=ar%%en$l'
return 8'5
Q-1)Buil a /a"-0eap 'ree for any array. ,naly%e theoperation in terms of running time an fin the time
comple"ity of the algorithm.
((*uthor+*,ay -a, Sri.asta.a 1411/4#inclu$e%iostream&
using namespace st$'
.oi$ ma!=heapiyint aint iint n)
int largesttemp'
int l2i' int rl91'
il%n ;; a6l7&a6i7)
largestl' else
largesti' ir%n ;; a6r7&a6largest7)
largestr'
ilargesti)
tempa6i7' a6i7a6largest7'
a6largest7temp'
ma!=heapiyalargestn)' 5
5
int main) int ni'
cout%%>?nter si@e o array>%%en$l' cin&&n'
cout%%>?nter elements o array>%%en$l'
int a6n917' ori1'i%n91'i99 )
cin&&a6i7'
cout%%>Ma! heapiy+>
orin(2'i&1'i++) ma!=heapiyain)'
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 13/56
ori1'i%n91'i99 )
cout%%a6i7%%> >' cout%%en$l'
return 8'
5
Q-2). mplement an algorithm to uil a /in-0eap 'reefor any array. ,naly%e the operation in terms of
running time an fin the time comple"ity of the
algorithm.
((*uthor+*,ay -a, Sri.asta.a 1411/4!#inclu$e%iostream&
using namespace st$'
.oi$ min=heapiyint aint iint n)
int smallesttemp' int l2i'
int rl91' il%n ;; a6l7%a6i7)
smallestl' else
smallesti'
ir%n ;; a6r7%a6smallest7) smallestr'
ismallesti)
tempa6i7'
a6i7a6smallest7' a6smallest7temp'
min=heapiyasmallestn)' 5
5int main)
int ni'
cout%%>?nter si@e o array>%%en$l' cin&&n' cout%%>?nter elements o array>%%en$l'
int a6n917' ori1'i%n91'i99 )
cin&&a6i7'
cout%%>Min heapiy+>%%en$l'
orin(2'i&1'i++) min=heapiyain)'
ori1'i%n91'i99 ) cout%%a6i7%%> >'
cout%%en$l'
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 14/56
return 8'
5
Q-)Q2. !esign an implement an algorithm to fin the th
largest elements of an unsorte array in *+n3klgn)time.
((*uthor+*,ay -a, Sri.asta.a 1411/4
#inclu$e%iostream&
using namespace st$'.oi$ ma!=heapiyint aint iint n)
int largesttemp'
int l2i'
int rl91' il%n ;; a6l7&a6i7)
largestl'
else largesti'
ir%n ;; a6r7&a6largest7) largestr'
ilargesti)
tempa6i7'
a6i7a6largest7' a6largest7temp'
ma!=heapiyalargestn)' 5
5
.oi$ buil$=heapint aint n)
int i'
orin(2'i&1'i++) ma!=heapiyain)'
5
int e!tract<thint aint <int n)int <th'
while<++) <tha617'
a617+1'
buil$=heapan)' 5
return <th'
5
int main)
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 15/56
int ni<'
cout%%>?nter si@e o array>%%en$l' cin&&n'
cout%%>?nter elements o array>%%en$l'
int a6n917' ori1'i%n91'i99 )
cin&&a6i7'
buil$=heapan)'
cout%%>?nter < to in$ <th largest element in gi.en unsorte$ array>%%en$l' cin&&<'
cout%%e!tract<tha<n)%%en$l'
return 8'
5
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 16/56
LAB EXERCISE-4
Implement an algorithm of Max-Priority-Queue for any array of elements havingINSERT, EXTR!T-MX, IN!RESE-"E# operations$ naly%e its operations interms of running time an& 'n& the time (omplexity of the algorithm$Algorithm-MX-)EP-INSERT*, +ey, n
$ heap-si%e./ 0 n 1 2$ .n 1 / 0 -3$ )EP-IN!RESE-"E#*, n 1 , +ey
)EP-IN!RESE-"E#*, i, +ey$ if +ey 4 .i/2$ then error 5ne6 +ey is smaller than (urrent +ey73$ .i/ 0 +ey8$ while i 9 an& .PRENT*i/ 4 .i/:$ do ex(hange .i/ ; .PRENT*i/<$ i 0 PRENT*i
)EP-EXTR!T-MX*, n$ if n 4 2$ then error 5heap un&er=o67
3$ max 0 ./8$ ./ 0 .n/:$ MX-)EPI>#*, , n- rema+es heap<$ return max
MX-)EPI>#*, i, n$ l 0 ?E>T*i2$ r 0 RI@)T*i3$ if l A n an& .l/ 9 .i/8$ then largest 0l:$ else largest 0i<$ if r A n an& .r/ 9 .largest/B$ then largest 0rC$ if largest i
D$ then ex(hange .i/ ; .largest/$ MX-)EPI>#*, largest, n
Program-Fin(lu&e4iostream9Fin(lu&e4st&liG$h9Fin(lu&e4math$h9using namespa(e st&Hint arr./Hvoi& input*int n
int Ja K arrHfor*int i K H i 4 nH i11
J*a1i K ran&*H LLint left*int i
return *2Ji1HLint right*int i
return *2Ji12HLint parent*int i
return *i-2HLvoi& maxheapify*int Ja, int i, int n
int l, r, largest, tempH l K left*iH
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 17/56
r K right*iH if* * l4n OO * J*a1l 9 J*a1i largest K lH L else largest K iH
L if* * r4n OO * J*a1r 9 J*a1largest largest K rH L if* largest K i temp K J*a1iH J*a1i K J*a1largestH J*a1largest K tempH maxheapify*a, largest, nH LLvoi& &isplay*int Ja, int n
for*int i K H i 4 nH i11 (out44*J*a1i44 H L (out44en&lHLvoi& Guil&maxheap*int Ja, int n
for*int i K =oor**n-2H i 9K H i-- maxheapify*a, i, nH LLvoi& heapsort*int Ja, int n int i, tempH Guil&maxheap*a, nH for *i K n-H i 9 H i-- temp K JaH Ja K J*a1iH J*a1i K tempH maxheapify*a, , iH LLint heapmaximum*int Ja, int n
if*n KK return -H L else return J*a1H LLint heapextra(tmax*int Ja, int Jn
int maxHif*nKK
return -H
L else Guil&maxheap*a,JnH
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 18/56
max K JaH Ja K J*a1*Jn-H *Jn--H maxheapify*a, , JnH L return maxHLvoi& heapin(rease+ey*int Ja, int i, int +ey
int tempHi--H
if*+ey 4 J*a1i (out44nNe6 +ey is smaller than (urrent +eyH goto xH L J*a1i K +eyH 6hile*i9 OO J*a1parent*i 4 J*a1i
temp K J*a1parent*iH J*a1parent*i K J*a1iH J*a1i K tempH
i K parent*i H L x
HLvoi& maxheapinsert*int Ja, int +ey, int Jn *Jn11H J*a1*Jn- K -H heapin(rease+ey*a,Jn,+eyHLint main*
int Jarr K arr, si%e, (hoi(e, max, in&ex, ne6+eyH
&o (out44n$ !reate )eapn2$ Maximum )eapifyn3$ uil& Maximum )eapn8$ )eap Sortn:$ )eapExtra(t Maximumn<$ )eap MaximumnB$ )eap In(rease "eynC$ Maximum )eap InsertnD$ ExitnH (out44nENter your (hoi(e H (in99(hoi(eH s6it(h*(hoi(e (ase (out44nEnter si%e of heap to (reate H
(in99si%eH input*si%eH (out44n)eap (reate&$nH &isplay*arr,si%eH (out44en&lH Grea+H L (ase 2 (out44nEnter in&ex of element to perform Max )eapify on H (in99in&exH maxheapify*arr,in&ex,si%eH (out44nfter Maximum )eapify H &isplay*arr, si%eH Grea+H L (ase 3
Guil&maxheap*arr, si%eH
(out44nfter uil& Maximum )eap H &isplay*arr, si%eH
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 19/56
Grea+H L (ase 8 heapsort*arr, si%eH (out44nfter )eap Sort H &isplay*arr, si%eH Grea+H
L (ase : max K heapextra(tmax*arr, Osi%eH (out44nMaximum element from heap 44max44en&lH (out44fter )eap Extra(t Maximum, ne6 )eap H &isplay*arr, si%eH Grea+H L (ase < max K heapmaximum*arr, si%eH (out44nMaximum element from heap 44max44en&lH Grea+H
L (ase B (out44nEnter in&ex of element to in(rese +ey H (in99in&exH (out44Enter ne6 value of +ey H (in99ne6+eyH heapin(rease+ey*arr, in&ex, ne6+eyH (out44nNe6 heap after (hanges H &isplay*arr, si%eH Grea+H L (ase C (out44nEnter the value of ne6 +ey to insert H (in99ne6+eyH maxheapinsert*arr, ne6+ey, Osi%eH (out44nNe6 heap after a&&ing a ne6 +ey H &isplay*arr, si%eH Grea+H L (ase D exit*H L
&efault (out44nEnter vali& (hoi(e H Grea+H L L L6hile*(hoi(e K DH return HL
Q$ Implement an algorithm of Min-Priority-Queue for any array of elementshaving INSERT, EXTR!TMIN, UE!RESE-"E# operations$ naly%e its operationsin terms of running time an& 'n& the time (omplexity of the algorithm$Algorithm-MIN-)EP-INSERT*, +ey, n
$ heap-si%e./ 0 n 1 2$ .n 1 / 0 -3$ )EP-UE!RESE-"E#*, n 1 , +ey
)EP-UE!RESE-"E#*, i, +ey
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 20/56
$ if +ey 9.i/2$ then error 5ne6 +ey is larger than (urrent +ey73$ .i/ 0 +ey8$ while i 9 an& .PRENT*i/ 9 .i/:$ do ex(hange .i/ ; .PRENT*i/<$ i 0 PRENT*i
)EP-EXTR!T-MIN*, n
$ if n 4 2$ then error 5heap un&er=o673$ min 0 .s/8$ .s/ 0 .n/:$ MIN-)EPI>#*, , n- rema+es heap<$ return min
MIN_HEAPIFY(A,i)
{
l=LEFT(i)r=RIGHT(i)
if((l<=A.heap_size)AN(A!l"<A!i"))
s$all=l
else
s$all=i
if((r<=A.heapsize)%%(A!s$all"&A!r")) s$all=r
if(s$all'=i)
{
eha*+e A!i" i-h A!s$all"
MIN_HEAPIFY(A,i)
Program-Fin(lu&e4iostream9Fin(lu&e4st&liG$h9Fin(lu&e4math$h9using namespa(e st&Hint arr./H
voi& input*int n
int Ja K arrHfor*int i K H i 4 nH i11
J*a1i K ran&*H LLint left*int i
return *2Ji1HLint right*int i
return *2Ji12HLint parent*int i
return *i-2HLvoi& minheapify*int Ja, int i, int n
int l, r, smallest, tempH l K left*iH r K right*iH if* * l4n OO * J*a1l 4 J*a1i smallest K lH
L else smallest K iH
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 21/56
L if* * r4n OO * J*a1r 4 J*a1smallest smallest K rH L if* smallest K i temp K J*a1iH
J*a1i K J*a1smallestH J*a1smallest K tempH minheapify*a, smallest, nH LLvoi& Guil&minheap*int Ja, int n
for*int i K =oor**n-2H i 9K H i-- minheapify*a, i, nH LLvoi& heapsortmin*int Ja, int n
int i, tempH Guil&minheap*a, nH for *i K n-H i 9 H i-- temp K JaH Ja K J*a1iH J*a1i K tempH minheapify*a, , iH LLvoi& &isplay*int Ja, int n
for*int i K H i 4 nH i11 (out44*J*a1i44 H L (out44en&lHLint heapminimum*int Ja, int n
if*n KK return -H L else return J*a1H LLint heapextra(tmin*int Ja, int Jn
int minHif*nKK
return -H L else Guil&minheap*a,JnH min K JaH Ja K J*a1*Jn-H *Jn--H minheapify*a, , JnH
L return minHLvoi& heap&e(rease+ey*int Ja, int i, int +ey
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 22/56
int tempHi--H
if*+ey 9 J*a1i (out44nNe6 +ey is greater than (urrent +eyH goto xH L
J*a1i K +eyH 6hile*i9 OO J*a1parent*i 9 J*a1i
temp K J*a1parent*iH J*a1parent*i K J*a1iH J*a1i K tempH i K parent*i H L x
HLvoi& minheapinsert*int Ja, int +ey, int Jn *Jn11H
J*a1*Jn- K -H heap&e(rease+ey*a,Jn,+eyHLint main*
int Jarr K arr, si%e, (hoi(e, min, in&ex, ne6+eyH
&o (out44n$ !reate )eapn2$ Minimum )eapifyn3$ uil& Minimum )eapn8$ )eap Sortn:$ )eapExtra(t Minimumn<$ )eap MinimumnB$ )eap Ue(rease "eynC$ Minimum )eap InsertnD$ ExitnH (out44nEnter your (hoi(e H (in99(hoi(eH s6it(h*(hoi(e (ase (out44nEnter si%e of heap to (reate H
(in99si%eH input*si%eH (out44n)eap (reate&$nH &isplay*arr,si%eH (out44en&lH Grea+H L (ase 2 (out44nEnter in&ex of element to perform Min )eapify on H (in99in&exH minheapify*arr,in&ex,si%eH (out44nfter Minimum )eapify H &isplay*arr, si%eH Grea+H L (ase 3 Guil&minheap*arr, si%eH
(out44nfter uil& Minimum )eap H &isplay*arr, si%eH Grea+H L (ase 8
heapsortmin*arr, si%eH (out44nfter )eap Sort H &isplay*arr, si%eH Grea+H
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 23/56
L (ase : min K heapextra(tmin*arr, Osi%eH (out44nMinimum element from heap 44min44en&lH (out44fter )eap Extra(t Minimum, ne6 )eap H &isplay*arr, si%eH Grea+H
L (ase < min K heapminimum*arr, si%eH (out44nMinimum element from heap 44min44en&lH Grea+H
L (ase B (out44nEnter in&ex of element to &e(rease +ey H (in99in&exH (out44Enter ne6 value of +ey H (in99ne6+eyH heap&e(rease+ey*arr, in&ex, ne6+eyH
(out44nNe6 heap after (hanges H &isplay*arr, si%eH Grea+H L (ase C (out44nEnter the value of ne6 +ey to insert H (in99ne6+eyH minheapinsert*arr, ne6+ey, Osi%eH (out44nNe6 heap after a&&ing a ne6 +ey H &isplay*arr, si%eH Grea+H L (ase D exit*H L &efault (out44nEnter vali& (hoi(e H Grea+H L L L6hile*(hoi(e K DH return HL
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 24/56
LAB EXERCISE-5
Vrite a menu &riven program sho6ing follo6ing operations in a Ginary sear(htree through$ Inor&er Traversal2$ Preor&er Traversal3$ Postor&er Traversal
* Insertion* Ueletion*! Sear(hingnaly%e the operation in terms of running time an& 'n& the time (omplexity ofthe algorithm$
Algorithm-INWRUER-TREE-V?"*x
B$ if x NI?
C$ then INWRUER-TREE-V?" * left .x/ D$ print +ey .x/$ INWRUER-TREE-V?" * right .x/
Preorder(root): Traverse a Ginary tree in no&e-left-right seuen(e$B$ If *root is not nullC$ pro(ess *rootD$ Preor&er *leftSuGtree$ Preor&er *rightSuGtree
Postorder(root): Traverse a Ginary tree in left-right-no&e seuen(e$$ If *root is not null2$ Postor&er *leftSuGtree3$ Postor&er *rightSuGtree
8$ pro(ess *root TREE-SER!)*x, +8$ if x K NI? or + K +ey .x/:$ then return x<$ if + 4 +ey .x/B$ then return TREE-SER!)*left .x/, +
else return TREE-SER!)*right .x/, +
TREE-INSERT(T, z)B$ y 0 NI?C$ x 0 root .T/
D$ while x Y NI?$ do y 0 x$ if +ey .%/ 4 +ey .x/2$ then x 0 left .x/3$ else x 0 right .x/8$ p.%/ 0 y:$ if y K NI?<$ then root .T/ 0 % Tree T 6as emptyB$ else if +ey .%/ 4 +ey .y/C$ then left .y/ 0 %
else right .y/ 0 % TREE-UE?ETE*T, %
B$ if left.%/ K NI? or right.%/ K NI?C$ then y 0 %
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 25/56
D$ else y 0 TREE-SZ!!ESSWR*%$ if left.y/ NI?
$ then x 0 left.y/2$ else x 0 right.y/3$ if x NI?
8$ then p.x/ 0 p.y/
:$ if p.y/ K NI?<$ then root.T/ 0 xB$ else if y K left.p.y//C$ then left.p.y// 0 xD$ else right.p.y// 0 x2$ if y %2$ then +ey.%/ 0 +ey.y/22$ (opy y[s satellite &ata into %23$ return y
Program-Fin(lu&e 4st&io$h9Fin(lu&e 4st&liG$h9stru(t Gtno&e int valueH stru(t Gtno&e JlH stru(t Gtno&e JrHLJroot K NZ??, Jtemp K NZ??, Jt2, JtHvoi& &elete*Hvoi& insert*Hvoi& &elete*Hvoi& inor&er*stru(t Gtno&e JtHvoi& (reate*Hvoi& sear(h*stru(t Gtno&e JtH
voi& preor&er*stru(t Gtno&e JtHvoi& postor&er*stru(t Gtno&e JtHvoi& sear(h*stru(t Gtno&e Jt,int &ataHint =ag K Hvoi& main* int (hH printf*nWPERTIWNS ---H printf*n - Insert an element into treenH printf*2 - Uelete an element from the treenH printf*3 - Inor&er TraversalnH printf*8 - Preor&er TraversalnH printf*: - Postor&er TraversalnH printf*< - ExitnH
6hile* printf*nEnter your (hoi(e H s(anf*\&, O(hH s6it(h *(h (ase
insert*H Grea+H (ase 2
&elete*H Grea+H (ase 3
inor&er*rootH
Grea+H (ase 8
preor&er*rootH Grea+H
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 26/56
(ase :postor&er*rootH
Grea+H (ase <
exit*H &efault
printf*Vrong (hoi(e, Please enter (orre(t (hoi(e H Grea+H
L LLJ To insert a no&e in the tree Jvoi& insert* (reate*H if *root KK NZ??
root K tempH else
sear(h*rootHLJ To (reate a no&e Jvoi& (reate*
int &ataH printf*Enter &ata of no&e to Ge inserte& H s(anf*\&, O&ataH temp K *stru(t Gtno&e Jmallo(*Jsi%eof*stru(t Gtno&eH temp-9value K &ataH temp-9l K temp-9r K NZ??HLJ >un(tion to sear(h the appropriate position to insert the ne6 no&e Jvoi& sear(h*stru(t Gtno&e Jt if **temp-9value 9 t-9value OO *t-9r K NZ?? J value more than root no&e value insert at right J sear(h*t-9rH else if **temp-9value 9 t-9value OO *t-9r KK NZ?? t-9r K tempH else if **temp-9value 4 t-9value OO *t-9l K NZ?? J value less than root no&e value insert at left J sear(h*t-9lH else if **temp-9value 4 t-9value OO *t-9l KK NZ?? t-9l K tempHLJ re(ursive fun(tion to perform inor&er traversal of tree Jvoi& inor&er*stru(t Gtno&e Jt if *root KK NZ?? printf*No elements in a tree to &isplayH returnH L if *t-9l K NZ??
inor&er*t-9lH printf*\& -9 , t-9valueH if *t-9r K NZ??
inor&er*t-9rHLJ To (he(+ for the &elete& no&e Jvoi& &elete* int &ataH if *root KK NZ?? printf*No elements in a tree to &eleteH returnH L
printf*Enter the &ata to Ge &elete& H s(anf*\&, O&ataH t K rootH t2 K rootH
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 27/56
sear(h*root, &ataHLJ To 'n& the preor&er traversal Jvoi& preor&er*stru(t Gtno&e Jt if *root KK NZ?? printf*No elements in a tree to &isplayH
returnH L printf*\& -9 , t-9valueH if *t-9l K NZ??
preor&er*t-9lH if *t-9r K NZ??
preor&er*t-9rHLJ To 'n& the postor&er traversal Jvoi& postor&er*stru(t Gtno&e Jt if *root KK NZ?? printf*No elements in a tree to &isplay H
returnH L if *t-9l K NZ??
postor&er*t-9lH if *t-9r K NZ??
postor&er*t-9rH printf*\& -9 , t-9valueHLvoi& sear(h*stru(t Gtno&e Jt, int &ata if **&ata9t-9value t K tH sear(h*t-9r, &ataH L else if **&ata 4 t-9value t K tH sear(h*t-9l, &ataH L else if **&ataKKt-9value &elete*tH LLJ To &elete a no&e Jvoi& &elete*stru(t Gtno&e Jt int +H
J To &elete leaf no&e J
if **t-9l KK NZ?? OO *t-9r KK NZ?? if *t-9l KK t t-9l K NZ??H L else t-9r K NZ??H L t K NZ??H free*tH returnH L J To &elete no&e having one left han& (hil& J else if **t-9r KK NZ??
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 28/56
if *t KK t root K t-9lH t K rootH L else if *t-9l KK t
t-9l K t-9lH L else t-9r K t-9lH L t K NZ??H free*tH returnH L J To &elete no&e having right han& (hil& J else if *t-9l KK NZ?? if *t KK t
root K t-9rH t K rootH L else if *t-9r KK t t-9r K t-9rH else t-9l K t-9rH t KK NZ??H free*tH returnH L J To &elete no&e having t6o (hil& J else if **t-9l K NZ?? OO *t-9r K NZ??
t2 K rootH if *t-9r K NZ?? + K smallest*t-9rH =ag K H L else + Klargest*t-9lH =ag K 2H L sear(h*root, +H t-9value K +H LL
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 29/56
LAB EXERCISE-6
Q$ Uesign an& implement an algorithm to perform ?E>T-RWTTE operation ona Ginary sear(h tree$Algorithm-?E>T-RWTTE*T, x
8$ y 0 right.x/ ]Set y
:$ right.x/ 0 left.y/ ] y[s left suGtree Ge(omes x[s right suGtree<$ if left.y/ NI?
B$ then p.left.y// 0 x ] Set the parent relation from left.y/ to xC$ p.y/ 0 p.x/ ] The parent of x Ge(omes the parent of yD$ if p.x/ K NI?$ then root.T/ 0 y$ else if x K left.p.x//2$ then left.p.x// 0 y3$ else right.p.x// 0 y8$ left.y/ 0 x ] Put x on y[s left:$ p.x/ 0 y ] y Ge(omes x[s parent
Program- #include<stdio.h> #include<conio.h> unsigned int leftrot(unsigned int x, unsigned int n); void main() { unsigned int x,n,l; clrscr(); printf("Enter the value of x and nn"); scanf("!u!u",x,n); lleftrot(x,n); printf("x!u,n!u,r!un",x,n,r); getch();
$ unsigned int rightrot(unsigned int x, unsigned int n) { unsigned int %,&,a; ax<<n; &x>>('n); %a*&; return %; $
Q2$ Uesign an& implement an algorithm to perform right rotate operation on aGinary sear(h tree$
Algorithm-RI@)T-RWTTE*T, x
y K x$left set y x$left K y$right turn y[s right suG tree into x[s left suG tree if y$right Y NZ?? y$right$parent K x y$parent K x$parent lin+ x[s parent to y if x$parent KK NZ?? T$root K y else if x KK x$parent$left x$parent$left K y else
x$parent$right K y y$right K x put x on y[s right x$parent K yProgram-
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 30/56
#include<stdio.h>#include<conio.h>unsigned int rightrot(unsigned int x, unsigned int n);void main(){ unsigned int x,n,r; clrscr(); printf("Enter the value of x and nn"); scanf("!u!u",x,n);
rrightrot(x,n); printf("x!u,n!u,r!un",x,n,r); getch();$unsigned int rightrot(unsigned int x, unsigned int n){ unsigned int %,&,a; ax>>n; &x<<('n); %a*&; return %;$
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 31/56
LAB EXERCISE-7
Uesign an& implement an algorithm to insert any numGer of elements in REU-?!" Tree$ Zse the appropriate te(hniues to sho6 the (olor of no&es alsoan& analy%e its time (omplexity Gase& on !PZ utili%ation$Algorithm-R-INSERT*T, %
:$ y 0 NI?<$ x 0 root.T/B$ while x NI?
C$ do y 0 xD$ if +ey.%/ 4 +ey.x/2$ then x 0 left.x/2$ else x 0 right.x/22$ p.%/ 0 yC$ if y K NI?D$ then root.T/ 0 %$ else if +ey.%/ 4 +ey.y/$ then left.y/ 0 %2$ else right.y/ 0 %3$ left.%/ 0 NI?8$ right.%/ 0 NI?:$ (olor.%/ 0 REU<$ R-INSERT->IXZP*T, %
R-INSERT->IXZP*T, %<$ while (olor.p.%// K REUB$ do if p.%/ K left.p.p.%///C$ then y 0 right.p.p.%///
D$ if (olor.y/ K REU!" then #ase$2$ else if % K right.p.%//" then #ase%" #ase%28$ else *same as then (lause 6ith 5right7 an& 5left7
ex(hange&2:$ (olor.root.T// 0 ?!"
R-INSERT->IXZP ^ !ase *% is a right (hil&
$ !olor p.%/ Gla(+
2$ !olor y Gla(+3$ !olor p.p.%// re&8$ %Kp.p.%//*% is a left (hil&$ !olor p.%/0Gla(+2$ !olor y0Gla(+3$ !olor p.p.%//0re&8$ %Kp.p.%//
R-INSERT->IXZP ^ !ase 3$&" %[s 5un(le7 *y is 'la
2$ % is a left (hil&28$ (olor p.%/ 0Gla(+2:$ (olor p.p.%// 0re&
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 32/56
2<$ RI@)T-RWTTE*T,p.p.%//
R-INSERT->IXZP ^ !ase 2• %[s 5un(le7 *y is 'la / % is a right (hil&$ %0p.%/
2$ ?E>T-RWTTE*T,%Program-Fin(lu&e 4(st&io9Fin(lu&e 4st&liG$h9using namespa(e st&Henum no&e!olor REU, ?!" LH stru(t rGNo&e
int &ata, (olorH stru(t rGNo&e Jlin+.2/H LH stru(t rGNo&e Jroot K NZ??H stru(t rGNo&e J(reateNo&e*int &ata stru(t rGNo&e Jne6no&eH ne6no&e K *stru(t rGNo&e Jmallo(*si%eof*stru(t rGNo&eH ne6no&e-9&ata K &ataH ne6no&e-9(olor K REUH ne6no&e-9lin+./ K ne6no&e-9lin+./ K NZ??H return ne6no&eH L voi& insertion*int &ata stru(t rGNo&e Jsta(+.DC/, Jptr, Jne6no&e, JxPtr, JyPtrH int &ir.DC/, ht K , in&exH ptr K rootH if *root root K (reateNo&e*&ataH returnH L sta(+.ht/ K rootH &ir.ht11/ K H 6hile *ptr K NZ??
if *ptr-9&ata KK &ata
printf*Uupli(ates Not llo6e&nH returnH
L in&ex K *&ata - ptr-9&ata 9 _ H sta(+.ht/ K ptrH ptr K ptr-9lin+.in&ex/H &ir.ht11/ K in&exH L sta(+.ht - /-9lin+.in&ex/ K ne6no&e K (reateNo&e*&ataH 6hile **ht 9K 3 OO *sta(+.ht - /-9(olor KK REU
if *&ir.ht - 2/ KK
yPtr K sta(+.ht - 2/-9lin+./H if *yPtr K NZ?? OO yPtr-9(olor KK REU
sta(+.ht - 2/-9(olor K REUH sta(+.ht - /-9(olor K yPtr-9(olor K ?!"H ht K ht -2H L else
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 33/56
if *&ir.ht - / KK
yPtr K sta(+.ht - /H L
else
xPtr K sta(+.ht - /H
yPtr K xPtr-9lin+./H xPtr-9lin+./ K yPtr-9lin+./H yPtr-9lin+./ K xPtrH sta(+.ht - 2/-9lin+./ K yPtrH L xPtr K sta(+.ht - 2/H xPtr-9(olor K REUH yPtr-9(olor K ?!"H xPtr-9lin+./ K yPtr-9lin+./H yPtr-9lin+./ K xPtrH if *xPtr KK root
root K yPtrH L
else sta(+.ht - 3/-9lin+.&ir.ht - 3// K yPtrH L Grea+H L L
else
yPtr K sta(+.ht - 2/-9lin+./H if **yPtr K NZ?? OO *yPtr-9(olor KK REU
sta(+.ht - 2/-9(olor K REUH sta(+.ht - /-9(olor K yPtr-9(olor K ?!"H ht K ht - 2H L
else
if *&ir.ht - / KK
yPtr K sta(+.ht - /H L else
xPtr K sta(+.ht - /H
yPtr K xPtr-9lin+./H xPtr-9lin+./ K yPtr-9lin+./H yPtr-9lin+./ K xPtrH sta(+.ht - 2/-9lin+./ K yPtrH L xPtr K sta(+.ht - 2/H yPtr-9(olor K ?!"H xPtr-9(olor K REUH xPtr-9lin+./ K yPtr-9lin+./H yPtr-9lin+./ K xPtrH if *xPtr KK root root K yPtrH L
else
sta(+.ht - 3/-9lin+.&ir.ht - 3// K yPtrH L Grea+H L
L L root-9(olor K ?!"H L
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 34/56
int main*int &ataHprintf*nEnter the &ata to insert Hs(anf*\&, O&ataHinsertion*&ataHget(h*H
return HL
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 35/56
LAB EXERCISE-8
Uesign an& implement an algorithm to &elete any numGer of elements in REU-?!" Tree$ Zse theappropriate te(hniues to sho6 the (olor of no&es also an& analy%e its time(omplexity on the Gasis of !PZ utili%ation$Algorithm-
R-UE?ETE*T, % if left.%/ K nil.T/ or right.%/ K nil.T/ 2 then y 0 % 3 else y 0 TREE-SZ!!ESSWR*% 8 if left.y/ Y nil.T/ : then x 0 left.y/ < else x 0 right.y/ B p.x/ 0 p.y/ C if p.y/ K nil.T/ D then root.T/ 0 x else if y K left.p.y// then left.p.y// 0 x 2 else right.p.y// 0 x 3 if y K %
8 then +ey.%/ 0 +ey.y/ : (opy y`s satellite &ata into % < if (olor.y/ K ?!" B then R-UE?ETE->IXZP*T, x C return y
TREE-SZ!!ESSWR*x if right.x/ Y NI?2 then return TREE-MINIMZM *right.x/3 y 0 p.x/8 6hile y Y NI? an& x K right.y/: &o x 0 y< y 0 p.y/B return yR-UE?ETE->IXZP*T, x 6hile x Y root.T/ an& (olor.x/ K ?!" 2 &o if x K left.p.x// 3 then 6 0 right.p.x// 8 if (olor.6/ K REU : then (olor.6/ 0 ?!" ▹ !ase < (olor.p.x// 0 REU ▹ !ase B ?E>T-RWTTE*T, p.x/ ▹ !ase C 6 0 right.p.x// ▹ !ase D if (olor.left.6// K ?!" an& (olor.right.6// K ?!" then (olor.6/ 0 REU ▹ !ase 2 x p.x/ ▹ !ase 22 else if (olor.right.6// K ?!" 3 then (olor.left.6// 0 ?!" ▹ !ase 38 (olor.6/ 0 REU ▹ !ase 3: RI@)T-RWTTE*T, 6 ▹ !ase 3< 6 0 right.p.x// ▹ !ase 3B (olor.6/ 0 (olor.p.x// ▹ !ase 8C (olor.p.x// 0 ?!" ▹ !ase 8D (olor.right.6// 0 ?!" ▹ !ase 82 ?E>T-RWTTE*T, p.x/ ▹ !ase 82 x 0 root.T/ ▹ !ase 822 else *same as then (lause 6ith right an& left ex(hange&23 (olor.x/ 0 ?!"
Program-Fin(lu&e 4(st&io9Fin(lu&e 4st&liG$h9using namespa(e st&Henum no&e!olor REU, ?!" LH stru(t rGNo&e
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 36/56
int &ata, (olorH stru(t rGNo&e Jlin+.2/H LH stru(t rGNo&e Jroot K NZ??H
stru(t rGNo&e J(reateNo&e*int &ata
stru(t rGNo&e Jne6no&eH ne6no&e K *stru(t rGNo&e Jmallo(*si%eof*stru(t rGNo&eH ne6no&e-9&ata K &ataH ne6no&e-9(olor K REUH ne6no&e-9lin+./ K ne6no&e-9lin+./ K NZ??H return ne6no&eH Lvoi& &eletion*int &ata
stru(t rGNo&e Jsta(+.DC/, Jptr, JxPtr, JyPtrH stru(t rGNo&e JpPtr, JPtr, JrPtrH int &ir.DC/, ht K , &i, iH int (olorH if *root
printf*Tree not availaGlenH returnH L ptr K rootH 6hile *ptr K NZ??
if **&ata - ptr-9&ata KK Grea+H &i K *&ata - ptr-9&ata 9 _ H sta(+.ht/ K ptrH &ir.ht11/ K &iH ptr K ptr-9lin+.&i/H L if *ptr-9lin+./ KK NZ??
if **ptr KK root OO *ptr-9lin+./ KK NZ??
free*ptrH root K NZ??H L else if *ptr KK root
root K ptr-9lin+./H free*ptrH L else
sta(+.ht - /-9lin+.&ir.ht - // K ptr-9lin+./H L L
else
xPtr K ptr-9lin+./H if *xPtr-9lin+./ KK NZ??
xPtr-9lin+./ K ptr-9lin+./H (olor K xPtr-9(olorH xPtr-9(olor K ptr-9(olorH ptr-9(olor K (olorH if *ptr KK root
root K xPtrH L
else
sta(+.ht - /-9lin+.&ir.ht - // K xPtrH L &ir.ht/ K H
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 37/56
sta(+.ht11/ K xPtrH L
else
i K ht11H 6hile *
&ir.ht/ K H
sta(+.ht11/ K xPtrH yPtr K xPtr-9lin+./H if *yPtr-9lin+./ Grea+H xPtr K yPtrH L &ir.i/ K H sta(+.i/ K yPtrH if *i 9 sta(+.i - /-9lin+.&ir.i - // K yPtrH yPtr-9lin+./ K ptr-9lin+./H xPtr-9lin+./ K yPtr-9lin+./H yPtr-9lin+./ K ptr-9lin+./H if *ptr KK root
root K yPtrH L
(olor K yPtr-9(olorH yPtr-9(olor K ptr-9(olorH ptr-9(olor K (olorH L L if *ht 4 returnH if *ptr-9(olor KK ?!"
6hile *
pPtr K sta(+.ht - /-9lin+.&ir.ht - //H if *pPtr OO pPtr-9(olor KK REU
pPtr-9(olor K ?!"H Grea+H L if *ht 4 2 Grea+H if *&ir.ht - 2/ KK
rPtr K sta(+.ht - /-9lin+./H if *rPtr Grea+H if *rPtr-9(olor KK REU
sta(+.ht - /-9(olor K REUH
rPtr-9(olor K ?!"H sta(+.ht - /-9lin+./ K rPtr-9lin+./H rPtr-9lin+./ K sta(+.ht - /H if *sta(+.ht - / KK root
root K rPtrH L
else
sta(+.ht - 2/-9lin+.&ir.ht - 2// K rPtrH L &ir.ht/ K H
sta(+.ht/ K sta(+.ht - /H sta(+.ht - / K rPtrH ht11H rPtr K sta(+.ht - /-9lin+./H
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 38/56
L if * *rPtr-9lin+./ bb rPtr-9lin+./-9(olor KK ?!" OO *rPtr-9lin+./ bb rPtr-9lin+./-9(olor KK ?!"
rPtr-9(olor K REUH
Lelse
if *rPtr-9lin+./ bb rPtr-9lin+./-9(olor KK ?!"Ptr K rPtr-9lin+./H
rPtr-9(olor K REUH Ptr-9(olor K ?!"H rPtr-9lin+./ K Ptr-9lin+./H Ptr-9lin+./ K rPtrH rPtr K sta(+.ht - /-9lin+./ K PtrH L
rPtr-9(olor K sta(+.ht - /-9(olorH sta(+.ht - /-9(olor K ?!"H rPtr-9lin+./-9(olor K ?!"H sta(+.ht - /-9lin+./ K rPtr-9lin+./H rPtr-9lin+./ K sta(+.ht - /H
if *sta(+.ht - / KK root root K rPtrH L
else
sta(+.ht - 2/-9lin+.&ir.ht - 2// K rPtrH L Grea+H L L
else
rPtr K sta(+.ht - /-9lin+./H if *rPtr Grea+H if *rPtr-9(olor KK REU
sta(+.ht - /-9(olor K REUH rPtr-9(olor K ?!"H sta(+.ht - /-9lin+./ K rPtr-9lin+./H rPtr-9lin+./ K sta(+.ht - /H if *sta(+.ht - / KK root
root K rPtrH L
else
sta(+.ht - 2/-9lin+.&ir.ht - 2// K rPtrH L &ir.ht/ K H sta(+.ht/ K sta(+.ht - /H sta(+.ht - / K rPtrH ht11H rPtr K sta(+.ht - /-9lin+./H L if * *rPtr-9lin+./ bb rPtr-9lin+./-9(olor KK ?!" OO *rPtr-9lin+./ bb rPtr-9lin+./-9(olor KK ?!"
rPtr-9(olor K REUH L
else
if *rPtr-9lin+./ bb rPtr-9lin+./-9(olor KK ?!"
Ptr K rPtr-9lin+./H rPtr-9(olor K REUH
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 39/56
Ptr-9(olor K ?!"H rPtr-9lin+./ K Ptr-9lin+./H Ptr-9lin+./ K rPtrH rPtr K sta(+.ht - /-9lin+./ K PtrH L rPtr-9(olor K sta(+.ht - /-9(olorH sta(+.ht - /-9(olor K ?!"H rPtr-9lin+./-9(olor K ?!"H
sta(+.ht - /-9lin+./ K rPtr-9lin+./H rPtr-9lin+./ K sta(+.ht - /H if *sta(+.ht - / KK root
root K rPtrH L else
sta(+.ht - 2/-9lin+.&ir.ht - 2// K rPtrH L Grea+H L L ht--H L
L Lint main*int &ataHprintf*nEnter the element to Ge &elete& Hs(anf*\&, O&ataH&eletion*&ataHget(h*H
return H
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 40/56
LAB EXERCISE-9
Q$ Uesign an& implement (o&es for Gasi( operations for re& Gla(+ trees arein(lu&e& in the Goo+$ && to the (o&e the follo6ing metho&sa$ Uelete a no&e from the re& Gla(+ tree$G$ !ount the numGer of leaves in a tree$($ Return the height of a tree$
&$ Return a list of all +eys in a tree Get6een a an& G$naly%e the &ieren(e of time (omplexities on the Gasis of !PZ utili%ation$Algorithm-R-INSERT*T, %
23$ y 0 NI?28$ x 0 root.T/2:$ while x NI?
2<$ do y 0 x2B$ if +ey.%/ 4 +ey.x/2C$ then x 0 left.x/2D$ else x 0 right.x/3$ p.%/ 0 yB$ if y K NI?
C$ then root.T/ 0 %D$ else if +ey.%/ 4 +ey.y/2$ then left.y/ 0 %2$ else right.y/ 0 %22$ left.%/ 0 NI?23$ right.%/ 0 NI?28$ (olor.%/ 0 REU2:$ R-INSERT->IXZP*T, %
R-INSERT->IXZP*T, %2<$ while (olor.p.%// K REU2B$ do if p.%/ K left.p.p.%///2C$ then y 0 right.p.p.%///2D$ if (olor.y/ K REU%!" then #ase$3$ else if % K right.p.%//%" then #ase%%" #ase%38$ else *same as then (lause 6ith 5right7 an& 5left7 ex(hange&3:$ (olor.root.T// 0 ?!"
R-INSERT->IXZP ^ !ase *% is a right (hil&
:$ !olor p.%/ Gla(+<$ !olor y Gla(+B$ !olor p.p.%// re&C$ %Kp.p.%//*% is a left (hil&:$ !olor p.%/0Gla(+<$ !olor y0Gla(+B$ !olor p.p.%//0re&C$ %Kp.p.%//
R-INSERT->IXZP ^ !ase 3$" %[s 5un(le7 *y is 'la 22$ % is a left (hil&2B$ (olor p.%/ 0Gla(+2C$ (olor p.p.%// 0re&2D$ RI@)T-RWTTE*T,p.p.%//
R-INSERT->IXZP ^ !ase 2• %[s 5un(le7 *y is 'la / % is a right (hil&3$ %0p.%/8$ ?E>T-RWTTE*T,%
*aR-UE?ETE*T, % if left.%/ K nil.T/ or right.%/ K nil.T/ 2 then y 0 %
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 41/56
3 else y 0 TREE-SZ!!ESSWR*% 8 if left.y/ Y nil.T/ : then x 0 left.y/ < else x 0 right.y/ B p.x/ 0 p.y/ C if p.y/ K nil.T/ D then root.T/ 0 x else if y K left.p.y//
then left.p.y// 0 x 2 else right.p.y// 0 x 3 if y K % 8 then +ey.%/ 0 +ey.y/ : (opy y`s satellite &ata into % < if (olor.y/ K ?!" B then R-UE?ETE->IXZP*T, x C return y
TREE-SZ!!ESSWR*x if right.x/ Y NI?2 then return TREE-MINIMZM *right.x/3 y 0 p.x/8 6hile y Y NI? an& x K right.y/: &o x 0 y
< y 0 p.y/B return yR-UE?ETE->IXZP*T, x 6hile x Y root.T/ an& (olor.x/ K ?!" 2 &o if x K left.p.x// 3 then 6 0 right.p.x// 8 if (olor.6/ K REU : then (olor.6/ 0 ?!" ▹ !ase < (olor.p.x// 0 REU ▹ !ase B ?E>T-RWTTE*T, p.x/ ▹ !ase C 6 0 right.p.x// ▹ !ase D if (olor.left.6// K ?!" an& (olor.right.6// K ?!" then (olor.6/ 0 REU ▹ !ase 2 x p.x/ ▹ !ase 22 else if (olor.right.6// K ?!" 3 then (olor.left.6// 0 ?!" ▹ !ase 38 (olor.6/ 0 REU ▹ !ase 3: RI@)T-RWTTE*T, 6 ▹ !ase 3< 6 0 right.p.x// ▹ !ase 3B (olor.6/ 0 (olor.p.x// ▹ !ase 8C (olor.p.x// 0 ?!" ▹ !ase 8D (olor.right.6// 0 ?!" ▹ !ase 82 ?E>T-RWTTE*T, p.x/ ▹ !ase 82 x 0 root.T/ ▹ !ase 822 else *same as then (lause 6ith right an& left ex(hange&23 (olor.x/ 0 ?!"
Program-Fin(lu&e 4(st&io9
Fin(lu&e 4st&liG$h9
using namespa(e st&H
enum no&e!olor REU, ?!" LH
stru(t rGNo&e
int &ata, (olorH stru(t rGNo&e Jlin+.2/H LH
stru(t rGNo&e Jroot K NZ??H
stru(t rGNo&e J(reateNo&e*int &ata
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 42/56
stru(t rGNo&e Jne6no&eH ne6no&e K *stru(t rGNo&e Jmallo(*si%eof*stru(t rGNo&eH ne6no&e-9&ata K &ataH ne6no&e-9(olor K REUH ne6no&e-9lin+./ K ne6no&e-9lin+./ K NZ??H return ne6no&eH L
voi& insertion*int &ata stru(t rGNo&e Jsta(+.DC/, Jptr, Jne6no&e, JxPtr, JyPtrH int &ir.DC/, ht K , in&exH ptr K rootH if *root
root K (reateNo&e*&ataH returnH L sta(+.ht/ K rootH &ir.ht11/ K H 6hile *ptr K NZ??
if *ptr-9&ata KK &ata
printf*Uupli(ates Not llo6e&nH returnH L in&ex K *&ata - ptr-9&ata 9 _ H sta(+.ht/ K ptrH ptr K ptr-9lin+.in&ex/H &ir.ht11/ K in&exH L sta(+.ht - /-9lin+.in&ex/ K ne6no&e K (reateNo&e*&ataH 6hile **ht 9K 3 OO *sta(+.ht - /-9(olor KK REU
if *&ir.ht - 2/ KK
yPtr K sta(+.ht - 2/-9lin+./H if *yPtr K NZ?? OO yPtr-9(olor KK REU
sta(+.ht - 2/-9(olor K REUH
sta(+.ht - /-9(olor K yPtr-9(olor K ?!"H ht K ht -2H L else
if *&ir.ht - / KK
yPtr K sta(+.ht - /H
L else
xPtr K sta(+.ht - /H
yPtr K xPtr-9lin+./H xPtr-9lin+./ K yPtr-9lin+./H yPtr-9lin+./ K xPtrH sta(+.ht - 2/-9lin+./ K yPtrH L
xPtr K sta(+.ht - 2/H
xPtr-9(olor K REUH yPtr-9(olor K ?!"H
xPtr-9lin+./ K yPtr-9lin+./H yPtr-9lin+./ K xPtrH if *xPtr KK root
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 43/56
root K yPtrH L
else
sta(+.ht - 3/-9lin+.&ir.ht - 3// K yPtrH L Grea+H L
L else
yPtr K sta(+.ht - 2/-9lin+./H if **yPtr K NZ?? OO *yPtr-9(olor KK REU
sta(+.ht - 2/-9(olor K REUH
sta(+.ht - /-9(olor K yPtr-9(olor K ?!"H ht K ht - 2H L
else
if *&ir.ht - / KK
yPtr K sta(+.ht - /H L else
xPtr K sta(+.ht - /H
yPtr K xPtr-9lin+./H xPtr-9lin+./ K yPtr-9lin+./H yPtr-9lin+./ K xPtrH sta(+.ht - 2/-9lin+./ K yPtrH L
xPtr K sta(+.ht - 2/H
yPtr-9(olor K ?!"H xPtr-9(olor K REUH xPtr-9lin+./ K yPtr-9lin+./H yPtr-9lin+./ K xPtrH if *xPtr KK root root K yPtrH L
else
sta(+.ht - 3/-9lin+.&ir.ht - 3// K yPtrH L Grea+H L L L
root-9(olor K ?!"H L
voi& &eletion*int &ata
stru(t rGNo&e Jsta(+.DC/, Jptr, JxPtr, JyPtrH stru(t rGNo&e JpPtr, JPtr, JrPtrH int &ir.DC/, ht K , &i, iH int (olorH
if *root
printf*Tree not availaGlenH returnH
L
ptr K rootH 6hile *ptr K NZ??
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 44/56
if **&ata - ptr-9&ata KK Grea+H &i K *&ata - ptr-9&ata 9 _ H sta(+.ht/ K ptrH &ir.ht11/ K &iH ptr K ptr-9lin+.&i/H L
if *ptr-9lin+./ KK NZ??
if **ptr KK root OO *ptr-9lin+./ KK NZ??
free*ptrH root K NZ??H L else if *ptr KK root
root K ptr-9lin+./H free*ptrH L else
sta(+.ht - /-9lin+.&ir.ht - // K ptr-9lin+./H
L Lelse
xPtr K ptr-9lin+./H if *xPtr-9lin+./ KK NZ??
xPtr-9lin+./ K ptr-9lin+./H (olor K xPtr-9(olorH xPtr-9(olor K ptr-9(olorH ptr-9(olor K (olorH
if *ptr KK root
root K xPtrH L
else
sta(+.ht - /-9lin+.&ir.ht - // K xPtrH L
&ir.ht/ K H sta(+.ht11/ K xPtrH L
else
i K ht11H 6hile *
&ir.ht/ K H sta(+.ht11/ K xPtrH yPtr K xPtr-9lin+./H if *yPtr-9lin+./ Grea+H xPtr K yPtrH L
&ir.i/ K H sta(+.i/ K yPtrH if *i 9 sta(+.i - /-9lin+.&ir.i - // K yPtrH
yPtr-9lin+./ K ptr-9lin+./H
xPtr-9lin+./ K yPtr-9lin+./H yPtr-9lin+./ K ptr-9lin+./H
if *ptr KK root
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 45/56
root K yPtrH L
(olor K yPtr-9(olorH yPtr-9(olor K ptr-9(olorH ptr-9(olor K (olorH L
L if *ht 4 returnH if *ptr-9(olor KK ?!"
6hile *
pPtr K sta(+.ht - /-9lin+.&ir.ht - //H if *pPtr OO pPtr-9(olor KK REU
pPtr-9(olor K ?!"H Grea+H L
if *ht 4 2 Grea+H
if *&ir.ht - 2/ KK
rPtr K sta(+.ht - /-9lin+./H
if *rPtr Grea+H
if *rPtr-9(olor KK REU
sta(+.ht - /-9(olor K REUH
rPtr-9(olor K ?!"H sta(+.ht - /-9lin+./ K rPtr-9lin+./H rPtr-9lin+./ K sta(+.ht - /H
if *sta(+.ht - / KK root
root K rPtrH L
else
sta(+.ht - 2/-9lin+.&ir.ht - 2// K rPtrH L &ir.ht/ K H sta(+.ht/ K sta(+.ht - /H
sta(+.ht - / K rPtrH ht11H
rPtr K sta(+.ht - /-9lin+./H L
if * *rPtr-9lin+./ bb rPtr-9lin+./-9(olor KK ?!" OO *rPtr-9lin+./ bb rPtr-9lin+./-9(olor KK ?!"
rPtr-9(olor K REUH
Lelse
if *rPtr-9lin+./ bb rPtr-9lin+./-9(olor KK ?!"
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 46/56
Ptr K rPtr-9lin+./H rPtr-9(olor K REUH Ptr-9(olor K ?!"H rPtr-9lin+./ K Ptr-9lin+./H Ptr-9lin+./ K rPtrH rPtr K sta(+.ht - /-9lin+./ K PtrH L
rPtr-9(olor K sta(+.ht - /-9(olorH sta(+.ht - /-9(olor K ?!"H rPtr-9lin+./-9(olor K ?!"H sta(+.ht - /-9lin+./ K rPtr-9lin+./H rPtr-9lin+./ K sta(+.ht - /H if *sta(+.ht - / KK root
root K rPtrH L
else
sta(+.ht - 2/-9lin+.&ir.ht - 2// K rPtrH L Grea+H
L L
else
rPtr K sta(+.ht - /-9lin+./H if *rPtr Grea+H
if *rPtr-9(olor KK REU
sta(+.ht - /-9(olor K REUH rPtr-9(olor K ?!"H sta(+.ht - /-9lin+./ K rPtr-9lin+./H rPtr-9lin+./ K sta(+.ht - /H
if *sta(+.ht - / KK root
root K rPtrH L
else
sta(+.ht - 2/-9lin+.&ir.ht - 2// K rPtrH L &ir.ht/ K H sta(+.ht/ K sta(+.ht - /H sta(+.ht - / K rPtrH ht11H
rPtr K sta(+.ht - /-9lin+./H L if * *rPtr-9lin+./ bb rPtr-9lin+./-9(olor KK ?!" OO *rPtr-9lin+./ bb rPtr-9lin+./-9(olor KK ?!"
rPtr-9(olor K REUH L
else
if *rPtr-9lin+./ bb rPtr-9lin+./-9(olor KK ?!"
Ptr K rPtr-9lin+./H rPtr-9(olor K REUH Ptr-9(olor K ?!"H rPtr-9lin+./ K Ptr-9lin+./H Ptr-9lin+./ K rPtrH rPtr K sta(+.ht - /-9lin+./ K PtrH L rPtr-9(olor K sta(+.ht - /-9(olorH
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 47/56
sta(+.ht - /-9(olor K ?!"H rPtr-9lin+./-9(olor K ?!"H sta(+.ht - /-9lin+./ K rPtr-9lin+./H rPtr-9lin+./ K sta(+.ht - /H if *sta(+.ht - / KK root
root K rPtrH L else
sta(+.ht - 2/-9lin+.&ir.ht - 2// K rPtrH L Grea+H L L ht--H L L Lint main*
int (h, &ataH 6hile *
printf*$ Insert a ne6 elementn2$ Uelete an elementnH s(anf*\&, O(hH s6it(h *(h
(ase
printf*nEnter the &ata to insert H s(anf*\&, O&ataH insertion*&ataH Grea+H
L
(ase 2
printf*nEnter the element to Ge &elete& H s(anf*\&, O&ataH &eletion*&ataH Grea+H
LL printf*nH L return H L
*GAlgorithm-
get?eaf!ount*stru(t no&eJ no&e
$ if*no&eKNZ??
2$ return
3$ if*no&e-9leftKNZ?? an& no&e-9rightKNZ??
8$ return
:$ else
<$ return get?eaf!ount*no&e-9left1get?eaf!ount*no&e-9right
Program-
Fin(lu&e 4st&io$h9
Fin(lu&e 4st&liG$h9
stru(t no&e
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 48/56
int &ataH
stru(t no&eJ leftH
stru(t no&eJ rightH
LH
unsigne& int get?eaf!ount*stru(t no&eJ no&e
if*no&e KK NZ??
return H
if*no&e-9left KK NZ?? OO no&e-9rightKKNZ??
return H
else
return get?eaf!ount*no&e-9left1get?eaf!ount*no&e-9rightH
L
int main*
stru(t no&e Jroot K ne6No&e*H
root-9left K ne6No&e*2H
root-9right K ne6No&e*3H
root-9left-9left K ne6No&e*8H
root-9left-9right K ne6No&e*:H
printf*?eaf (ount of the tree is \&, get?eaf!ount*rootH
get(har*H
return H
L
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 49/56
LAB EXERCISE-10
Q$ Uesign an& implement (o&es for the (onstru(tion of c? tree having anynumGer of no&es given Gy user an& perform single an& &ouGle rotation as perthe reuirement of c? tree$ naly%e its time (omplexity on the Gasis of !PZutili%ation$Program-
Fin(lu&e4st&io$h9Fin(lu&e4st&liG$h9 n c? tree no&estru(t no&e int +eyH stru(t no&e JleftH stru(t no&e JrightH int heightHLH utility fun(tion to get maximum of t6o integersint max*int a, int GH utility fun(tion to get height of the treeint height*stru(t no&e JN
if *N KK NZ?? return H return N-9heightHL utility fun(tion to get maximum of t6o integersint max*int a, int G return *a 9 G_ a GHLJ )elper fun(tion that allo(ates a ne6 no&e 6ith the given +ey an& NZ?? left an& right pointers$ Jstru(t no&eJ ne6No&e*int +ey stru(t no&eJ no&e K *stru(t no&eJ mallo(*si%eof*stru(t no&eH no&e-9+ey K +eyH no&e-9left K NZ??H no&e-9right K NZ??H no&e-9height K H ne6 no&e is initially a&&e& at leaf return*no&eHL utility fun(tion to right rotate suGtree roote& 6ith y See the &iagram given aGove$stru(t no&e JrightRotate*stru(t no&e Jy stru(t no&e Jx K y-9leftH stru(t no&e JT2 K x-9rightH Perform rotation x-9right K yH y-9left K T2H Zp&ate heights y-9height K max*height*y-9left, height*y-9right1H x-9height K max*height*x-9left, height*x-9right1H Return ne6 root return xHL utility fun(tion to left rotate suGtree roote& 6ith x See the &iagram given aGove$stru(t no&e JleftRotate*stru(t no&e Jx stru(t no&e Jy K x-9rightH stru(t no&e JT2 K y-9leftH
Perform rotation y-9left K xH x-9right K T2H Zp&ate heights
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 50/56
x-9height K max*height*x-9left, height*x-9right1H y-9height K max*height*y-9left, height*y-9right1H Return ne6 root return yHL @et alan(e fa(tor of no&e Nint getalan(e*stru(t no&e JN
if *N KK NZ?? return H return height*N-9left - height*N-9rightHLstru(t no&eJ insert*stru(t no&eJ no&e, int +ey J $ Perform the normal ST rotation J if *no&e KK NZ?? return*ne6No&e*+eyH if *+ey 4 no&e-9+ey no&e-9left K insert*no&e-9left, +eyH else no&e-9right K insert*no&e-9right, +eyH J 2$ Zp&ate height of this an(estor no&e J
no&e-9height K max*height*no&e-9left, height*no&e-9right 1 H J 3$ @et the Galan(e fa(tor of this an(estor no&e to (he(+ 6hether this no&e Ge(ame unGalan(e& J int Galan(e K getalan(e*no&eH If this no&e Ge(omes unGalan(e&, then there are 8 (ases ?eft ?eft !ase if *Galan(e 9 OO +ey 4 no&e-9left-9+ey return rightRotate*no&eH Right Right !ase if *Galan(e 4 - OO +ey 9 no&e-9right-9+ey return leftRotate*no&eH ?eft Right !ase if *Galan(e 9 OO +ey 9 no&e-9left-9+ey no&e-9left K leftRotate*no&e-9leftH return rightRotate*no&eH L Right ?eft !ase if *Galan(e 4 - OO +ey 4 no&e-9right-9+ey no&e-9right K rightRotate*no&e-9rightH return leftRotate*no&eH L J return the *un(hange& no&e pointer J return no&eHL utility fun(tion to print preor&er traversal of the tree$ The fun(tion also prints height of every no&evoi& preWr&er*stru(t no&e Jroot if*root K NZ?? printf*\& , root-9+eyH preWr&er*root-9leftH preWr&er*root-9rightH LLJ Uriver program to test aGove fun(tionJint main* stru(t no&e Jroot K NZ??H J !onstru(ting tree given in the aGove 'gure J root K insert*root, H
root K insert*root, 2H root K insert*root, 3H root K insert*root, 8H root K insert*root, :H
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 51/56
root K insert*root, 2:H J The (onstru(te& c? Tree 6oul& Ge 3 2 8 2: : J
printf*Pre or&er traversal of the (onstru(te& c? tree is nH preWr&er*rootH return H
L
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 52/56
*A+ EER#ISE-$$Uesign an& implement algorithm for -Tree 6ith t*minimum &egree of tree K3for follo6ingoperations(A) -Tree Sear(h(+) -Tree !reate
(#) -Tree Insertnaly%e its time (omplexities on the Gasis of !PZ utili%ation an& dustify it$Program- Fin(lu&e 4st&io$h9 Fin(lu&e 4st&liG$h9 F&e'ne MX 8 F&e'ne MIN 2 stru(t GtreeNo&e int val.MX 1 /, (ountH stru(t GtreeNo&e Jlin+.MX 1 /H LH stru(t GtreeNo&e JrootH J (reating ne6 no&e J
stru(t GtreeNo&e J (reateNo&e*int val, stru(t GtreeNo&e J(hil& stru(t GtreeNo&e Jne6No&eH ne6No&e K *stru(t GtreeNo&e Jmallo(*si%eof*stru(t GtreeNo&eH ne6No&e-9val./ K valH ne6No&e-9(ount K H ne6No&e-9lin+./ K rootH ne6No&e-9lin+./ K (hil&H return ne6No&eH L J Pla(es the value in appropriate position J voi& a&&calToNo&e*int val, int pos, stru(t GtreeNo&e Jno&e, stru(t GtreeNo&e J(hil& int d K no&e-9(ountH 6hile *d 9 pos no&e-9val.d 1 / K no&e-9val.d/H no&e-9lin+.d 1 / K no&e-9lin+.d/H d--H L no&e-9val.d 1 / K valH no&e-9lin+.d 1 / K (hil&H no&e-9(ount11H L J split the no&e J voi& splitNo&e *int val, int Jpval, int pos, stru(t GtreeNo&e Jno&e, stru(t GtreeNo&e J(hil&, stru(t GtreeNo&e JJne6No&e int me&ian, dH if *pos 9 MIN me&ian K MIN 1 H else me&ian K MINH Jne6No&e K *stru(t GtreeNo&e Jmallo(*si%eof*stru(t GtreeNo&eH d K me&ian 1 H 6hile *d 4K MX *Jne6No&e-9val.d - me&ian/ K no&e-9val.d/H *Jne6No&e-9lin+.d - me&ian/ K no&e-9lin+.d/H d11H L no&e-9(ount K me&ianH *Jne6No&e-9(ount K MX - me&ianH if *pos 4K MIN a&&calToNo&e*val, pos, no&e, (hil&H L else a&&calToNo&e*val, pos - me&ian, Jne6No&e, (hil&H
L Jpval K no&e-9val.no&e-9(ount/H *Jne6No&e-9lin+./ K no&e-9lin+.no&e-9(ount/H no&e-9(ount--H
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 53/56
L J sets the value val in the no&e J int setcalueInNo&e*int val, int Jpval, stru(t GtreeNo&e Jno&e, stru(t GtreeNo&e JJ(hil& int posH if *no&e Jpval K valH J(hil& K NZ??H
return H L if *val 4 no&e-9val./ pos K H L else for *pos K no&e-9(ountH *val 4 no&e-9val.pos/ OO pos 9 H pos--H if *val KK no&e-9val.pos/ printf*Uupli(ates not allo6e&nH return H L L if *setcalueInNo&e*val, pval, no&e-9lin+.pos/, (hil& if *no&e-9(ount 4 MX
a&&calToNo&e*Jpval, pos, no&e, J(hil&H L else splitNo&e*Jpval, pval, pos, no&e, J(hil&, (hil&H return H L L return H L J insert val in -Tree J voi& insertion*int val int =ag, iH stru(t GtreeNo&e J(hil&H =ag K setcalueInNo&e*val, Oi, root, O(hil&H if *=ag root K (reateNo&e*i, (hil&H L J (opy su((essor for the value to Ge &elete& J voi& (opySu((essor*stru(t GtreeNo&e JmyNo&e, int pos stru(t GtreeNo&e J&ummyH &ummy K myNo&e-9lin+.pos/H for *H&ummy-9lin+./ K NZ??H &ummy K &ummy-9lin+./H myNo&e-9val.pos/ K &ummy-9val./H
L J removes the value from the given no&e an& rearrange values J voi& removecal*stru(t GtreeNo&e JmyNo&e, int pos int i K pos 1 H 6hile *i 4K myNo&e-9(ount myNo&e-9val.i - / K myNo&e-9val.i/H myNo&e-9lin+.i - / K myNo&e-9lin+.i/H i11H L myNo&e-9(ount--H L J shifts value from parent to right (hil& J voi& &oRightShift*stru(t GtreeNo&e JmyNo&e, int pos stru(t GtreeNo&e Jx K myNo&e-9lin+.pos/H int d K x-9(ountH 6hile *d 9 x-9val.d 1 / K x-9val.d/H x-9lin+.d 1 / K x-9lin+.d/H L
x-9val./ K myNo&e-9val.pos/H x-9lin+./ K x-9lin+./H x-9(ount11H x K myNo&e-9lin+.pos - /H
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 54/56
myNo&e-9val.pos/ K x-9val.x-9(ount/H myNo&e-9lin+.pos/ K x-9lin+.x-9(ount/H x-9(ount--H returnH L J shifts value from parent to left (hil& J voi& &o?eftShift*stru(t GtreeNo&e JmyNo&e, int pos int d K H
stru(t GtreeNo&e Jx K myNo&e-9lin+.pos - /H x-9(ount11H x-9val.x-9(ount/ K myNo&e-9val.pos/H x-9lin+.x-9(ount/ K myNo&e-9lin+.pos/-9lin+./H x K myNo&e-9lin+.pos/H myNo&e-9val.pos/ K x-9val./H x-9lin+./ K x-9lin+./H x-9(ount--H 6hile *d 4K x-9(ount x-9val.d/ K x-9val.d 1 /H x-9lin+.d/ K x-9lin+.d 1 /H d11H L returnH
L J merge no&es J voi& mergeNo&es*stru(t GtreeNo&e JmyNo&e, int pos int d K H stru(t GtreeNo&e Jx K myNo&e-9lin+.pos/, Jx2 K myNo&e-9lin+.pos - /H x2-9(ount11H x2-9val.x2-9(ount/ K myNo&e-9val.pos/H x2-9lin+.x2-9(ount/ K myNo&e-9lin+./H 6hile *d 4K x-9(ount x2-9(ount11H x2-9val.x2-9(ount/ K x-9val.d/H x2-9lin+.x2-9(ount/ K x-9lin+.d/H d11H L d K posH 6hile *d 4 myNo&e-9(ount myNo&e-9val.d/ K myNo&e-9val.d 1 /H myNo&e-9lin+.d/ K myNo&e-9lin+.d 1 /H d11H L myNo&e-9(ount--H free*xH L J a&dusts the given no&e J voi& a&dustNo&e*stru(t GtreeNo&e JmyNo&e, int pos if *pos if *myNo&e-9lin+./-9(ount 9 MIN &o?eftShift*myNo&e, H L else mergeNo&es*myNo&e, H L L else if *myNo&e-9(ount K pos if*myNo&e-9lin+.pos - /-9(ount 9 MIN &oRightShift*myNo&e, posH L else if *myNo&e-9lin+.pos 1 /-9(ount 9 MIN &o?eftShift*myNo&e, pos 1 H L else mergeNo&es*myNo&e, posH L L L else
if *myNo&e-9lin+.pos - /-9(ount 9 MIN &oRightShift*myNo&e, posH else mergeNo&es*myNo&e, posH
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 55/56
L L L J &elete val from the no&e J int &elcal>romNo&e*int val, stru(t GtreeNo&e JmyNo&e int pos, =ag K H if *myNo&e if *val 4 myNo&e-9val./
pos K H =ag K H L else for *pos K myNo&e-9(ountH *val 4 myNo&e-9val.pos/ OO pos 9 H pos--H if *val KK myNo&e-9val.pos/ =ag K H L else =ag K H L L if *=ag if *myNo&e-9lin+.pos - / (opySu((essor*myNo&e, posH
=ag K &elcal>romNo&e*myNo&e-9val.pos/, myNo&e-9lin+.pos/H if *=ag KK printf*@iven &ata is not present in -TreenH L L else removecal*myNo&e, posH L L else =ag K &elcal>romNo&e*val, myNo&e-9lin+.pos/H L if *myNo&e-9lin+.pos/ if *myNo&e-9lin+.pos/-9(ount 4 MIN a&dustNo&e*myNo&e, posH L L return =agH L J &elete val from -tree J voi& &eletion*int val, stru(t GtreeNo&e JmyNo&e stru(t GtreeNo&e JtmpH if *&elcal>romNo&e*val, myNo&e printf*@iven value is not present in -TreenH returnH L else if *myNo&e-9(ount KK tmp K myNo&eH myNo&e K myNo&e-9lin+./H free*tmpH L L root K myNo&eH returnH L J sear(h val in -Tree J voi& sear(hing*int val, int Jpos, stru(t GtreeNo&e JmyNo&e if *myNo&e returnH L if *val 4 myNo&e-9val./ Jpos K H L else for *Jpos K myNo&e-9(ountH *val 4 myNo&e-9val.Jpos/ OO Jpos 9 H *Jpos--H
if *val KK myNo&e-9val.Jpos/ printf*@iven &ata \& is present in -Tree, valH returnH L
8/16/2019 Programs Algo
http://slidepdf.com/reader/full/programs-algo 56/56
L sear(hing*val, pos, myNo&e-9lin+.Jpos/H returnH L J -Tree Traversal J voi& traversal*stru(t GtreeNo&e JmyNo&e int iH if *myNo&e
for *i K H i 4 myNo&e-9(ountH i11 traversal*myNo&e-9lin+.i/H printf*\& , myNo&e-9val.i 1 /H L traversal*myNo&e-9lin+.i/H L L int main* int val, (hH 6hile * printf*$ Insertiont2$ UeletionnH printf*3$ Sear(hingt8$ TraversalnH printf*:$ ExitnEnter your (hoi(eH s(anf*\&, O(hH
s6it(h *(h (ase printf*Enter your inputH s(anf*\&, OvalH insertion*valH Grea+H (ase 2 printf*Enter the element to &eleteH s(anf*\&, OvalH &eletion*val, rootH Grea+H (ase 3 printf*Enter the element to sear(hH s(anf*\&, OvalH sear(hing*val, O(h, rootH Grea+H (ase 8 traversal*rootH Grea+H (ase : exit*H &efault printf*Z have entere& 6rong optionnH Grea+H L printf*nH L
L