You are on page 1of 50

1.

Does it do what we want t to de P


a. Dacs it wstk .cstsectly accotding to the ginal
speoijicati ons the doak ?
3. a theoe documertotion which ccbe he
Lto wse it and há itiKA! u
4. Ase hunctions (s uba outines) eatocl in Buch
way hat dhey pealonm iÝgical ubaunctina
s. 46 the code eadable?

TheRinngve aPaogham
tkpends eng
Bhe input dhe
2. Jhe quality o coole grnated y the compilo
used to oLate th shject. poam .

Jhe nate ancl speLcd ot the instuctisn n


the mmacine wsed te icut thi hogam.

T(n) cn
n’ Snput siae:
C Constont

1. use FIBONACcI

3+4. , n<0 thene 4 stop.


enO.
n=0 than phint O, stop. I,0
1-8. ik n= hn yotint I, stos, I,0
4.

for to-2 to n, ole


n-/
||. fomy n-1
12.
n
l3. fam f
n-l
|4. encl;
15. ontnt (fr)
16. end,
5n+ 4

these istt tse costat


C

Tn) =,3n+en
let no0=5,3hen ue can
shou thot

Th) Big Sh
T) n.

3000

1Ö00

l0,,5 a0a5
n
s/a/23
Suppose T(n) and T,n) ae the tuming: timus

a, nzn ther To) < 9fo)


then T, (n) agon)
Let no= max( n )

We can wtit

Cornbineol nning dine Ton) +T,en) ws


OCmax (fn),gn))). Jhs is collac the
Bum suule.
St g) <fn) yo alln aboue constant ns.
then O(fn) ta)) us samu us
O(n+ n3)
The Feodudt Rule
4, T0) andl Tion)au OGFo) ard o Caoy)

Faom psokoduet suue,


OCefon) ’ 0(fn))
OCe) ’ ’lenstant time

Bubble Set

ijtemp : întege
O)
2 2
(n-) 2. n
2
n-)ut nln-)-
me
uk,i tiiuunning' Total
iketp intme ule, oduct bySe
).
ime oci) takes (G) te (3)
b the testingstatement ithe For
Çn biconci
endj
end, ()
end; (8)
.end: (1)
temp A=;I:= (6)
(5)
ALj-J temp:= (4)
(3)
de.:=hdoion-to
4
Veuy Jast Gouing hancin
A;) =
ALi, 1) = A(i-l, 2) \or iz 2
Auj) = A(-, A(, j-1)) tor ij z2
Aag) 2. 34
44 8 1G
2. 4 16

Ass.
Ackesomai Junction A Wsite a
non-tecuweive
yunction en
this(1ol&23)
Snvense Acketonarns Funetion
Slos ghouing yuncior

and ogln 0
A]s|23
Gensal Rde to cAnalye a Bogtom
I. Jhe suunning dine oc aach assigriment,
and utite atatements can wiually de take

ime ot a seqenee a stotmot


a. Jhe unning using the sumieule.
Bum iHule, y
can de ebtoined
dinie ef cônditiona-state
3. Jhe cunning

Jhe tine hot on 3, -thin-se csingtut is


4. conolition u cthe
the time to valuato the
Janjesi he dine nicded to he statomint
bea.cuute c when the conditin ste/ alse.
tinete ocecue a detp ds dhe eum gve
5..he

to wce cuto thu boly and dthe ine to


aualute the canitÑn ton tisination
Bracaclune Calls.
Recunste ogom
To conrpute n! yndion hact (ni imtegle) : integok
begin,
(D n<| then
fact :=1;
else
fact
enol;
R7. 0) and (ey 00)

Ihus Jo cseme costant c and d


c+TnzL), nzl

Ton) = 2c + Tn-2) A n72


’ Tn) = 3c + T(n-3) ), n3

Ta) = ic + Tn-i) 1 nzi


Firaly ten t=n-l, we qet
Finolly
Tn)= con-)+ T)
F.Cn-) + d

Tón) = O)
q/&] 23
Babie ta 8tuctwes
Btock
LIFO
A
FoA a gien stack S
en

itam i,s.pushl0; tu stock.


S. pop() peps alement out a

Amathematical wrpsesston
1-[{x* (e)/ (G-)f t /e-+3)
Ensuke:
I. Jhee ae an agual
atanthesis
Evehy ight pakontheik Jhas a matehing
Juht panthisia.
Valiol = tuue;
S= the empty atack ;
uhile ( not uad antiu sting)
uad dhe nct symbol (syb) e strng.

pdh s, syoib

volid -false
else f
i- popls) ;
valid - }alses

valid = talaej
ih Cvalid) s t ("Valid");
dlye putnt ("Anialio"):
Tmslenmentation & a Stack
# dalone STACKSIZE lo0
sut stack
nt top
jnt itoms[stA dkSIZE];

empty (rs)
stut so ck * s;

sutisen (Jsuue);
se
sulsn (false);
3
empty (Gs)
popls)
stuct stack * ps
}(emty (ps) )
baint | Stack empt, ")
ocit ()

ps-»top = ps top-;
3 sutwoi (po-tonotap +l))
Uhe: x = pop (4s)
pushlps
sttuct stack *ps;

, (ps’top == STACKs|ZE-))

oitu);
3
alse

A+B ’ Snhix
t AB’

AB+ ’ Postex
AB*C- Dt E/F | (G+H)

(atB) * C- (b-E))$ (F+G)


((AB+) *C - (DE-)) $(FG+)
’ ((AB+C+)- (oE-) )4 (FG +)
AB+C* DE-- FG+
’tAB) c -EDE))+FG)
’(*+ABC)-CrE) )$(+FG)
(-at ABC-DE) C+FG)
’ $-*t ABC - DE+ FG
Evalu ating Tosthx Fxkasion
Grdstk = the amsty stack,
while (mot end o put)
Symb =neet input ahata cto:
t (agrnb an tpekand.)
alse
puahl epetk,synb);
Spndz top (apndsth);
Spndi= pop (opndstk)
volue

push (opndstk, valua)


Postlix: G23+ - 3 82/ +*2 $3
anhix:6- (zt3)*(8'2 t3)$2 +3
Coneting to fostlix
Gpstk = the ompty stack;
lhile (not and input sting)
symb - nost input chasacto;
Gekandl)
odd symb tio pot),x stng
alse -

topsymb pop (opstk);


stbcktlast)
add tpsgb epostixsing

tapsymb = poplopstk);
add tosymb
l9ort
9-ot
mamoy
de seuse nst does method obeve The
-. te Aet is. eat Shitialy,
L9-hont
tt tems -q,.
c) (9. insect
[MAXQUEUE: tems it
queue stsuuot
kteat MAXQUEUE
l0O
kfront B
FIFo
A
Queues
<sl23
suct quul *i

pi 'qyua wndertao")
aiti);

== MAXQUEUE-)
ort -0;
Font tt;

Ünsent (pvÐ, x)
steuct quQue xP,; int j

sle

it U);

alinn,
3
Linkad List
stuuct rode
int into;
stuct node xvoct;

typede sat node xNoDErR.;


NoDEPIR utnade C)
NODEPTR fi
þ- (NorEPTR) mallec (sizee (sbuct nae ;

hanode (f)
NoDEPTR EP;

6/s/2s
Assume dhat p is a psiite vaniable thet pointy
a dist (i any) and to
to the yost node oef empty
NÚL ncase &
2quall NpL. dist. Sou have
ia a funchon [ SrsdhH O7. that. ins erte a
node wtth' an the ino ield ath he
ngde povtecd do by
tnsaltase (p. x)
NopEPTR ;
int c;

NOpE PTR
5l== NULL )
Juint,(Woid ingstion");

geiredat);
4>nt= þ n t
nct =

del altor le)


NODEPTR Pi

=NULL || -nt =-NL)


phint,("void datetion);,

NoDEPTR 4
Note,: Can stoe
9 not;
pnot 4¬nvot;
puanode Ca).
Queues al tista

NoDEPTR kont, Huat j


stut

sthuct qeue x PA int x;


NoDEPTR P;
-getnodet);
þ-hat NULL;

ta-ont =p;

(b4 a4)’net -P
3

stuct quaue *pi

( Voicl da lution, ueu Qmity")


Phint.
wet) le
NoDEPTR 4; intc;

tijiont
heenode (y);
(Pg- kont == NULL) puat =NULL;
3 etan(c
17/s|23
Some list oeatia
Let list' toint to oa gotud ist and is on
slemot to be insested wte gpeh poiion
List
HIHIO-H
Soted ist
place (list, )
NoDEPTR xpltsti

NODEPTR , i
9-NULL;
llp= *plist : pl=NuL 84 >pines

i, (4=-NULL)
pushl*Aist,
ale
x):
nsalin (, );
3
(wuLL) Hetisn
hetunn);
P)
NODEPTR
jntx, ustj DEPTR No
(list,
) Satch NoDEPTR
ist the ein nimbet letseasach knction
do Weta
to
t) -NULL nt (4-*plist, n
-p; *plist
"NULL) (*plit=
NOL', neete
getnodel); -
.y) NODEPTR
*}list; NODEPTR
(list,
x) Srsend
ist the
o
ndthe at Iumbes seed in nction Osiate
Wnit a huncitn to Jemove all ecouhencog

Humover (lst e)
NoDEpTR list; int j

NoDEPTR q
unt y:
4-NOLL; = * pist ;
hile (o!= NULL)

zonot,
l9,-= NUL)f
ynode bplist),
*pist <Pi
3
ahe
dialtin (9,4y);
P
Ciuculos dist Sast
node

nodlo
þnoct st node
ongty lpstack)
NODEPTR *pstack
utusn| (stock == NU) ?TRUE: FALsE)
3
push (pstack,, x)
NO DEFTR *pstack; int j

NODEPTR ;
þgetnode C);

due

4nit pstack)-noct;
*pstack)- nest -p;

NoDEPTR *pstack;
int , NCDEPTR -Þ;
t Cempty (pstack) ==TRUE
phinty statk wndslous"
cxit);
P= (*pstack)’ not,;
P inyo.
E} lp== * pstack )
*pstack = NULj
olee
pstackj-anet - nt,
Banode ();
setusen (c) ;
3
HW : Splomnt queue wsÊng cin cuda dinked lit
(empy, insett, emone)

Deubluy CiCular inked st

1
sttuct node
int ünto
stt nocde *at, xight;
dpecla stuet nocle noDEPTR;
ote a sasutine that deletes the node ipointee to
usy p in a dousy inked dist and atoeu ta
connt in ec.
.

NODEPIR i unt *px,

buont! (Voidl deletion ):


otu);
NODEPTR V,nt:

nect

lyrnooe bp);
ese ih ley== NULL)

alse Lnwct == NULL);

ueanoda p);

pHayrit nt,
3 Heanodelp):
Wsite a nction to anseut a noole with x to

insa,tt (p, x)
NoDE PIR P, int xc;

NoDEPTR 9,4j
9getnoce );

Hetun,
-3242197849l62.

324

G7694 |
Seasch
Jist Dejth
Bstoudes (c) Grsds (Besndee
B) : (A)
TrnsuLiang
Jhue. Einasay Iaysing
a
2aveL Contoins
compue n
awinodey nunbek
of Total o). Lul
hgdes atmat
Qm containl levelld,
it hodes
af m
at
dea ony drel Maximur Dpth-
tue
binasy Complete binay Sbidly
Jevel2
child at
d`ild
suight
Root
chleen. dioo
Je Binauy
inum max havea can node Each -
TREES
a4/*/23
A Beg dos -
..|. Visit the oot

3. Jhale tu riçhkt subleee in prestd.


B) Snsude
Jhawsse tu dt subbe in insdo
a Viuit the ot. 9,

3. Ihaverse the suight sublee jn ngds


) Postodey :
I. Jhavede the ut sublee in tastsadet.
a Iuaeuse the sight subieo n pastodo.
3. Visit the oot.

Prasade: AB DGCE HIF


Jnoude : DGBA. HEICE

Postoudes : GDB HIEFCA


H
K
(M

Basdest : ABDE CFIJH K L NM O


HNL KMO
Snendes: D BE A IFJC
frsterdor: DEBITE NL OMK HCA

Sronds: A+(B-c) +(p$(E*F) ) ’ini ’

Baeends: +A* -Bc$D¥EF


Pestohc: AC-pEF* $*+*t pcathx
stuet nodcbgpe
cuct nodetye dit s

typeo, stuct nodetye *nonEPTR;


NoDEPTR maketeel)
NODEPTR t;
NoDE PTR ;
patrt (id insertio
let NLL; phint}('hvaid inetin
sight =NULL else
satwn lp);
3
Binasuy hae ravssal.

stack.
itav (tue)
NODEPTR t e

stsuct stoack ?

NopEPTR tem [MASTACK:


Js;
NonEPTR ; s. top =-1;

do
whielpl- NUL)
pushls);

3
ethav ltce)
NODEPTR te:
stuct stack S

NODE PTRtem [MANSTACK];

NoDE PTR , stop=-l;


=tie;
olo

while (p!=NULL)

push lp,s),;

t-psaiht ;
aatiovac (te)
NODEPT RJee;

Ltue =NULL )
etasn;

sulsan ec tee-> Jat);


yatoy. suight);
3
ünteovec (tee)
NopEPIR tee;
NUL)
Jitien j
inthav-ec( taee t).
pinty tRL-in}o);

tn-thseadad
Binay he
Betheaadlol

Pest-thecacd
binesu tiee

Snga-Rght Thunold Biray Thee.


suuct nodethype
suct noditype alt;
stuct nadletpe *ight ;
int sthead:

FlAg ntheuod TRUE when the iqht ohid


yinting ts a
a saht clel. FAISE
inte ynot
othewiae.
iithav- wWcthsuac( BThe)
NODEPTR BIe;

NODEPTR , )

de
9,= NULL;
while (pl= NULL)

f-yight,

p=fosigt;

whilels! NULL);
3\]8|23
Binasay Seach Juee
J4

20

(6

Snoden traveual qiveL soutd liut.


NOEPTR maketuu()

NODEPTR J;
- getnocle();
Pin7e =x;
dat = NULL;
ight =NULL;
stid:TRUE ;
tutnl);

setleht bp,e)
NoDEPTR ;int ,
(p==NULL |lp-let|=NULL)
i} pintf(Vaid insetisn);
wcit);
3
NODEPTR q,= makete (e);

setsight (p, «)
NoDEPTRD; int )

paint ("Void inssution");


eritli);

4l!pdtoad )
tkintf ("Voidl insenton);

NODEPTR ,
e9, <maketu(x):

thuod =FALSE;
GeneclIee
Abe is a tinit non-emst set o aomt in
uhich en elemnt k called theut and the
duaining eemeit ae tatitioned into mzo
diyoint seta, cach a which itselk a taee.
Begus a a nodle - No. e childen that node.
An sdesud te is dlined ak ate in which
sot.
the subtes de tom an hdeed
ah no oe
The linst child & ahode in an hdeuedcidtae is
dten callad dhe ddst hild. Jha Jost e
callad the goungest
A\oest

-yungest
olelest ’

J-yungut
ololest
O alolest

Fsest
Ti
An ohdeed t eis called an hchac.

+dafine MAXCH|LD 50
stuct unooe

trut tnade. *child [MAxCHILD];


T CGnstaint by MAxcHUL)
stBut tuenode

sauet tuenode *cld;


stut unode nut;
3tipecde stauct trenode * NODEPTR
Dynamic ollocaton
A
Ih

setohiloen lb, dist.)


NopE PTRp
NODE PTR Jist

eit i);
3
.hild=l'st
addlchild(b
NODEPTRp; int x;
NoDEPT R, 9;
}(==NULL)
acit u)
9= pchild;
NULLj
uwhie la, !=NULL)te

4. =9nont;
4-getnocdel );
'q, net -NULL;
}lNULL)
pchild=4j
elhe
net

a Gensal Jhe
Paedes : I. Fust visit uthe noSt eh the jst
ttee in a joest. ubtees
a.Then taesue dthe oest oh
the jiust tue nkesadea.
3. Then tuavee the olst the
B
D

Byerde::A BELMNOF CGHIPDTKaR

Tngile:
tees not on inoceo
a. Visit the aoot oh the hit ee
3. Thn dFayesge dhe
subtees un inado.:

LEMNO BFAGCHPIJ DQKR


foust subtua!
Pestoude: 1. Fisst taanese the postsa'
o tha iutt ree n
in pstoide .
3. Vst dhu sogt iist tee .
hon Jus:LM NOEFBA HPI CTaRKDA
4'y2s Fusads Shosal a foest
1. Qaut the Bhchand ot t the lst child' e aach
veer is Bmmediatelyy belew the vete
a has a vetical donk tom uach vetr to ats jist
child aned dhou. houzontal ink yom cach vete te
Jtt net stbling
3. Rune the nemaining tigial inks
4. Rotata the digam 45° dadwie o tht
dhe vesticaL dihs appeot as daft dnke
and hsizontal dinh appean ad sighdt din ke.
(A

Convested
achod
to a
Busdo: ABCDE FGTK H

E A

(H
Ealuodion tunction: he no: seus, columns
and diagpral bamaining pen Joen one laye miny
the numbe seamaining then ore i ponant.

3-, 3-%
=
Best
value
gthe
/Moves
cee

8l4<a3

chas bsakd [ J ] : asigng


nt ten;
stunt noddtype: *child,;
cuct snocdtyje *neet;

Set
Operotians
1. MakeNull (s) 7. Union (A, B, c)
seMembels, c) C= AUB
8. Sntssection (A,B,c)
3. Snsent (8, x)
4. Dalet (s,) 4. Dilfoena (A,B,C)
5. Min (s) =A-B
6. Max (S)
Btt Vacte Smplementotisn
int s[ioo;
Make Null (s)
Be (i-lj i< MAXSIZE; t+)
sL]= FALSEJ
3
Jnsest (s, x) Qelot Cs, )
s[]=TRUE; sl-FASE;
AsMembes4 S )
hatsn s];
Union (A, B, C)

Sntesaection (A, B, )
o4 li=0) i<MAXS IZE ; +)

Dil (A, B, c)
en li=0; iKMAXSIZE; it+)
# deline MAXSIZE I00
typedl studt set

nt dast:

MakENull (s) tnt aMemkes Cs,)


setpis; setstu S; int;
Sast =-l; inti, oR j
i=o; eh=0;
Qdlete (s, x) hile ( is shst b ITRLE)
setus; trdige xc;
alse
nt i, Jound; 3 tttj
i-0;4oundD;

slost-j
3else
Ansert (s, x)

while (is s slost 64 Itend )


Csslneli]-=x)
J

S>lasttt; chek lo

AsMembe s,«)
int 4, s, mi }eund;

jund=l;
se
Serting.
Bubble set
Bubble (a,n)
int al1:ivt nj

. (int i=0j. i<n-;i+t)


std= i:

flali+j< alj)
S:seted =0;
int tmpa;+J,
otj] temP
i (stcl)
bsak;
3
Seluction St
Rash dun sod
selectienthase in which the
Contists ol a aament
phgten postbion
5, 2, 12, 100, &0, 75
5, 2, I2, 0, 75, 100
5, 2, 12, 75, %0, |003x
R, 5, 2, 15, 80, 100

Selechion (x,n)
tnt cL1,n;

le(int i=0; ik nl; itt


int moc ='-|ind =-l;

imx=eL;];

Bwap (*[ndl],n-i-l);
3
1s]|23
Soding
Exchange Divide &"s.
Rule set
O7) Olnbgn)
Guick Set

1o), 2., 7, A3, 34, 8, ,4,10,5

Q1,8,1,1,5\(00) >4,1|

tyneda, suct Jist


int count
tentype entey LiAxLISTI;
3Jist typej
int Posdition (List tpe *p, int lou, int high
ivt i, pivdtioc ;
key-tye pavottey
suap(dau, lousthig, lp);
pivetlec = lu;
loli ndeasl; iehigh ; i++)
sunpltt pivotlec, I, Jp)
Swap(touw, votec,p);
sutisn pivet lsc,
Vsid set ist tpe *dp, nt doo , int
hish)
int pivetlsc;

sert (p ivct lac tl,; high);

ist stye *Guick (Jist.tape nlp)


, dp > count -);

You might also like