软件学院数据结构与算法分析期末试题B.docx
《软件学院数据结构与算法分析期末试题B.docx》由会员分享,可在线阅读,更多相关《软件学院数据结构与算法分析期末试题B.docx(8页珍藏版)》请在冰豆网上搜索。
软件学院数据结构与算法分析期末试题B
四川大学期末考试试题
(2007-2008学年第1学期)
课程号:
课程名称:
数据结构与算法分析(B卷)任课教师:
适用专业年级:
06级软件工程学号:
姓名:
考试须知
四川大学学生参加由学校组织或由学校承办的各级各类考试,必须严格执行《四川大学考试工作管理办法》和《四川大学考场规则》。
有考试违纪作弊行为的,一律按照《四川大学学生考试违纪作弊处罚条例》进行处理。
四川大学各级各类考试的监考人员,必须严格执行《四川大学考试工作管理办法》、《四川大学考场规则》和《四川大学监考人员职责》。
有违反学校有关规定的,严格按照《四川大学教学事故认定及处理办法》进行处理。
题号
1
2
3
4
5
6
7
卷面成绩
得分
20
10
10
15
15
15
15
阅卷教师
阅卷时间
1.Single-Choicequestions(eachquestion2scores,total20scores)
(1)Theprimarypurposeofmostcomputerprogramsis
a)toperformamathematicalcalculation.
b)tostoreandretrieveinformation.
c)tosortacollectionofrecords.
d)alloftheabove.
(2)AssumethatPcontainsnelements.ThenumberofsetsinthepowersetofPis
a)nb)n^2
c)2^nd)2^n-1
e)2^n+1
(3)Pickthegrowthratethatcorrespondstothemostefficientalgorithmasngetslarge:
a)5nb)20logn
c)2n^2d)2^n
(4)Asequencehasthefollowingproperties:
a)Mayhaveduplicates,elementhaveaposition.
b)Mayhaveduplicates,elementsdonothaveaposition.
c)Maynothaveduplicates,elementshaveaposition.
d)Maynothaveduplicates,elementsdonothaveaposition.
(5)ThecorrecttraversaltouseonaBSTtovisitthenodesinsortedorderis:
a)Preordertraversal.b)Inordertraversal.
c)Postordertraversal.
(6)Whensortingnrecords,Insertionsorthasbest-casecost:
a)O(logn).b)O(n).
c)O(nlogn).d)O(n^2)
e)O(n!
)f)Noneoftheabove.
(7)Foralistoflengthn,thelinked-listimplementation'sprev
functionrequiresworst-casetime:
a)O
(1).
b)O(logn).
c)O(n).
d)O(n^2).
(8)Theeasiestwaytorepresentageneraltreeisto:
a)converttoalist.b)converttoabinarytree.
c)converttoagraph.
(9)Depth-firstsearchisbestimplementedusing:
a)Astackorrecursion.b)Aqueue.
c)Atree.
(10)ForsetP,thenotation|P|indicates
a)ThenumberofelementsinP.b)TheinverseofP.
c)ThepowersetofP.d)Noneoftheabove.
2.(10scores)
WriteaseriesofC++statementsthatusestheListADTasfollowstocreatealistcapableofholdingtwentyelementsandwhichactuallystoresthelistwithfollowingconfiguration:
<6,28|18,8,19>
solution:
listL1(20);
L1.append(6);
L1.append(28);
L1.append(18);
L1.append(8);
L1.append(9);
L1.next();
L1.next();
//Listabstractclass
templateclassList{
public:
//Reinitializethelist.Theclientisresponsiblefor
//reclaimingthestorageusedbythelistelements.
virtualvoidclear()=0;
//Insertanelementatthefrontoftherightpartition.
//Returntrueifsuccessful,falseifthelistisfull.
virtualboolinsert(constElem&)=0;
//Appendanelementattheendoftherightpartition.
//Returntrueifsuccessful,falseifthelistisfull.
virtualboolappend(constElem&)=0;
//Removethefirstelementofrightpartition.Return
//trueifsuccessful,falseifrightpartitionisempty.
//Theelementremovedisreturnedintheparameter.
virtualboolremove(Elem&)=0;
//Placefenceatliststart,makingleftpartitionempty
virtualvoidsetStart()=0;
//Placefenceatlistend,makingrightpartitionempty
virtualvoidsetEnd()=0;
//Movefenceonestepleft;nochangeifalreadyatstart
virtualvoidprev()=0;
//Movefenceonestepright;nochangeifalreadyatend
virtualvoidnext()=0;
//Returnlengthofleftpartition
virtualintleftLength()const=0;
//Returnlengthofrightpartition
virtualintrightLength()const=0;
//Ifposormoreelementsareinthelist,setthesize
//ofleftpartitiontoposandreturntrue.Otherwise,
//donothingandreturnfalse.
virtualboolsetPos(intpos)=0;
//Returninfirstparameterthefirstelementofthe
//rightpartition.Returntrueifsuccessful,false
//iftherightpartitionisempty.
virtualboolgetValue(Elem&)const=0;
//Printthecontentsofthelist
virtualvoidprint()const=0;
};
3.(10scores)
BuildtheHuffmancodingtreeanddeterminethecodesforthefollowingsetoflettersandweights:
abcde.
135711
solution:
Huffmancodingtreeasfollows:
Herearethefinalcodes.
a11111
b1110
c110
d10
e0
4.(15scores)
Whataretheminimumandmaximumnumberofelememtsinaheapofheighth?
solution:
Theminimumnumberofelementsiscontainedintheheapwithasinglenodeatdepthh-1,foratotalof2h-1nodes.
Themaximumnumberofelementsiscontainedintheheapthathascompletelyfilleduplevelh-1,foratotalof2h-1nodes.
5.(15scores)
TheBubbleSortimplentationhasthefollowinginnerforloop:
for(intj=n–1;j>i;j--)
Considertheeffectofreplacingthiswiththefollowingstaement:
for(intj=n–1;j>0;j--)
Wouldthenewimplementationworkcorrectly?
Wouldthechangeaffecttheasymptoticcomplexityofthealgorithm?
Howwouldthechangeaffecttherunningtimeofthealgorithm?
solution:
Therevisedalgorithmwillworkcorrectly,anditsasymptoticcomplexitywillremainΘ(n2).However,itwilldoabouttwiceasmanycomparisons,sinceitwillcompareadjacentelementswithintheportionofthelistalreadyknowntobesorted.Theseadditionalcomparisonsareunproductive.
6.(15scores)
//Binarytreenodeabstractclass
templateclassBinNode{
public:
//Returnthenode'selement
virtualElem&val()=0;
//Setthenode'selement
virtualvoidsetVal(constElem&)=0;
//Returnthenode'sleftchild
virtualBinNode*left()const=0;
//Setthenode'sleftchild
virtualvoidsetLeft(BinNode*)=0;
//Returnthenode'srightchild
virtualBinNode*right()const=0;
//Setthenode'srightchild
virtualvoidsetRight(BinNode*)=0;
//Returntrueiffthenodeisaleaf
virtualboolisLeaf()=0;
};
Writearecursivefunctionthatreturnsacountofthenumberofleafnodesinabinarytrue..
solution:
template
intcount(BinNode*subroot)
{
if(subroot==NULL)return0;//Emptysubtree
if(subroot->isLeaf())return1;//Aleaf
return1+count(subroot->left())+
count(subroot->right());
}
7.(15scores)
ListtheorderinwhichtheedgesofthefollowinggrapharevisitedwhenrunningKruskal’sMSTalgorithm.ShowthefinalMST.
solution:
(1,6)(6,5)(2,3)(2,4)(4,6).
Alternatively,(1,6)(6,5)(2,3)(2,4)(1,2).
etc