《计算机科学导论》课件Unit 11Data and File Structure_第1页
《计算机科学导论》课件Unit 11Data and File Structure_第2页
《计算机科学导论》课件Unit 11Data and File Structure_第3页
《计算机科学导论》课件Unit 11Data and File Structure_第4页
《计算机科学导论》课件Unit 11Data and File Structure_第5页
已阅读5页,还剩38页未读 继续免费阅读

下载本文档

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

文档简介

?计算机科学导论?课件Unit11DataandFileStructure211-1AbstractDataTypes11-2List11-3Stack11-4Queue11-5TreeandGraph11-6FileStructure11-7ReferencesAndRecommendedReading11-8Summary11-9PracticeSetOUTLINE11-1AbstractDataTypesWhendealingwithcomplexity,weassignalabeltoanassemblyofobjectsorconceptsandthenmanipulatethelabelinplaceoftheassembly.Suchalabeliscalledametaphorbycognitivepsychologists.Aparticularlabelmightberelatedtootherlabelsorotherpiecesofinformation.Ahierarchyofconceptsandlabelsisformedbygivingalabeltothiscollection.Thishierarchyoflabelsallowsustopaymoreattentiononcrucialissueswhileignoringunnecessarydetails.Figure11.1showsusthemodelforanADT.11-1AbstractDataTypesFigure11.1ThemodelforanADT11-1AbstractDataTypesTakeacarasanexample.Steering,acceleratingandbrakingarenecessaryactivitiesforitsoperation.Onalmostallpassengercars,thesteeringwheelisturnedforsteering,thegaspedalispushedforaccelerating,andthebrakepedalispushedforapplyingbrakes.ThisdesignforcarscanberegardedasanADTwithoperations“steer,〞“accelerate,〞and“brake〞.Theseoperationsontwodifferentcarsmightbeimplementedverydifferently,driversarenotrequiredtohaveknowledgeofthespecificsofanyparticularengineordrivedesign.Thesedifferencesarehiddendeliberatelyfromusers.11-1AbstractDataTypesFigure11.2Therelationshipbetweendataitems,abstractdatatypes,anddatastructures.Alist【列表】isdefinedasafinite,orderedsequenceofdataitemsknownaselements.Eachelementinthelisthasapositionandadatatype.Array-basedlist【基于数组列表】andlinkedlist【基于链表列表】arethetwostandardmethodstoimplementlists.

11-2ListInthearray-basedlist,elementsarestoredincontiguouscellsofthearray.Andthispropertymustbemaintainedafterinsert,

appendandremoveoperations.

Array-basedListFigure11.3Insertinganelementattheheadofanarray-basedlistItiseasytoinsertorremoveelementsatthetailofthelist,sotheappendoperationtakesO(1)time.Butifanelementneedstobeinsertedattheheadofthelist,allelementsmustshiftonepositiontowardthetailtomakeroom,asillustratedby.ThisprocesstakesO(n)timeifthelisthasncurrentelements.Ifwewanttoinsertanelementatpositioniwithinalisthavinglengthequivalentton,thenn–ielementsmustmovetowardtheend.Removinganelementatpositionirequiremovingn-i–1elementstowardtheheadofthelist.Array-basedListmemory【内存】isallocateddynamicallywhenanewelementisaddedintoit

Aseparatelistcanbemadeasanodeclassbecausenodesinalistaredistinctobjects(asopposedtosimplyacellinanarray).

Thefirstnodeofthelistisaccessedfromapointercalledhead【头指针】.Apointercalledtail【尾指针】isalsokepttothelastlinkofthelist.Anotherpointercalledcurr【当前指针】isdefinedtoindicatethepositionofthecurrentelement.

linkedListFigure11.4Onepossibleimplementationofalinked-listlinkedList

Abetterimplementationof

linked-listIfcurrissettopointtotheprecedingelement,addinganewelementaftercurrismucheasier.linkedListFigure11.6Nodeinsertioninalinkedlist

showsthenewlinkednode’selementfield.showsthenewlinkednode’snextfield.Itispointingtothenodeusedtobethecurrentnode(thenodewithvalue12).showsthenextfieldofthenodeprecedingthecurrentposition.Itwaspointingtothenodehavingvalue12;nowitispointingtothenewnodehavingvalue10.linkedListFigure11.7NoderemovalprocessinlinkedlistRemovinganodefromthelinkedlistneedsonlytoredirecttheappropriatepointeraroundthenodetobedeleted.Becarefulto‘leave’thememoryforthedeletedlinknode.InFigure11.7,shows‘it’pointingthelistnodewithvalue10beingremoved.showsthenextfieldoftheprecedinglistnode,whichispointingthenodefollowingtheonebeingremoved.11-3StackThestack【栈】isalist-likedatastructure.Elementscanonlybeinsertedorremovedfromoneendinstack.Forthisreason,stacksarenotasflexibleaslists.However,intheapplicationsthatrequireonlythelimitedformofinsertandremoveoperations,stacksaremoreefficientthanlists.Longbeforetheinventionofthecomputer,stackswereusedbyaccountants.Theynamedthestacka“LIFO〞【后进先出】list,whichmeans“Last-In,First-Out.〞Stacksremoveelementsinreverseorderoftheirarrival.Theelementweaccesseachtimeiscalledthetopelement.Whenanewelementisadded,wesayitispushedontothestack.Whenremoved,anelementissaidtobepoppedfromthestack.11-3StackFigure11.8showsasimplerepresentationofstackruntimewithpushandpopoperations.Thetwocommonapproachesofstackimplementationpresentedherearearray-basedandlinkedstacks,whichareanalogoustoarray-basedandlinkedlists,respectively.Theapplicationofstacksinamostcommoncomputerapplicationisperhapsnotevenvisibletoitsusers.Liketheuseofstacksduringruntimeofasubroutinecall【子程序调用】inmostoftheprogramminglanguages.

Theimplementationofasubroutineincludespushingitsinformationincludingreturnaddress,parameters,andlocalvariables,ontoastack.Thesubroutine’sinformationisreferredtoasanactivationrecord.Ifasubroutinecallcontainsanothersubroutinecall,itsactivationrecordisalsopushedontothestack.Thetopactivationrecordispoppedoffthestackoneachreturnfromasubroutine.11-3Stack11-4

QueueSimilartostack,thequeue【队列】isalist-likedatastructurethatgiveslimitedaccesstoitselements.Aqueuehastwokindsofoperations.Oneisenqueue【入队】operation,andtheotherisdequeue【出队】operation.Queuesoperatelikestandinginlineatasubwaystation.Ifnobodycheats,thennewcomersgotothebackoftheline.Thepersonatthefrontofthelineisthenexttoenterthesubway.Thus,inqueues,theelementsareretrievedinorderofarrival.Queueisalsocalleda“FIFO〞【先进先出】list,whichmeans“First-In,First-Out.〞11-4QueueFigure11.9showstherepresentationofaqueue.Figure11.10ThecircularqueuewitharraypositionsincreasingintheclockwisedirectionTherearetwoimplementationsofqueue:thearray-basedqueueandlinkedqueue.Inpractice,aqueuecanalsobetreatedasacircle.11-5TreeandGraphAtree【树】isaconnectedundirectedgraphwithfinitesetofelementscallednodesandfinitesetofbranchesthatconnectthenodes.Thedegree【度】ofanodeisdefinedasthenumberofbranchesassociatedwithanode.Thenumberofbranchesdirectedtowardthenodeiscalledthenode’sindegree【入度】while,thenumberofbranchesdirectedawayfromthenodeiscalledoutdegree【出度】.Theroot【根节点】ofatreeisthefirstnode,ifthetreeisnotempty.Theindegreeoftherootiszero.Excepttheroot,eachnodeinatreehasanindegreeofexactlyone.However,outdegreeofanynodeinatreecanbezero,oneormore.Aleaf【叶节点】isanynodewithanoutdegreeofzero.Anodethatisnotarootoraleafiscalledaninternalnode.11-5TreeandGraphFigure11.11Anexampleofangeneraltree

Ifanodehassuccessornodes,thatis,ifithasoutdegreeofatleastone,itiscalledaparent【父节点】.Anodewithapredecessoriscalledachild【子节点】.Childnode’sindegreeisnecessarilyone..Nodeswiththesameparentarereferredtoassiblings【兄弟节点】.Apathisasequenceofnodesinwhicheachnodeisadjacenttothenextone.Everynodehasauniquepathfromtheroot.Anynodeinthepathfromtheroottoaparticularnodeiscalledanancestor,andanynodeinapathbelowaparentnodeiscalledthedescendent.Asubtree【子树】isanyconnectedstructurebelowtheroot.Figure11.11showsanexampleofageneraltree.11-5TreeandGraphThelevelofanodeisitsdistancefromtheroot.Therootisatlevelzeroanditschildnodesareatlevel1,andsoon.Theheight,alsocalleddepth,ofatreeisthehighestlevelplus1.

Abinarytree【二叉树】isarootedtreeinwhicheachnodehasnomorethantwochildren,thatisthemaximumoutdegreeofeachnodeistwo.

AbinarytreeThemaximumheightofbinarytreehavingNnodesisN,whereasminimumheightis[log2N]+1.Conversely,assumetheheightofabinarytreetobeH,thenthenumberofnodesinthisbinarytreerangesfromHto2H-1.Abinarysearchtree(BST)

Abinarysearchtree(BST)【二叉查找树】isakindofbinarytreethathasasemanticpropertyonnodes’values.Thevalueineachnodemustbegreaterthanthevalueinanynodeinitsleftsubtreeandsmallerthanthevalueinanynodeinitsrightsubtree.Figure11.13showsanexampleofabinarysearchtree.Figure11.13ExampleofabinarysearchtreeGraphFigure11.14Anexampleofgraph(directflightsforpartofcitiesinChina)Inatree,anodeisrestrictedtobepointedbyatmostoneothernode.Ifweremovethisrestrictiononnode,thenwegetanewdatastructurecalledagraph.GraphAgraphismadeupofagroupofnodescalledvertices【顶点】andagroupoflinesbetweenthenodescallededges【边】.Anundirectedgraph【无向图】isagraphinwhichedgeshavenodirectionswhileedgesinadirectedgraph【有向图】aredirectedfromonenodetoanother,asshowninFigure11.14.IfweaddflightpricetoeachofthearrowsinFigure11.14,thenwegetaweightedgraph【加权图】.Thepriceontheedgesarecalledtheweights【权重】.Twoverticesconnectedbyanedgearecalledadjacentvertices【邻接点】.Andapath【路径】isasequenceofedgesthatconnectstwonodesinagraph.

graphtraversal

graphtraversalisdefinedasaprocessthataccessesalloftheverticesinagraphonceandonlyonceatatimestartingfromanyvertex.therearetwokindsofgraphtraversalmethods:depth-firstsearch【深度优先搜索】breadth-firstsearch【广度优先搜索】Inadepth-firstsearchprocess,wegotothedeepestbranch.Whenwehavetobacktrack,webackupaslittleaspossibleinadepth-firstsearch.Inabreadth-firstsearch,wewanttobackupasfaraspossibletofindapathoriginatingfromtheearliestvertices.

Bothofthesearchmethodscanhavemorethantwosearchpaths.

graphtraversalFigure11.15Anexampleforgraphtraversal

Inadepth-firstsearch,onepossiblesearchpathis15->8->6->2->9->18->17->16->20->23.Whileinabreadth-firstsearch,onepossiblesearchpathis15->8->18->6->9->17->20->2->16->23.Exceptforthedatastructuresmentionedabove,therearehashtable,red-blacktreeandB-tree.Moreaboutthesethreedatastructuresisavailableinreferencematerialslistedattheendofthechapter.11-6FileStructureDataisstoredintheformoffilesinstoragedevices.Afileisanexternalcollectionofrelateddatatreatedasaunit.Youmayusefileinthefollowingsituations.

Youneedtostoredatapermanently.Thecollectionofdataisoftentoolargetostoretheentiredatasimultaneouslyinmainmemory.Filesarestoredinauxiliaryorsecondarystorage【二级存储】devices(disk).Filescanbeopenedforbothreadingandwriting.UNIXseesalldevicesasfiles.So,wecansaythatthekeyboardisalsoakindoffile,althoughitcannotstoredata.Accessmethods

Sequentialaccess:afileisaccessedsequentially,andrecordsareprocessedoneafteranother,frombeginningtoend.Randomaccess:recordsareaccesseddirectly.

Figure11.16TaxonomyoffilestructuresSequentialfiles

Recordsinasequentialfile【顺序文件】canonlybereadandwritteninsequence.Toaccessaspecificrecordwithinthefile,theprogrammustsuccessivelyretrievethepreviousrecords.AnEOF(end-of-file)markerisputafterthelastrecord.Sequentialfilesareusefulinapplicationswhichneedtoaccessallrecordsinsequencefrombeginningtoend.Forexample,ifpersonalinformationabouteachemployeeinacompanyisstoredinafile,youcanusesequentialaccess【顺序访问】toretrieveeachrecordattheendofmonthtoprintthepaychecks.Inthiscase,sequentialaccessismoreefficientandeasierthanrandomaccess【随机访问】.However,indexedfilesareveryinefficientforrandomaccessoflargedatafiles

Indexedfiles

Akey【键】istheuniqueidentifierofdata(orrecord)inthefilewhichismadeupofoneormorefields.Sequentialfilesmaycontainakey,butitisnotusedtolocatetheaddressofrecords.However,theaddressoftherecordshouldbeknownwhenaccessingarecordinafilerandomly.Anindexedfile【索引文件】containsrecordsorderedbyauniquelyidentifiablekey.Forexample,anaccountnumbermightbethekeyforcustomer’srecordinabank.Anindexedfileismadeupofasequentialdatafileandanindex.Theindexitselfisaverysmallfilewithonlytwofields:thekeyofthesequentialfileandtheaddressofthecorrespondingrecordonthedisk.Figure11.17showsthelogicalviewofanindexedfile.IndexedfilesFigure11.17Logicalviewofanindexedfile

Invertedfile:Oneoftheadvantagesofindexedfileisthatitcanusealternateindexes,eachwithadifferentkey.Forexample,anemployeefilecanberetrievedthroughemployeedepartmentorlastnameratherthanemployeenumber.Thistypeofindexedfileissaidtobeaninvertedfile.Hashedfiles

Theindexisusedtomapthekeytotheaddressinanindexedfile.Thehashedfile【哈希文件】doesnotneedanextrafile(index).Itusesmathematicalfunctiontoimplementthismapping.Thefunctionreturnstheaddressonceakeyisgiven.Hashfilesaremostoftenusedasamethodforverifyingfilesize.Thehashedfileisalsoefficientforrandomaccess.

Incontrastwithindexedfile,thehashedfilecanbespace-efficientandquicktofindthekeyofanaddress.However,hashedfilessometimesmayhavetheirownproblems.HashingmethodsHashingmethodsareusedforkey-addressmapping.Someofthehashingmethodsaredirecthashing,divisionhashing,digitextractionhashing,folding,midsquare,rotation,subtraction,andpseudorandommethod.Directhashing:Indirecthashing,thekeyistheaddresswithoutanyalgorithmicmanipulation.

Modulodivisionhashing:Inmodulodivisionhashing,thekeyisdividedbythefilesizeandtheremainderplus1isusedasthehashaddressofthekey.CollisionFigure11.18Modulodivisionhashinganddirecthashing

Ahashcollisionisasituationwhenahashingalgorithmproducesanaddressforthekeybutthataddressisalreadyoccupied.Collisionisanissueforthehashingmethods.Exceptfordirectmethod.Collisionresolution

Thecollisionofkeysbyaddressescanberesolvedbypacingoneofkeysanddatainanotherlocation.Someofthecollisionresolutionmethodsareopenaddressing,linkedlistresolution,buckethashing,chaining,andquadraticprobing.Inopenaddressresolution,whenacollisionoccurs,theaddressesaresearchedforopenorunoccupiedrecordtostorenewdata.Inthisaddressresolutiontechnique,addressoftheitemisnotdeterminedbyitshashvalue.Amajordisadvantagetoopenaddressingisthat,themorecollisionsyouresolve,theprobabilityoffuturecollisionsalsoincreases.

Linkedlistresolutionresolvetheissueofhighprobabilityoffuturecollisions.

11-7ReferencesAndRecommendedReadingDaleNandLewisJ:ComputerScienceIlluminated,Sudbury,MA:JonesandBartlett,2004ForouzanBandGilbergR:ComputerScience:AStructuredProgrammingApproachUsingC,Boston,MA:CourseTechnology,2007ForouzanBandMosharrafF:FoundationsofComputerScience,CengageLearningEMEA,2021GilbergRandForouzanB:DataStructures–APseudocodeApproachwithC,Boston,MA:CourseTechnology,2005GoodrichMandTamassiaR:DataStructuresandAlgorithmsinJava,NewYork:Wiley,2005ShafferCA:DataStructuresandAlgorithmAnalysis,Blacksburg,VA:DoverPublications,202111-7ReferencesAndRecommendedReadingShafferCA:DataStructures&AlgorithmAnalysisinJava,NewYork,DoverPublications,2021SinghMandGargD:ChoosingBestHashingStrategiesandHashFunctions,IEEEInternationalAdvanceComputingConference,2021WeissMA:DataStructures&AlgorithmAnalysisinC++,England,PearsonEducationLimited,2021ZaqoutF,AbbasM,HosamAS:IndexedSequentialAccessMethod(ISAM):AReviewoftheProcessingFiles,UKSimEuropeanSymposiumonComputerModelingandSimulation,202111-8SummaryAdatatypeiscomposedofatypeandacollectionofoperationstomanipulatethetype.Therealizationofadatatypeisanabstractdatatype(ADT)whichisasoftwarecomponent.AnADTdoesnotspecifyhowtoimplementthedatatype.Thehidingofimplementationdetailsfromtheuserandprotectingitfromoutsideaccessisreferredtoasencapsulation.Alistisafinite,orderedsequenceofdataitemsknownaselements.Thearray-basedlistandthelinkedlistarethetwostandardimplementingapproaches.Comparedtoalist,astackisa“LIFO〞listandaqueueisa“FIFO〞list.So,therearemanyvariationsontheimplementationsofstackandqueue.Abinarysearchtreeisakindofbinarytreethathasasemanticpropertyonno

温馨提示

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

最新文档

评论

0/150

提交评论