数据结构与算法分析C++版答案.docx

上传人:b****3 文档编号:1453657 上传时间:2022-10-22 格式:DOCX 页数:56 大小:32.89KB
下载 相关 举报
数据结构与算法分析C++版答案.docx_第1页
第1页 / 共56页
数据结构与算法分析C++版答案.docx_第2页
第2页 / 共56页
数据结构与算法分析C++版答案.docx_第3页
第3页 / 共56页
数据结构与算法分析C++版答案.docx_第4页
第4页 / 共56页
数据结构与算法分析C++版答案.docx_第5页
第5页 / 共56页
点击查看更多>>
下载资源
资源描述

数据结构与算法分析C++版答案.docx

《数据结构与算法分析C++版答案.docx》由会员分享,可在线阅读,更多相关《数据结构与算法分析C++版答案.docx(56页珍藏版)》请在冰豆网上搜索。

数据结构与算法分析C++版答案.docx

数据结构与算法分析C++版答案

DataStructuresandAlgorithm习题答案

Prefaceii

1DataStructuresandAlgorithms1

2MathematicalPreliminaries5

3AlgorithmAnalysis17

4Lists,Stacks,andQueues23

5BinaryTrees32

6GeneralTrees40

7InternalSorting46

8FileProcessingandExternalSorting54

9Searching58

10Indexing64

11Graphs69

12ListsandArraysRevisited76

13AdvancedTreeStructures82

i

iiContents

14AnalysisTechniques88

15LimitstoComputation94

Preface

 

ContainedhereinarethesolutionstoallexercisesfromthetextbookAPractical

IntroductiontoDataStructuresandAlgorithmAnalysis,2ndedition.

FormostoftheproblemsrequiringanalgorithmIhavegivenactualcode.In

afewcasesIhavepresentedpseudocode.Pleasebeawarethatthecodepresented

inthismanualhasnotactuallybeencompiledandtested.WhileIbelievethealgorithms

tobeessentiallycorrect,theremaybeerrorsinsyntaxaswellassemantics.

Mostimportantly,thesesolutionsprovideaguidetotheinstructorastotheintended

answer,ratherthanusableprograms.

1

DataStructuresandAlgorithms

 

Instructor’snote:

Unliketheotherchapters,manyofthequestionsinthischapter

arenotreallysuitableforgradedwork.Thequestionsaremainlyintendedtoget

studentsthinkingaboutdatastructuresissues.

1.1

Thisquestiondoesnothaveaspecificrightanswer,providedthestudent

keepstothespiritofthequestion.Studentsmayhavetroublewiththeconcept

of“operations.”

1.2

Thisexerciseasksthestudenttoexpandontheirconceptofanintegerrepresentation.

AgoodanswerisdescribedbyProject4.5,whereasingly-linked

listissuggested.Themoststraightforwardimplementationstoreseachdigit

initsownlistnode,withdigitsstoredinreverseorder.Additionandmultiplication

areimplementedbywhatamountstograde-schoolarithmetic.For

addition,simplymarchdowninparallelthroughthetwolistsrepresenting

theoperands,ateachdigitappendingtoanewlisttheappropriatepartial

sumandbringingforwardacarrybitasnecessary.Formultiplication,combine

theadditionfunctionwithanewfunctionthatmultipliesasingledigit

byaninteger.Exponentiationcanbedoneeitherbyrepeatedmultiplication

(notreallypractical)orbythetraditionalΘ(logn)-timealgorithmbasedon

thebinaryrepresentationoftheexponent.Discoveringthisfasteralgorithm

willbebeyondthereachofmoststudents,soshouldnotberequired.

1.3

AsampleADTforcharacterstringsmightlookasfollows(withthenormal

interpretationofthefunctionnamesassumed).

Chap.1DataStructuresandAlgorithms

//Concatenatetwostrings

Stringstrcat(Strings1,Strings2);

 

//Returnthelengthofastring

intlength(Strings1);

 

//Extractasubstring,startingat‘start’,

//andoflength‘length’

Stringextract(Strings1,intstart,intlength);

 

//Getthefirstcharacter

charfirst(Strings1);

 

//Comparetwostrings:

thenormalC++strcmpfunction.

Some

//conventionshouldbeindicatedforhowtointerpret

the

//returnvalue.InC++,thisis1

fors1

//and1fors1>s2.

intstrcmp(Strings1,Strings2)

 

//Copyastring

intstrcpy(Stringsource,Stringdestination)

 

1.4

TheanswertothisquestionisprovidedbytheADTforlistsgiveninChapter

4.

1.5

One’scomplimentstoresthebinaryrepresentationofpositivenumbers,and

storesthebinaryrepresentationofanegativenumberwiththebitsinverted.

Two’scomplimentisthesame,exceptthatanegativenumberhasitsbits

invertedandthenoneisadded(forreasonsofefficiencyinhardwareimplementation).

ThisrepresentationisthephysicalimplementationofanADT

definedbythenormalarithmeticoperations,declarations,andothersupport

givenbytheprogramminglanguageforintegers.

1.6

AnADTfortwo-dimensionalarraysmightlookasfollows.

Matrixadd(MatrixM1,MatrixM2);

Matrixmultiply(MatrixM1,MatrixM2);

Matrixtranspose(MatrixM1);

voidsetvalue(MatrixM1,introw,intcol,intval);

intgetvalue(MatrixM1,introw,intcol);

Listgetrow(MatrixM1,introw);

 

OneimplementationforthesparsematrixisdescribedinSection12.3Anotherimplementation

isahashtablewhosesearchkeyisaconcatenationofthematrixcoordinates.

 

1.7

Everyproblemcertainlydoesnothaveanalgorithm.AsdiscussedinChapter15,

thereareanumberofreasonswhythismightbethecase.Someproblemsdon’t

haveasufficientlycleardefinition.Someproblems,suchasthehaltingproblem,

arenon-computable.Forsomeproblems,suchasonetypicallystudiedbyartificial

intelligenceresearchers,wesimplydon’tknowasolution.

1.8

Wemustassumethatby“algorithm”wemeansomethingcomposedofstepsare

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 总结汇报 > 工作总结汇报

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1