数据结构例题解析1Word格式.docx
- 文档编号:20838939
- 上传时间:2023-01-25
- 格式:DOCX
- 页数:15
- 大小:604.02KB
数据结构例题解析1Word格式.docx
《数据结构例题解析1Word格式.docx》由会员分享,可在线阅读,更多相关《数据结构例题解析1Word格式.docx(15页珍藏版)》请在冰豆网上搜索。
c.linkedlisttostoretheelements
8.(b)Supposethereare3edgesinanundirectedgraphG,IfwerepresentgraphGwithaadjacencymatrix,Howmany“1”sarethereinthematrix?
a.3b.6c.1d.9
9.(d)ConstructaHuffmantreebyfourleafwhoseweightsare9,2,5,7respectively.Theweightedpathlengthis___________.
a.29b.37c.46d.44
10.Considerthefollowingweightedgraph.
ConsiderDijkstra’salgorithmonthisgraphtofindtheshortestpathswithsasastartingvertex.Whicharethefirstfourverticesextractedfromthepriorityqueuebythealgorithm(listedintheordertheyareextracted)?
a.s,y,t,xb.s,y,x,zc.s,t,y,xd.s,y,x,t
Fig.1
11.Hereisanarrayoftenintegers:
5389170264
Supposewepartitionthisarrayusingquicksort'
spartitionfunctionandusing5forthepivot.Whichshowsthearrayafterpartitionfinishes:
a.5342107968
b.0342157968
c.3102458967
d.3102458976
e.Noneoftheabove
IIFillinBlank(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.
5.T(n)=2T(n/2)+cn,T(n)=O(logn)
T(n)=T(n-1)+cn,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.2
ABCDE
A01010
B00110
C00001
D00001
E00000
Dft:
ABCED
Bft:
ABDCE
2.Thesequenceofinputkeysisshownbelow:
19,1,23,14,55,20,84,27,68,11,10,17
Afixedtablesizeof19andahashfunctionH(key)=key%13,withlinearprobing(线性探测),fillthetablebelowandcomputetheaveragelengthofsuccessfulsearch.
3.Showtheresultsofinserting53,17,78,09,45,65,87each,oneatatime,inainitiallyemptymaxheap(大根堆)
4.writethesequenceofpreorder,postordertraversalsandaddinorderthreadsinthetree.
Fig.3
5.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.04
6.GraphGshowninFig4isadirectedgraph,pleasedescribeGwithadjacencylistandwritetopologicalordering.
Fig.4
IVFillinblankofalgorithms.(15)
1.HereissinglesourceshortestpathalgorithmDijkstra.Fillinblankofthealgorithm.
classGraph{//图的类定义
private:
floatEdge[NumVertices][NumVertices];
floatdist[NumVertices];
//最短路径长度数组
intpath[NumVertices];
//最短路径数组
intS[NumVertices];
//最短路径顶点集
public:
voidShortestPath(int,int);
intchoose(int);
};
voidGraph:
:
ShortestPath(intn,intv){
//在具有n个顶点的带权有向图中,各边上权值由Edge[i][j]给出。
建立一个数组:
dist[j],0j<
n,//保存从顶点v到顶点j的最短路径长度,同时用数组path[j],0j<
n,存放求到的最短路径。
i++){
dist[i]=Edge[v][i];
//dist数组初始化
S[i]=0;
if(i!
=v&
&
dist[i]<
MAXNUM)path[i]=v;
elsepath[i]=-1;
//path数组初始化
}
S[v]=1;
dist[v]=0;
//顶点v加入顶点集合
//选择当前不在集合S中具有最短路径的顶点u
for(i=0;
n;
i++){
floatmin=MAXNUM;
intu=v;
j++)
if(!
S[j]&
dist[j]<
min)
{u=j;
min=dist[j];
S[u]=1;
//将顶点u加入集合S
for(intw=0;
w<
w++)//修改
S[w]&
Edge[u][w]<
MAXNUM&
dis[w]>
(min+Edge[u][w])){
dist[w]=min+Edge[u][w];
path[w]=u;
}}
}
3.Considerthefollowingfunctiontobalancesymbolsstoredinstringexpthatincludesparentheses(圆括号)andnumbers.PleaseFillinblank.
#include<
stack>
Usingnamespacestd;
intmatching(string&
exp){
//expisapointertoastringtocheck
intstate=1,i=0;
chare;
stack<
char>
while(i<
exp.length()&
state)
switch(exp[i]){
case'
('
s.push(exp[i]);
i++;
break;
case'
)'
if(!
s.empty()&
s.front()==’(’){
s.pop();
}
else
state=0;
//anerroroccurs
default:
}//endofwhile
if(state&
s.empty())return1;
elsereturn0;
VProgramming(30)
1.Writeefficientfunctions(andgivetheirBig-Ohrunningtimes)thattakeapointertoabinarytreerootTandcompute:
–ThenumberofleavesofT
typedefstructBiTNode
{TElemTypedata;
structBiTNode*lchild,*rchild;
}BiTNode,*BiTree;
2.WriteamethodcalledmaximumDegreeofanundirectedhgraphthatreturnsthemaximumdegreeofanyvertexinthegraph.Thegraphisstoredwithadjacencymatrix.Writethedefinitionofthegraph.implementthefunction.Analyzespacecomplexityandtimecomplexityofyourfunction.
3.Writeafunctionwithlinkedlistthatinsertsanumberintoasortedlinkedlist.Firstly,youshouldwriteafunctioncreatesalistthatlikethis:
L={3,5,8,12,32,48}
andtheninsert25intothislist.
答案解析0-0,仅供参考,若有不同意见请联系QQ767954870☆_☆
选择题:
1-5:
ACBDB6-11:
CBBDDE
1、知识点:
复杂度分析,必考
思路:
复杂度主要计算算法的步数,可以看出,当前循环执行的步数与i的值是相等的,所以可列1+2+..+i=(5*n*n+2),复杂度的计算忽略常数,(1+i)*i/2=(5*n*n+2),i~O(n)
2、知识点:
non-linear与linear的区别
3、知识点:
复杂度分析+线性序列
很显然,当元素在sequencelist的末尾的时候,removing元素复杂度最高O(n)
4、知识点:
循环队列(circularqueue),重点
主要区分循环队列判断空与满的条件。
主要有三个方法
count计数,判断当前队列的元素与maxsize的大小
vis标志,比如可以一开始设vis=0,满时设置vis=1
就是题目中说的gap(....重点)
front代表头指针,rear代表尾指针
循环队列空时:
front==rear;
满时:
front==(rear+1)%maxsize
5、知识点:
递归的定义,terminationmissing,终止条件缺失则可能无限调用。
6、知识点:
完全二叉树(completebinarytree)与满二叉树(fullbinarytree)的区别
学院PPT上有如下定义
depthofanode:
numberofancestors
heightofatree:
maximumdepthofanynode
并且有结点计算公式:
2h+1-1(其中h为树的高度,与某XXXX定义树的高度不一样,且照学院教材来做==)
所以ans:
24+1-1=31
7、知识点:
查找
有疑问的题...
单纯来说二分查找(binarysearch)的速度O(logN)是比较快的,可是题目仅仅要求Searchinginanunsortedlist,只进行一次查找,那我们用二分还要先进行排序O(NlogN)+O(logN)的复杂度是不如选项b的。
sentinel(哨兵...)的概念可见ppt讲插入排序的地方,貌似能加快查找速度吧...
8、知识点:
图的邻接矩阵存储
注意题目所问,无向图(undirectedgraph),每条边都是要存储两遍的
9、知识点:
哈夫曼树(Huffmantree)
离散上学过的。
。
weightedpathlength=
所以ans=9*1+7*2+5*3+2*3=44(自己构造哈夫曼树》。
《)
10、知识点:
Dijkstra/最短路,重点
11、知识点:
快排,重点
10、11两题是重点,限文字难于描述清楚,请自主学习%>
_<
%
注意10题在priority_queue里进行更新时一开始肯定加入s、y结点,而后x结点会因为松弛操作从而距离变为1+3=4<
5(t结点),所以x结点会比t结点先压入队列。
2、填空题
1、O(n2)
2、6数组元素存储地址的计算。
注意题目中规定存储下三角矩阵lowertriangleonly
3、location在稀疏矩阵中sparsematrix,如果对每个元素都进行存储的话空间复杂度为O(N2),因为好多位置没有值所以这会造成空间的极大浪费。
可以用题目所说的,只存储有值元素的值与位置(即i,j下标)。
4、stack栈(stack)与队列(queue)的区别,重点
5、题目有问题。
正确问法应该是这样:
T(n)=2T(n/2)+cn,T(n)=O(____logn_____)
T(n)=T(n-1)+cn,T(n)=O(_____n______)
时间复杂度计算。
对题目有点疑问,故此题答案不确定。
(不清楚这是按递归还递推进行计算得出,还有cn中的n是下标还cn相乘。
)
对于T(n)=2T(n/2)+cn,可以这样想,每次计算T(n)都会转化为2*T(n/2)+cn,对于T(n/2)又会转化为T(n/4)的计算,如此计算下去,其实就是按2的指数次幂的程度在递减。
可以自己举个例子,比如计算T(16),那计算过程为T(16)->
T(8)->
T(4)->
T
(2)->
T
(1),所以计算次数为log16=4,类似T(n)=T(n-1)+cn的复杂度可以计算。
6、树的前、中、后序遍历,重点
首先要明白前、中、后序遍历是根据根的位置决定的,比如前序遍历就是(根左右),中序遍历为(左根右)....
首先你得能很熟练的写出一棵树的前、中、后序遍历(preorder、inorder、postorder),然后可以进行一下分析,对于前序遍历ABECDFGHIJ,中序遍历EBCDAFHIGJ,由前序遍历可知根结点肯定为A,那么从中序遍历里面可以以A为中点进行分割,左边的部分必定属于左子树,右边的部分肯定属于右子树,然后进行一步步分割,自己多尝试一下就ok了
构造树如下:
所以后序遍历为:
EDCBIHJGFA
ps:
已知前序遍历和后序遍历,不能确定唯一的中序遍历
7、n/2+1
满二叉树,设叶子层leafnode为第p层,则非叶子结点20+21+22+...+2p-1=2p-1
叶子结点:
2p
若总结点为n,那叶子结点为n/2+1
ps:
有好多种答案,比如(n+1)/2,n/2取下界等。
8、O(N)
关于插入排序,最好的情况就是序列已经有序,那就少去了比较的步数,直接进行n个元素的插入,故复杂度为O(N)
9、1502212426574366804873
希尔排序。
每个数与增量处进行大小比较若大于则交换。
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 例题 解析