unit-8数据结构与算法_第1页
unit-8数据结构与算法_第2页
unit-8数据结构与算法_第3页
unit-8数据结构与算法_第4页
unit-8数据结构与算法_第5页
已阅读5页,还剩24页未读 继续免费阅读

下载本文档

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

文档简介

Unit8DatastructureByWucanghai1.OverviewofDataStructuresOnceyou'velearnedtoprogram,yourunintoreal-worldproblemsthatrequiremorethanaprogramminglanguagealonetosolve.注1DataStructuresandAlgorithmsinJavaisagentleimmersion(3)intothemostpractical(4)waystomakedatadowhatyouwantittodo.注2注1:这句话的含义是:当你学编程的时候,你遇到的现实的问题就是每一种编程语言单独的存储数据。注2:这句话的含义是:基于Java的数据结构与算法是把数据按你想要的方式存储的最实用的方式。TextADatastructure(1)sandalgorithm(2)sinjavaAnotherwaytolookatdatastructuresistofocusontheirstrengthsandweaknesses.Inthissectionwe'llprovideanoverview,intheformof(5)atable,ofthemajordatastoragestructureswe'llbediscussing.DataStructureAdvantagesDisadvantagesArrayQuickinsertion,veryfastaccessifindexknownSlowsearch,slowdeletion,fixedsize.注3OrderedarrayQuickersearchthanunsortedarraySlowinsertionanddeletion,fixedsize.StackProvideslast-in,first-outaccess.SlowaccesstootheritemsLinkedlistQuickinsertion,quickdeletionSlowsearchBinarytreeQuicksearch,insertion,deletion(iftreeremainsbalanced)Deletionalgorithmiscomplex.注4Red-blacktreeQuicksearch,insertion,deletion.Treealwaysbalanced.ComplexHashtableVeryfastaccessifkeyknown.Fastinsertion.Slowdeletion,accessslowifkeynotknown,inefficientmemoryusage.注5GraphModelsreal-worldsituations.Somealgorithmsareslowandcomplex.注62.OverviewofAlgorithms

Manyofthealgorithmswe'lldiscussapplydirectlytospecificdatastructures.Formostdatastructures,youneedtoknowhowto(1)Insertanewdataitem.(2)Searchforaspecifieditem.(3)Deleteaspecifieditem.

Youmayalsoneedtoknowhowtoiteratethroughalltheitemsinadatastructure,visitingeachoneinturnsoastodisplayitorperformsomeotheractiononit.Oneimportantalgorithmcategory(7)issorting.Therearemanywaystosortdata,Theconceptofrecursionisimportantindesigningcertainalgorithms(1)ProblemswithProcedural(9)Languages

OOPwasinventedbecauseprocedurallanguages,suchasC,Pascal,andBASIC,werefoundtobeinadequate(10)forlargeandcomplexprograms.Whywasthis?3.Object-OrientedProgrammingTheproblemshavetodowiththeoverallorganizationoftheprogram.Proceduralprogramsareorganizedbydividingthecodeintofunctions(calledproceduresorsubroutinesinsomelanguages).注7Groupsoffunctionscouldformlargerunitscalledmodulesorfiles.CrudeOrganizationalUnitsOnedifficultywiththiskindoffunction-basedorganizationwasthatitfocusedonfunctionsattheexpenseofdata.Thereweren'tmanyoptionswhenitcametodata.Tosimplifyslightly,datacouldbelocaltoaparticularfunctionoritcouldbeglobal—accessible(11)toallfunctions.Therewasnoway(atleastnotaflexibleway)tospecifythatsomefunctionscouldaccessavariable(12)andotherscouldn't.Thiscausedproblemswhenseveralfunctionsneededtoaccessthesamedata.Tobeavailabletomorethanonefunction,suchvariableshadtobeglobal,butglobaldatacouldbeaccessedinadvertently(13)byanyfunctionintheprogram.注8Thisleadstofrequentprogrammingerrors.Whatwasneededwasawaytofine-tunedataaccessibility,allowinvariablestobeavailabletofunctionswithaneedtoaccessit,buthidingitfromothers.FromTheideaofobjectsaroseintheprogrammingcommunityasasolutiontotheproblemswithprocedurallanguages.4.ObjectsinaNutshellThisnewentity(14),theobject,solvesseveralproblemssimultaneously(15).Notonlydoesaprogrammingobjectcorrespondmoreaccurately(16)toobjectsintherealworld,italsosolvestheproblemengenderedby(17)globaldataintheproceduralmodel.

注9(1)Objects

Youmightthinkthattheideaofanobjectwouldbeenoughforoneprogrammingrevolution(18),butthere'smore.

注10Earlyon,itwasrealizedthatyoumightwanttomakeseveralobjectsofthesametype.Maybeyou'rewritingafurnacecontrolprogram(19)foranentireapartmenthouse,forexample,andyouneedseveraldozenthermostatobjects(20)inyourprogram.(2)ClassesItseemsashametogotothetroubleofspecifyingeachoneseparately(21).Thus,theideaofclasseswasborn。WordsandPhrasesaccessibleaccuratelycategorycharacteristicentityimmersioninadequateinadvertently可以访问的准确地分类,类别特性,特点实体,实数沉浸;浸没;浸不满意的,不足的不经意的,随意的WordsandPhrasesorientpracticalproceduralrevolutionseparatelysimultaneouslystructurevariable面向….的,朝向实用的过程的革命分别地同时地结构体[数]变量

TextBTheintroductionoftwoimportantdatastructures

Inthispartwewilltakeabriefintroductionabouttwoimportantdatastructures,LinkedlistandHashtable,whichwillmakeiteffectivetosearchorstorethedata.1.Linked(1)listWesawthatarrayshadcertaindisadvantagesasdatastoragestructures.Inanunorderedarray,searchingisslow,whereasinanorderedarray,insertionisslow.注1Inbothkindsofarraysdeletionisslow.Also,thesizeofanarraycan'tbechangedafterit'screated.Inthispartwe'lllookatadatastoragestructurethatsolvessomeoftheseproblems:thelinkedlist.Linkedlistsareprobablythesecondmostcommonlyusedgeneral-purposestoragestructuresafterarrays.Thelinkedlistisaversatile(2)mechanism(3)suitableforuseinmanykindsofgeneral-purposedatabases.Itcanalsoreplaceanarrayasthebasisforotherstoragestructuressuchasstacksandqueues.易变的,灵活的机制

Infact,youcanusealinkedlistinmanycaseswhereyouuseanarray(unlessyouneedfrequentrandomaccess(4)toindividualitemsusinganindex).Linkedlistsaren'tthesolutiontoalldatastorageproblems,buttheyaresurprisinglyversatileandconceptually(5)simplerthansomeotherpopularstructuressuchastrees.

注2We'llinvestigate(6)theirstrengthsandweaknessesaswegoalong.Inthissectionwewillrefertotwoveryusefuldatastructure.随机存取概念上的探讨,投入2.LinksInalinkedlist,eachdataitemisembeddedinalink.AlinkisanobjectofaclasscalledsomethinglikeLink.Becausetherearemanysimilarlinksinalist,itmakessensetouseaseparateclassforthem,distinct(7)fromthelinkedlistitself.注3Eachlinkobjectscontainsareference(usuallycallednext)tothenextlinkinthelist.Afieldinthelistitselfcontainsarereferencetothefirstlink.Here'spartofthedefinitionofaclassLink.Itcontainssomedataandareferencetothenextlink.classLink{publicintiData;//datapublicdoubledData;//datapublicLinknext;//referencetonextlink}Thiskindofclassdefinitionissometimescalledself-referentialbecauseitcontainsafield—callednextinthiscase—ofthesametypeasitself.注53.Hash(8)Tables

Ahashtableisadatastructurethatoffersveryfastinsertionandsearching.Whenyoufirsthearaboutthem,hashtablessoundalmosttoogoodtobetrue.Nomatterhowmanydataitemsthereare,insertionandsearching(andsometimesdeletion)cantakecloseto(9)constanttime:O(1)inBigOnotation.注6Inpracticethisisjustafewmachineinstructions.Forahumanuserofahashtablethisisessentially(10)instantaneous.It'ssofastthatcomputerprogramstypicallyusehashtableswhentheyneedtolookuptensofthousandsofitemsinlessthanasecond(asinspellingcheckers).Hashtablesaresignificantly(11)fasterthantreesNotonlyaretheyfast,hashtablesarerelatively(12)easytoprogr

温馨提示

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

评论

0/150

提交评论