Sie sind auf Seite 1von 6

DataStructures

Attheheartofvirtuallyeverycomputerprogramareitsalgorithmsanditsdatastructures.Itishardtoseparate
thesetwoitems,fordatastructuresaremeaninglesswithoutalgorithmstocreateandmanipulatethem,and
algorithmsareusuallytrivialunlesstherearedatastructuresonwhichtooperate.
Thiscategoryconcentratesonfourofthemostbasicstructures:stacks,queues,binarysearchtrees,andpriority
queries.Questionswillcoverthesedatastructuresandimplicitalgorithms,notonimplementationlanguage
details.Astackisusuallyusedtosaveinformationthatwillneedtobeprocessedlater.Itemsareprocessedin
alastin,firstout(LIFO)order.Aqueueisusuallyusedtoprocessitemsintheorderinwhichrequestsare
generated;anewitemisnotprocesseduntilallitemscurrentlyonthequeueareprocessed.Thisisalsoknown
asfirstin,firstout(FIFO)order.Abinarysearchtreeisusedwhenoneisstoringasetofitemsandneedsto
beabletoefficientlyprocesstheoperationsofinsertion,deletionandquery(i.e.findoutifaparticularitemis
partofthesetandifnot,whichiteminthesetisclosetotheiteminquestion).Apriorityqueueisusedlikea
binarysearchtree,exceptonecannotdeleteanarbitraryitem,norcanonemakeanarbitraryquery.Onecan
onlydeletethesmallestelementoftheset,andcanonlyfindoutwhatisthesmallestelementoftheset.
Astacksupportstwooperations:PUSHandPOP.AcommandoftheformPUSH(A)putsthekeyAatthe
topofthestack;thecommandPOP(X)removesthetopitemfromthestackandstoresitsvalueintovariable
X.Ifthestackwasempty(becausenothinghadeverbeenpushedonit,orifallelementshasbeenpoppedoff
ofit),thenXisgiventhespecialvalueofNIL.Ananalogytothisisastackofbooksonadesk:anewbookis
placedonthetopofthestack(pushed)andabookisremovedfromthetopalso(popped).Sometextbookscall
thisdatastructureapushdownstackoraLIFOstack.
Queuesoperatejustlikestacks,exceptitemsareremovedfromthebottominsteadofthetop.Agoodphysical
analogyofthisisthewayatrainconductorornewspaperboyusesacoinmachinetogivechange:newcoins
areaddedtothetopsofthepiles,andchangeisgivenfromthebottomofeach.Sometextbooksrefertothis
datastructureasaFIFOstack.
Considerthefollowingsequenceof14operations:
PUSH(A),PUSH(M),PUSH(E),POP(X),PUSH(R),
POP(X),PUSH(I),POP(X),POP(X),POP(X),
POP(X),PUSH(C),PUSH(A),PUSH(N)
Iftheseoperationsareappliedtoastack,thenthevaluesofthepopsare:E,R,I,M,AandNIL.Afterallofthe
operations,therearethreeitemsstillonthestack:theNisatthetop(itwillbethenexttobepopped,ifnothing
elseispushedbeforethepopcommand),andCisatthebottom.If,insteadofusingastackweusedaqueue,
thenthevaluespoppedwouldbe:A,M,E,R,IandNIL.Therewouldbethreeitemsstillonthequeue:Natthe
topandConthebottom.Sinceitemsareremovedfromthebottomofaqueue,Cwouldbethenextitemtobe
poppedregardlessofanyadditionalpushes.
Abinarysearchtreeiscomposedofnodeshavingthreeparts:information(orakey),apointertoaleftchild,
andapointertoarightchild.Ithasthepropertythatthekeyateverynodeisalwaysgreaterthanorequaltothe
keyofitsleftchild,andlessthanthekeyofitsrightchild.ThefollowingtreeisbuiltfromthekeysA,M,E,R,
I,C,A,Ninthatorder:

A
M

A
E

TherootoftheresultingtreeisthenodecontainingthekeyA;notethatduplicatekeysareinsertedintothetree
asiftheywerelessthantheirequalkey.Thetreehasadepth(sometimescalledheight)of3becausethe
deepestnodeis3nodesbelowtheroot.Nodeswithnochildrenarecalledleafnodes;therearefourofthemin
thetree:A,C,IandN.Anexternalnodeisthenamegiventoaplacewhereanewnodecouldbeattachedto
thetree.Inthefinaltreeabove,thereare9externalnodes;thesearenotdrawn.Thetreehasaninternalpath
lengthof15:thesumofthedepthsofallnodes.Ithasanexternalpathlengthof31:thesumofthedepthsof
allexternalnodes.ToinserttheN(thelastkeyinserted),3comparisonswereneeded:againsttherootA,theM
andtheR.
Toperformaninordertraversalofthetree,recursivelytraversethetreebyfirstvisitingtheleftchild,thenthe
root,thentherightchild.Inthetreeabove,thenodesarevisitedinthefollowingorder:A,A,C,E,I,M,Nand
R.Apreordertravel(root,left,right)visitsinthefollowingorder:A,A,M,E,C,I,RandN.Apostorder
traversal(left,right,root)is:A,C,I,E,N,R,M,A.Inordertraversalsaretypicallyusedtolistthecontentsof
thetreeinorder.
Binarysearchtreescansupporttheoperations:insert,deleteandsearch.Moreover,ithandlestheoperations
efficiently:inatreewith,say,1millionitems,onecansearchforaparticularvalueinaboutlog2100000020
steps.Itemscanbeinsertedordeletedinaboutasmanysteps,too.However,binarysearchtreescanbecome
unbalanced,ifthekeysbeinginsertedarenotprettyrandom.Forexample,considerthebinarysearchtree
resultingfrominsertingthekeysA,E,I,O,U,Y.Sophisticatedtechniquesareavailabletomaintainbalanced
trees.Binarysearchtreesaredynamicdatastructures:theycansupportanunlimitednumberofoperations,
andinanyorder.
Tosearchforanodeinabinarytree,thefollowingalgorithm(inpseudocode)isused:
p=root
found=FALSE
loopwhile(pNIL)and(notfound)
if(x<pskey)thenp=psleftchild
elseif(x>pskey)thenp=psrightchild
elsefound=TRUE
repeat

Deletingfromabinarysearchtreeisabitmorecomplicated.Thealgorithmwelluseisasfollows:
p=nodetodelete
f=fatherofp
if(phasnochildren)then
deletep
elseif(phasonechild)then
makepschildbecomefschild
deletep
else(phastwochildren)
l=psleftchild(itmightalsohavechildren)
r=psrightchild(itmightalsohavechildren)
makelbecomefschildinsteadofp
stickrontotheltree
deletep
end
Thefollowingthreediagramsillustratethealgorithmusingthetreeabove.Attheleft,wedeleteI(nochildren);
inthemiddle,wedeletetheR(onechild);andattheright,wedeletetheM(twochildren).
A

A
M

A
E

I
N

Apriorityqueueisquitesimilartoabinarysearchtree,butonecanonlydeletethesmallestitemandsearch
forthesmallest.Theseoperationscanbedoneinaguaranteedtimeproportionaltothelogofthenumberof
items.Onepopularwaytoimplementapriorityqueueisusingaheapdatastructure.Aheapusesabinary
tree(thatis,atreewithtwochildren)andmaintainsthefollowingtwoproperties:everynodeislargerthanits
twochildren(nothingissaidabouttherelativemagnitudeofthetwochildren),andtheresultingtreecontains
noholes.Thatis,alllevelsofthetreearecompletelyfilled,exceptthebottomlevel,whichisfilledinfrom
thelefttotheright.
Youmaywanttostopforamomenttothinkabouthowyoumightmakeanefficientimplementationofa
priorityqueue.
Thealgorithmforinsertionisnottoodifficult:putthenewnodeatthebottomofthetreeandthengoupthe
tree,makingexchangeswithitsparent,untilthetreeisvalid.ConsiderinsertingCintothefollowingheapthat
hasbeenbuiltbyinsertingA,M,E,R,I,C,A,N:

Thesmallestvalueisalwaystheroot.Todeleteit(andonecanonlydeletethesmallestvalue),onereplacesit
withthebottommostandrightmostelement,andthenwalksdownthetreemakingexchangeswiththechildin
ordertoinsurethatthetreeisvalid.Thefollowingpseudocodeformalizesthisnotion:
b=bottommostandrightmostelement
p=rootoftree
pskey=bskey
deleteb
loopwhile(pislargerthaneitherchild)
exchangepwithsmallerchild
p=smallerchild
repeat

BUILDINGAHEAPFROMA,M,E,R,I,C,A,N:

A
M

A
A

A
E

I
R

E
R

References
Amberg,Wayne.DataStructuresfromArraystoPriorityQueues,Wadsworth(1985).
Bentley,Jon.Thanks,HeapsinProgrammingPearls,CommunicationsoftheACM,Vol.28,No.3,March
1985.
Sedgewick,Robert.Algorithms,AddisonWesley(1983),Chapters11and14.
Wirth,Niklaus.DataStructuresandAlgorithmsinScientificAmerican,September1984,pp.6079.

SampleProblems
Thestatementspush(p,q)andpop(p,q)handletwo
parallelstacks.Thepushputsponthetopofone
stackandtheqonthetopofthesecondstack.The
popcommanddoesapopofthefirststackandputsthe
valueintop,andapopofthesecondstackandputs
thevalueintoq.Forexample,
push(4,6)
push(2,5)
pop(a,b)
pop(c,d)
wouldresultina=2,b=5,c=4,d=6.
Considerthefollowingoperationsonaninitially
emptystack:
push(40,10)
push(35,3)
push(12,20)
pop(a,b)
pop(a,b)
push(10,23)

Thefirstpopputsa=12andb=20;thesecondpopset
a=35andb=3.Thepush(10,23)wouldcausethenext
poptoseta=10andb=23.Thewordingofthe
problemindicatesthataisthefirststackandbthe
second.

Whatwouldbethenextitemremovedfromthefirst
stack?

Whichofthefollowingbinarytreesarevalidbinary
searchtrees?(Emptynodesarenotdrawn.)
AB
J
A

CDE
B

S
W

Becareful!Abinarytreeisatreewhereeachnode
hasatmost2children.Abinarysearchtreeisabinary
treewiththeadditionalpropertythattheletterofeach
nodeisgreaterthanthevalueofitsleftchild,andless
thanthevalueofitsrightchild.Thevalidtreesare:
(a),(b),and(d).

Ifonetraversedthefollowingtreeinpreorder(visit
theroot,andtheneachofthesubtreesfromleftto
right),inwhatorderwouldnodesbevisited?Besure
youranswerisneatandclear!
A
C

Answer:ABDEFHICG
Acommonmistakeisnottorecursivelyvisitallnodes
ineachsubtree.

Das könnte Ihnen auch gefallen