數(shù)據(jù)結(jié)構(gòu)例題解析_第1頁
數(shù)據(jù)結(jié)構(gòu)例題解析_第2頁
數(shù)據(jù)結(jié)構(gòu)例題解析_第3頁
數(shù)據(jù)結(jié)構(gòu)例題解析_第4頁
數(shù)據(jù)結(jié)構(gòu)例題解析_第5頁
已閱讀5頁,還剩3頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

..ISingleChoice(10points)1.(a)Forthefollowingprogramfragmenttherunningtime(Big-Oh)is.i=0;s=0;while(s<(5*n*n+2)){i++;s=s+i;}a.O(n)b.O(n2)c.O(n1/2)d.O(n3)2.(c)Whichisnon-lineardatastructure_____.a.queueb.stackc.treed.sequencelist3.(b)Theworst-timeforremovinganelementfromasequencelist(Big-Oh)is.a.O(1)b.O(n)c.O(n2)d.O(n3)4.(d)Inacircularqueuewecandistinguish(區(qū)分)emptyqueuesfromfullqueuesby.a.usingagapinthearrayb.incrementingqueuepositionsby2insteadof1c.keepingacountofthenumberofelementsd.aandc(b)Arecursivefunctioncancauseaninfinitesequenceoffunctioncallsif.theproblemsizeishalvedateachsteptheterminationconditionismissingnousefulincrementalputationisdoneineachsteptheproblemsizeispositive6.(c)Thefullbinarytreewithheight4hasnodes.a.15b.16c.31d.327.(b)Searchinginanunsortedlistcanbemadefasterbyusing.binarysearchasentinel〔哨兵〕attheendofthelistlinkedlisttostoretheelementsaandc8.〔b〕Supposethereare3edgesinanundirectedgraphG,IfwerepresentgraphGwithaadjacencymatrix,Howmany"1〞sarethereinthematrix"a.3b.6c.1d.99.(d)ConstructaHuffmantreebyfourleafwhoseweightsare9,2,5,7respectively.Theweightedpathlengthis___________.a.29b.37c.46d.4410.Considerthefollowingweightedgraph.ConsiderDijkstra’salgorithmonthisgraphtofindtheshortestpathswithsasastartingvertex.Whicharethefirstfourverticesextractedfromthepriorityqueuebythealgorithm(listedintheordertheyareextracted)"a.s,y,t,xb.s,y,x,zc.s,t,y,xd.s,y,x,tFig.111.Hereisanarrayoftenintegers:5389170264Supposewepartitionthisarrayusingquicksort'spartitionfunctionandusing5forthepivot.Whichshowsthearrayafterpartitionfinishes:a.5342107968b.0342157968c.3102458967d.3102458976e.NoneoftheaboveIIFillinBlank(10points)1.Forthefollowingprogramfragmenttherunningtime(Big-Oh)isO(n2).for(inti=0;i<n;i++)for(intj=0;j<=i;j++)s;//s為某種根本操作2.Westorea4×4symmetricmatrixAintoanarrayBwithrowmajororder,Storethelowertriangleonly.theindexofelementa[2][3]inBis6.3.Wecanuse3vectortypetostorevalueandofnon-zeroelementsinasparsematrix.4.A______stack______isalistwhereremovalandadditionoccuratthesameend.FrequentlyknownaLIFO(Last-In-First-Out)structure.T(n)=2T(n/2)+,T(n)=O(logn)T(n)=T(n-1)+,T(n)=O(_____n_____)6.Thereisabinarytreewhoseelementsarecharacters.Preorderlistofthebinarytreeis"ABECDFGHIJ〞andinorderlistofthebinarytreeis"EBCDAFHIGJ〞.PostordertraversalsequenceofthebinarytreeisEDCBIHJGFA.7.Thereare(n+1)/2leafnodesinafullbinarytreewithnnodes.8.Whentheinputhasbeensorted,therunningtimeofinsertionsort(Big-Oh)isO(n).9.Wesortthesequence〔43,02,80,48,26,57,15,73,21,24,66〕withshellsortforincrement3,theresultis______(1502212426574366804873)_.10、Inacircularqueue,"front〞and"rear〞arethefrontpointerandrearpointerrespectively.Queuesizeis"maxsize〞.Wheninsertanelementinthequeue,rear=__(rear+1)%maxsize__11.A_________________B樹_____________________isanexampleofasearchtreewhichismultiway(allowsmorethantwochildren).12.Atreeinwhicheverynodeisnosmallerthanitschildrenistermed_____大頂堆______.IIIApplicationofAlgorithms〔35points〕1.GraphGshowninFig2isadirectedgraph,pleasedescribeGwithadjacencymatrixandwritetheordersofbreadthfirsttraversalanddepthfirsttraversal.Fig.2ABCDEA01010B00110C00001D00001E00000Dft:ABCEDBft:ABDCE2.Thesequenceofinputkeysisshownbelow:19,1,23,14,55,20,84,27,68,11,10,17Afixedtablesizeof19andahashfunctionH(key)=key%13,withlinearprobing(線性探測(cè)),fillthetablebelowandputetheaveragelengthofsuccessfulsearch.3.Showtheresultsofinserting53,17,78,09,45,65,87each,oneatatime,inainitiallyemptymaxheap〔大根堆〕4.writethesequenceofpreorder,postordertraversalsandaddinorderthreadsinthetree.Fig.35.BuildaHuffmantreeanddetermineHuffmancodewhentheprobabilitydistribution(概率分布)overthe8alphabets(c1,c2,c3,c4,c5,c6,c7,c8)is(0.05,0.25,0.03,0.06,0.10,0.11,0.36,0.046.GraphGshowninFig4isadirectedgraph,pleasedescribeGwithadjacencylistandwritetopologicalordering.Fig.4IVFillinblankofalgorithms.〔15〕HereissinglesourceshortestpathalgorithmDijkstra.Fillinblankofthealgorithm.classGraph{//圖的類定義private:floatEdge[NumVertices][NumVertices];floatdist[NumVertices]; //最短路徑長(zhǎng)度數(shù)組 intpath[NumVertices]; //最短路徑數(shù)組 intS[NumVertices]; //最短路徑頂點(diǎn)集 public:voidShortestPath(int,int);intchoose(int);};voidGraph::ShortestPath(intn,intv){//在具有n個(gè)頂點(diǎn)的帶權(quán)有向圖中,各邊上權(quán)值由Edge[i][j]給出。建立一個(gè)數(shù)組:dist[j],0j<n,//保存從頂點(diǎn)v到頂點(diǎn)j的最短路徑長(zhǎng)度,同時(shí)用數(shù)組path[j],0j<n,存放求到的最短路徑。for(inti=0;i<n;i++){dist[i]=Edge[v][i];//dist數(shù)組初始化S[i]=0; if(i!=v&&dist[i]<MAXNUM)path[i]=v;elsepath[i]=-1; //path數(shù)組初始化 }S[v]=1;dist[v]=0; //頂點(diǎn)v參加頂點(diǎn)集合//選擇當(dāng)前不在集合S中具有最短路徑的頂點(diǎn)ufor(i=0;i<n;i++){ floatmin=MAXNUM;intu=v;for(intj=0;j<n;j++) if(!S[j]&&dist[j]<min) {u=j;min=dist[j];}S[u]=1; //將頂點(diǎn)u參加集合Sfor(intw=0;w<n;w++)//修改if(!S[w]&&Edge[u][w]<MAXNUM&&dis[w]>(min+Edge[u][w])){dist[w]=min+Edge[u][w];path[w]=u;}}}3.Considerthefollowingfunctiontobalancesymbolsstoredinstringexpthatincludesparentheses〔圓括號(hào)〕andnumbers.PleaseFillinblank.#include<stack>Usingnamespacestd;intmatching(string&exp){//expisapointertoastringtocheckintstate=1,i=0;chare;stack<char>s;while(i<exp.length()&&state)switch(exp[i]){case'(':s.push(exp[i]);i++;break;case')':if(!s.empty()&&s.front()==’(’){ s.pop();i++;}else state=0;//anerroroccursbreak;default:i++;break;}//endofwhileif(state&&s.empty())return1;elsereturn0;VProgramming〔30〕1.Writeefficientfunctions(andgivetheirBig-Ohrunningtimes)thattakeapointertoabinarytreerootTandpute:ThenumberofleavesofTtypedefstructBiTNode{TElemTypedata;structBiTNode*lchild,*rchild;}BiTNode,*BiTree;2.WriteamethodcalledmaximumDegreeofanundirectedhgraphthatreturnsthemaximumdegreeofanyvertexinthegraph.Thegraphisstoredwithadjacencymatrix.Writethedefinitionofthegraph.implementthefunction.Analyzespaceplexityandtimeplexityofyourfunction.3.Writeafunctionwithlinkedlistthatinsertsanumberintoasortedlinkedlist.Firstly,youshouldwriteafunctioncreatesalistthatlikethis:L={3,5,8,12,32,48}andtheninsert25intothislist.答案解析0-0,僅供參考,假設(shè)有不同意見請(qǐng)聯(lián)系QQ767954870☆_☆選擇題:1-5:ACBDB6-11:CBBDDE知識(shí)點(diǎn):復(fù)雜度分析,必考思路:復(fù)雜度主要計(jì)算算法的步數(shù),可以看出,當(dāng)前循環(huán)執(zhí)行的步數(shù)與i的值是相等的,所以可列1+2+..+i=(5*n*n+2),復(fù)雜度的計(jì)算忽略常數(shù),(1+i)*i/2=(5*n*n+2),i~O(n)知識(shí)點(diǎn):non-linear與linear的區(qū)別知識(shí)點(diǎn):復(fù)雜度分析+線性序列思路:很顯然,當(dāng)元素在sequencelist的末尾的時(shí)候,removing元素復(fù)雜度最高O(n)4、知識(shí)點(diǎn):循環(huán)隊(duì)列(circularqueue),重點(diǎn)思路:主要區(qū)分循環(huán)隊(duì)列判斷空與滿的條件。主要有三個(gè)方法count計(jì)數(shù),判斷當(dāng)前隊(duì)列的元素與maxsize的大小vis標(biāo)志,比方可以一開場(chǎng)設(shè)vis=0,滿時(shí)設(shè)置vis=1就是題目中說的gap(....重點(diǎn))front代表頭指針,rear代表尾指針循環(huán)隊(duì)列空時(shí):front==rear;滿時(shí):front==(rear+1)%maxsize知識(shí)點(diǎn):遞歸的定義,terminationmissing,終止條件缺失那么可能無限調(diào)用。知識(shí)點(diǎn):完全二叉樹(pletebinarytree)與滿二叉樹(fullbinarytree)的區(qū)別思路:學(xué)院PPT上有如下定義depthofanode:numberofancestorsheightofatree:maximumdepthofanynode并且有結(jié)點(diǎn)計(jì)算公式:2h+1-1(其中h為樹的高度,與某XX百度定義樹的高度 不一樣,且照學(xué)院教材來做==)所以ans:24+1-1=31知識(shí)點(diǎn):查找思路:有疑問的題...單純來說二分查找(binarysearch)的速度O(logN)是比擬快的,可是題目?jī)H僅要求 Searchinginanunsortedlist,只進(jìn)展一次查找,那我們用二分還要先進(jìn)展排序 O(NlogN)+O(logN)的復(fù)雜度是不如選項(xiàng)b的。sentinel(哨兵...)的概念可見ppt講插入排序的地方,貌似能加快查找速度吧...知識(shí)點(diǎn):圖的鄰接矩陣存儲(chǔ)思路:注意題目所問,無向圖(undirectedgraph),每條邊都是要存儲(chǔ)兩遍的知識(shí)點(diǎn):哈夫曼樹(Huffmantree)思路:離散上學(xué)過的。。。weightedpathlength=所以ans=9*1+7*2+5*3+2*3=44(自己構(gòu)造哈夫曼樹"。")知識(shí)點(diǎn):Dijkstra/最短路,重點(diǎn)知識(shí)點(diǎn):快排,重點(diǎn)10、11兩題是重點(diǎn),限文字難于描述清楚,請(qǐng)自主學(xué)習(xí)%>_<%注意10題在priority_queue里進(jìn)展更新時(shí)一開場(chǎng)肯定參加s、y結(jié)點(diǎn),而后x結(jié)點(diǎn)會(huì)因 為松弛操作從而距離變?yōu)?+3=4<5(t結(jié)點(diǎn)),所以x結(jié)點(diǎn)會(huì)比t結(jié)點(diǎn)先壓入隊(duì)列。填空題O(n2)6數(shù)組元素存儲(chǔ)地址的計(jì)算。注意題目中規(guī)定存儲(chǔ)下三角矩陣lowertriangleonlylocation在稀疏矩陣中sparsematrix,如果對(duì)每個(gè)元素都進(jìn)展存儲(chǔ)的話空間復(fù)雜度為 O(N2),因?yàn)楹枚辔恢脹]有值所以這會(huì)造成空間的極大浪費(fèi)。可以用題目所說的,只存 儲(chǔ)有值元素的值與位置(即i,j下標(biāo))。stack棧(stack)與隊(duì)列(queue)的區(qū)別,重點(diǎn)題目有問題。正確問法應(yīng)該是這樣:T(n)=2T(n/2)+,T(n)=O(____logn_____)T(n)=T(n-1)+,T(n)=O(_____n______)時(shí)間復(fù)雜度計(jì)算。對(duì)題目有點(diǎn)疑問,故此題答案不確定。(不清楚這是按遞歸還遞推進(jìn)展計(jì)算得出,還有 中的n是下標(biāo)還相乘。)對(duì)于T(n)=2T(n/2)+,可以這樣想,每次計(jì)算T(n)都會(huì)轉(zhuǎn)化為2*T(n/2)+,對(duì)于T(n/2) 又會(huì)轉(zhuǎn)化為T(n/4)的計(jì)算,如此計(jì)算下去,其實(shí)就是按2的指數(shù)次冪的程度在遞減。可 以自己舉個(gè)例子,比方計(jì)算T(16),那計(jì)算過程為T(16)->T(8)->T(4)->T(2)->T(1),所以計(jì) 算次數(shù)為log16=4,類似T(n)=T(n-1)+的復(fù)雜度可以計(jì)算。樹的前、中、后序遍歷,重點(diǎn)首先要明白前、中、后序遍歷是根據(jù)根的位置決定的,比方前序遍歷就是(根左右),中 序遍歷為(左根右)....首先你得能很熟練的寫出一棵樹的前、中、后序遍歷(preorder、inorder、postorder),然 后可以進(jìn)展一下分析,對(duì)于前序遍歷ABECDFGHIJ,中序遍歷EBCDAFHIGJ,由前序 遍歷可知根結(jié)點(diǎn)肯定為A,那么從中序遍歷里面可以以A為中點(diǎn)進(jìn)展分割,左邊的部 分必定屬于左子樹,右邊的局部肯定屬于

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論