![《计算机科学导论》课件Unit 11Data and File Structure_第1页](http://file4.renrendoc.com/view/06bb537eb6566ef0d393b35436df36fc/06bb537eb6566ef0d393b35436df36fc1.gif)
![《计算机科学导论》课件Unit 11Data and File Structure_第2页](http://file4.renrendoc.com/view/06bb537eb6566ef0d393b35436df36fc/06bb537eb6566ef0d393b35436df36fc2.gif)
![《计算机科学导论》课件Unit 11Data and File Structure_第3页](http://file4.renrendoc.com/view/06bb537eb6566ef0d393b35436df36fc/06bb537eb6566ef0d393b35436df36fc3.gif)
![《计算机科学导论》课件Unit 11Data and File Structure_第4页](http://file4.renrendoc.com/view/06bb537eb6566ef0d393b35436df36fc/06bb537eb6566ef0d393b35436df36fc4.gif)
![《计算机科学导论》课件Unit 11Data and File Structure_第5页](http://file4.renrendoc.com/view/06bb537eb6566ef0d393b35436df36fc/06bb537eb6566ef0d393b35436df36fc5.gif)
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
?计算机科学导论?课件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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 17 赛小车 说课稿-2023-2024学年科学三年级下册人教鄂教版001
- 2024秋七年级数学上册 第四章 整式的加减4.1 整式 2多项式说课稿(新版)冀教版
- 5 一起做游戏(说课稿)-2024-2025学年一年级上册数学北师大版2024
- 2024-2025学年高中语文 第一单元 走近经济 第2课 规则和信用:市场经济的法制基石和道德基石说课稿 粤教版必修5
- 耳鼻喉科手术器械项目融资渠道探索
- 二零二五版内贸集装箱货物运输代理合同市场拓展支持
- 沐足保底协议书(2篇)
- 海鲜股东合同(2篇)
- 2024秋一年级道德与法治上册 第5课 上课铃儿响说课稿 粤教版
- 2024-2025学年新教材高中数学 第4章 概率与统计章末综合提升说课稿 新人教B版选择性必修第二册001
- 2024-2025学年成都市金牛区九年级上期末(一诊)英语试题(含答案)
- 2024-2025学年广东省深圳市南山区监测数学三年级第一学期期末学业水平测试试题含解析
- 广东2024年广东金融学院招聘专职辅导员9人笔试历年典型考点(频考版试卷)附带答案详解
- 2025年研究生考试考研英语(二204)试卷与参考答案
- DB31∕731-2020 船舶修正总吨单位产品能源消耗限额
- 2024-年全国医学博士外语统一入学考试英语试题
- 2024年卫生专业技术资格考试卫生检验技术(初级(师)211)相关专业知识试题及答案指导
- 《手卫生知识培训》培训课件
- 江苏省南京鼓楼区2024年中考联考英语试题含答案
- 儿科护理学试题及答案解析-神经系统疾病患儿的护理(二)
- 15篇文章包含英语四级所有词汇
评论
0/150
提交评论