《数据结构与算法》课后答案_第1页
《数据结构与算法》课后答案_第2页
《数据结构与算法》课后答案_第3页
《数据结构与算法》课后答案_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1.FilltheblankwithcorrectC++codes:

Givenanarraystoringintegersorderedbydistinctvaluewithoutduplicate,modifythebinary

searchroutinestoreturnthepositionoftheintegerwiththesmallestvaluegreaterthanKwhenK

itselfdoesnotappearinthearray.ReturnERRORifthegreatestvalueinthearrayislessthanK:

(12scores)

//Returnpositionofsmallestclement>=K

intnewbinary(intarray[],intn,intK){

intl=-l;

intr=n;//1andrbeyondarraybounds

while(1+1!=r){//Stopwhen1andrmeet

___inti=(1+r)/2;//Lookatmiddleofsubarray

if(K<array[i])_r=i—;//Inlefthalf

if(K==array[i])_returni__;//Foundit

if(K>array[i])___l=i___//Inrighthalf

)

//KisnotinarrayorthegreatestvalueislessthanK

ifK<=array[n-lJ(orr!=n)_//thegreatestvalueinthearrayisnotlessthanKwithr

updated

returnr;//whenKitselfdoesnotappearinthearray

elsereturnERROR;//theintegerwiththegreatestvaluelessthanK

(2)Theheightofacompletebinarytreewithknodesis」log?(k+l)|[1nodetree

hashight1)

(3)Thenumberofdifferentshapesofbinarytreeswith5nodesis_42_.

2.AcertainbinarytreehasthepreorderenumerationasABECDFGHIJandthe

inorderenumerationasEBDCAFIHGJ.Trytodrawthebinarytreeandgivethe

postorderenumeration.(Theprocessofyoursolutionisrequired!!!)

3.Determine@forthefollowingcodefragmentsintheaveragecase.Assumethat

allvariablesareoftypeint.

(1)sum=0;

for(i=0;i<5;i++)

for(j=0;j<n;j++)

sum++;solution:©___(n)

(2)sum=0;

for(i=l;i<=n;i++)

for(j=n;j>=i;j-)

sum++;solution:®_(n2)

(3)sum=0;

if(EVEN(n))

for(i=0;i<n;i++)

sum++;

else

sum=sum+n;solution:®___(n)

4.Tracebyhandtheexecutionofcreationabinarysearchtreewiththeinputsequence

as:{46,25,78,62,12,37,70,29}whichisemptytreeinitially.

Solution:BSTobtainedwithdatainsertedonebyone

46

2578

123762

2970

5.Designanalgorithmtotransferthescorereportfrom100-pointto5-point,thelevel

Ecorrespondingscore<60,60〜69beingD,70〜79beingC,80〜89asB,score>=90

asA.Thedistributiontableisasfollowing.Pleasedescribeyouralgorithmusinga

decisiontreeandgivethetotalpathlength.

Scorein100-point0-5960-6970-7980-8990-100

Distributionrate|5%10%45%25%15%

solution:

thedesignlogicistobuildaHuffmantree

Totallength:4+4+3+2+1=14,Averagelength:4*5%+10%*4+15%*3+25%*2+

45%=2.00,the0-false,1-trueasthelogicbranches.

6.Assumeadiskdriveisconfiguredasfollows.Thetotalstorageisapproximately

1.35Gdividedamong15surfaces.Eachsurfacehas612tracks;thereare144

sectors/track,1024byte/sector,and16sectors/cluster.Theinterleavingfactorisfour.

Thediskturnsat7200rmp(8.33ms/r).Thetrack-to-trackseektimeis20ms,andthe

averageseektimeis80ms.Nowhowlongdoesittaketoreadallofthedataina360

KBfileonthedisk?Assumethatthefile'sclustersarespreadrandomlyacrossthe

disk.AseekmustbeperformedeachtimetheI/Oreadermovestoanewtrack.Show

yourcalculations.(Theprocessofyoursolutionisrequired!!!)

Solution:

Thefirstquestionishowmanyclustersthefilerequires?

Aclusterholds16*1K=16K.Thus,thefilerequires360/16=22.5

clusters=22completeclusterand8k(8sectors)

Thetimetoreadaclusterisseektimetothe

cluster+latencytime+(interleaffactorXrotationtime).

Averageseektimeisdefinedtobe80ms.Latencytimeis0.5*8.33ms(60/7200q

8.33ms),andclusterrotationtimeis4*(16/144)*8.33.

Seektimeforthetotalfilereadtimeis

22*(80+0.5*8.33+4*(16/144)*8.33)+(80+0.5*8.33+4*(8/144)*8.33尸

2019.095ms

7.Usingclosedhashing,withdoublehashingtoresolvecollisions,insertthe

followingkeysintoahashtableofelevenslots(theslotsarenumbered0through10).

ThehashfunctionstobeusedareH1andH2,definedbelow.Youshouldshowthe

hashtableafteralleightkeyshavebeeninserted.Besuretoindicatehowyouare

usingHlandH2todothehashing.(Theprocessofyoursolutionisrequired!!!)

Hl(k)=3kmod11H2(k)=7kmod10+1

Keys:22,41,53,46,30,13,1,67.

Solution:

Hl(22)=0,Hl(41)=2,Hl(53)=5,Hl(46)=6,noconflict

WhenH1(30)=2,H2(30)=1(2+1*1)%11=3,so30entersthe3rdslot;

Hl(l3)=6,H2(l3)=2(6+1*2)%11=8,so13entersthe8thslot;

Hl(1)=3,H2(1)=8(3+5*8)%ll=10so1enters10(passby0,8,5,2);

H1(67)=3,H2(67)=10(3+2*10)%11=1so67entersl(passby2)

226741305346131

012345678910

8.Youaregivenaseriesofrecordswhosekeysareintegers.Therecordsarriveinthe

followingorder:C,S,D,T,A,M,P,I,B,W,N,G,U,R.Showthe2-3treethat

resultsfrominsertingtheserecords,(theprocessofyoursolutionisrequired!!!)

Solution:

9.

Thefollowinggraphisacommunicationnetw

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论