




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
SearchTrees692418<>=1SearchTreesOrderedDictionariesKeysareassumedtocomefromatotalorder.Newoperations:closestKeyBefore(k)closestElemBefore(k)closestKeyAfter(k)closestElemAfter(k)2SearchTreesBinarySearch(§3.1.1)BinarysearchperformsoperationfindElement(k)onadictionaryimplementedbymeansofanarray-basedsequence,sortedbykeyateachstep,thenumberofcandidateitemsishalvedterminatesafterO(logn)stepsExample:findElement(7)134578911141618191345789111416181913457891114161819134578911141618190000mlhmlhmlhl=m=h3SearchTreesLookupTable(§3.1.1)AlookuptableisadictionaryimplementedbymeansofasortedsequenceWestoretheitemsofthedictionaryinanarray-basedsequence,sortedbykeyWeuseanexternalcomparatorforthekeysPerformance:findElementtakesO(logn)time,usingbinarysearchinsertItemtakesO(n)timesinceintheworstcasewehavetoshiftn/2itemstomakeroomforthenewitemremoveElementtakeO(n)timesinceintheworstcasewehavetoshiftn/2itemstocompacttheitemsaftertheremovalThelookuptableiseffectiveonlyfordictionariesofsmallsizeorfordictionariesonwhichsearchesarethemostcommonoperations,whileinsertionsandremovalsarerarelyperformed(e.g.,creditcardauthorizations)4SearchTreesSearch(§3.1.3)Tosearchforakeyk,wetraceadownwardpathstartingattherootThenextnodevisiteddependsontheoutcomeofthecomparisonofkwiththekeyofthecurrentnodeIfwereachaleaf,thekeyisnotfoundandwereturnNO_SUCH_KEYExample:findElement(4)Algorithm
findElement(k,v)
if
T.isExternal(v) return
NO_SUCH_KEYifk
<
key(v)
return
findElement(k,T.leftChild(v))elseifk
=
key(v)
return
element(v)else
{k
>
key(v)}
return
findElement(k,T.rightChild(v))692418<>=6SearchTreesInsertion(§3.1.4)ToperformoperationinsertItem(k,o),wesearchforkeykAssumekisnotalreadyinthetree,andletletwbetheleafreachedbythesearchWeinsertkatnodewandexpandwintoaninternalnodeExample:insert56924186924185<>>ww7SearchTreesDeletion(§3.1.5)ToperformoperationremoveElement(k),wesearchforkeykAssumekeykisinthetree,andletletvbethenodestoringkIfnodevhasaleafchildw,weremovevandwfromthetreewithoperationremoveAboveExternal(w)Example:remove46924185vw692518<>8SearchTreesDeletion(cont.)Weconsiderthecasewherethekeyktoberemovedisstoredatanodevwhosechildrenarebothinternalwefindtheinternalnodewthatfollowsvinaninordertraversalwecopykey(w)intonodevweremovenodewanditsleftchildz(whichmustbealeaf)bymeansofoperationremoveAboveExternal(z)Example:remove3318695vwz251869v29SearchTreesPerformance(§3.1.6)ConsideradictionarywithnitemsimplementedbymeansofabinarysearchtreeofheighththespaceusedisO(n)methodsfindElement,insertItemandremoveElementtakeO(h)timeTheheighthisO(n)intheworstcaseandO(logn)inthebestcase10SearchTreesAVLTrees6384vz11SearchTreesAVLTreeDefinitionAVLtreesarebalanced.AnAVLTreeisabinarysearchtreesuchthatforeveryinternalnodevofT,theheightsofthechildrenofvcandifferbyatmost1.AnexampleofanAVLtreewheretheheightsareshownnexttothenodes:12SearchTreesHeightofanAVLTreeFact:TheheightofanAVLtreestoringnkeysisO(logn).Proof:Letusboundn(h):theminimumnumberofinternalnodesofanAVLtreeofheighth.Weeasilyseethatn(1)=1andn(2)=2Forn>2,anAVLtreeofheighthcontainstherootnode,oneAVLsubtreeofheightn-1andanotherofheightn-2.Thatis,n(h)=1+n(h-1)+n(h-2)Knowingn(h-1)>n(h-2),wegetn(h)>2n(h-2).Son(h)>2n(h-2),n(h)>4n(h-4),n(h)>8n(n-6),…(byinduction),n(h)>2in(h-2i).Nowleti=(floorofh/2)-1Thenweget:n(h)>2h/2-1Takinglogarithms:h<2logn(h)+2ThustheheightofanAVLtreeisO(logn)34n(1)n(2)13SearchTreesInsertioninanAVLTreeInsertionisasinabinarysearchtreeAlwaysdonebyexpandinganexternalnode.Example:441778325088486254wb=xa=yc=z4417783250884862beforeinsertionafterinsertion14SearchTreesTrinodeRestructuringlet(a,b,c)beaninorderlistingofx,y,zperformtherotationsneededtomakebthetopmostnodeofthethreeb=ya=zc=xT0T1T2T3b=ya=zc=xT0T1T2T3c=yb=xa=zT0T1T2T3b=xc=ya=zT0T1T2T3case1:singlerotation(aleftrotationabouta)case2:doublerotation(arightrotationaboutc,thenaleftrotationabouta)(othertwocasesaresymmetrical)15SearchTreesInsertionExample,continued884417783250486224112231541T0T1T2T3xyzunbalanced......balancedT116SearchTreesRestructuring (asSingleRotations)SingleRotations:17SearchTreesRestructuring (asDoubleRotations)doublerotations:18SearchTreesRemovalinanAVLTreeRemovalbeginsasinabinarysearchtree,whichmeansthenoderemovedwillbecomeanemptyexternalnode.Itsparent,w,maycauseanimbalance.Example:4417783250884862544417785088486254beforedeletionof32afterdeletion19SearchTreesRebalancingafteraRemovalLetzbethefirstunbalancednodeencounteredwhiletravellingupthetreefromw.Also,letybethechildofzwiththelargerheight,andletxbethechildofywiththelargerheight.Weperformrestructure(x)torestorebalanceatz.Asthisrestructuringmayupsetthebalanceofanothernodehigherinthetree,wemustcontinuecheckingforbalanceuntiltherootofTisreached4417785088486254wc=xb=ya=z441778508848625420SearchTreesRunningTimesforAVLTreesasinglerestructureisO(1)usingalinked-structurebinarytreefindisO(logn)heightoftreeisO(logn),norestructuresneededinsertisO(logn)initialfindisO(logn)Restructuringupthetree,maintainingheightsisO(logn)removeisO(logn)initialfindisO(logn)Restructuringupthetree,maintainingheightsisO(logn)21SearchTreesBouned-DepthSearchTrees9101425722SearchTreesOutlineandReadingBounded-DepthSearchTreesMulti-waysearchtree(§3.3.1)DefinitionSearch(2,4)tree(§3.3.2)DefinitionSearchInsertionDeletion23SearchTreesMulti-WaySearchTreeAmulti-waysearchtreeisanorderedtreesuchthatEachinternalnodehasatleasttwochildrenandstoresd
-1key-elementitems(ki,oi),wheredisthenumberofchildrenForanodewithchildrenv1v2…vd
storingkeysk1k2…kd-1keysinthesubtreeofv1arelessthank1keysinthesubtreeofviarebetweenki-1andki
(i=2,…,d-1)keysinthesubtreeofvd
aregreaterthankd-1Theleavesstorenoitemsandserveasplaceholders11242681530273224SearchTreesMulti-WayInorderTraversalWecanextendthenotionofinordertraversalfrombinarytreestomulti-waysearchtreesNamely,wevisititem(ki,oi)ofnodevbetweentherecursivetraversalsofthesubtreesofvrootedatchildrenviandvi
+
1Aninordertraversalofamulti-waysearchtreevisitsthekeysinincreasingorder1124268153027321357911131915172461418812101625SearchTreesMulti-WaySearchingSimilartosearchinabinarysearchtreeAeachinternalnodewithchildrenv1v2…vdandkeysk1k2…kd-1k
=
ki(i=1,…,d-1):thesearchterminatessuccessfullyk
<
k1:wecontinuethesearchinchildv1ki-1<
k
<
ki(i=2,…,d-1):wecontinuethesearchinchildvik
>kd-1:wecontinuethesearchinchildvdReachinganexternalnodeterminatesthesearchunsuccessfullyExample:searchfor3011242681530273226SearchTrees(2,4)TreeA(2,4)tree(alsocalled2-4treeor2-3-4tree)isamulti-waysearchwiththefollowingpropertiesNode-SizeProperty:everyinternalnodehasatmostfourchildrenDepthProperty:alltheexternalnodeshavethesamedepthDependingonthenumberofchildren,aninternalnodeofa(2,4)treeiscalleda2-node,3-nodeor4-node101524281227321827SearchTreesHeightofa(2,4)TreeTheorem:A(2,4)treestoringn
itemshasheightO(logn) Proof:Lethbetheheightofa(2,4)treewithnitemsSincethereareatleast2iitemsatdepthi
=
0,…,h-1andnoitemsatdepthh,wehave
n
1+2+4+…+2h-1=
2h-1Thus,h
log(n+1)Searchingina(2,4)treewithnitemstakesO(logn)time122h-10items01h-1hdepth28SearchTreesInsertionWeinsertanewitem(k,o)attheparentvoftheleafreachedbysearchingforkWepreservethedepthpropertybutWemaycauseanoverflow(i.e.,nodevmaybecomea5-node)Example:insertingkey30causesanoverflow27323510152428121810152428122730323518vv29SearchTreesOverflowandSplitWehandleanoverflowata5-nodevwithasplitoperation:letv1…v5bethechildrenofvandk1…k4bethekeysofvnodevisreplacednodesv'andv"v'isa3-nodewithkeysk1
k2andchildrenv1
v2
v3v"isa2-nodewithkeyk4
andchildrenv4
v5keyk3isinsertedintotheparentuofv(anewrootmaybecreated)Theoverflowmaypropagatetotheparentnodeu1524122730323518vuv1v2v3v4v515243212273018v'uv1v2v3v4v535v"30SearchTreesAnalysisofInsertionAlgorithm
insertItem(k,o)
1. Wesearchforkeyktolocatetheinsertionnodev
2. Weaddthenewitem(k,o)atnodev
3.while
overflow(v)ifisRoot(v) createanewemptyrootabovevv
split(v)LetTbea(2,4)treewithnitemsTreeThasO(logn)height
Step1takesO(logn)timebecausewevisitO(logn)nodesStep2takesO(1)timeStep3takesO(logn)timebecauseeachsplittakesO(1)timeandweperformO(logn)splitsThus,aninsertionina(2,4)treetakesO(logn)time31SearchTreesDeletionWereducedeletionofanitemtothecasewheretheitemisatthenodewithleafchildrenOtherwise,wereplacetheitemwithitsinordersuccessor(or,equivalently,withitsinorderpredecessor)anddeletethelatteritemExample:todeletekey24,wereplaceitwith27(inordersuccessor)273235101524281218323510152728121832SearchTreesUnderflowandFusionDeletinganitemfromanodevmay
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 五年級上冊數(shù)學(xué)教案 除數(shù)是整數(shù)的小數(shù)除法(二) 西師大版
- 二年級下冊數(shù)學(xué)教案 第1課時(shí) 東西南北 北師大版
- 三年級數(shù)學(xué)下冊教學(xué)設(shè)計(jì)-1.6集郵北師大版
- 六年級下冊數(shù)學(xué)教案-7.2 圖形與位置 ∣蘇教版
- 三年級下冊數(shù)學(xué)教案-5.5 求簡單的經(jīng)過時(shí)間丨蘇教版
- 2025年房地產(chǎn)經(jīng)紀(jì)公司補(bǔ)充協(xié)議反饋 副本
- 2025年學(xué)習(xí)雷鋒精神62周年主題活動(dòng)實(shí)施方案 (3份)
- 湖南省2024年普通高等學(xué)?!緦凇空猩荚嚒編煼额悺繉I(yè)【綜合知識】試題及答案
- 3-乘法-北師大版三年級下冊數(shù)學(xué)單元測試卷(含答案)
- 《晚春》歷年中考古詩欣賞試題匯編(截至2023年)
- 生活會前談心談話提綱
- 比較思想政治教育(第二版)第十二章課件
- 普通外科常見疾病臨床路徑
- 人教版區(qū)域地理課件世界地理之中亞五國【公開課教學(xué)PPT課件】高中地理
- 人教版九年級下冊初中英語全冊作業(yè)設(shè)計(jì)一課一練(課時(shí)練)
- 2021新版GJB9001C-2017體系文件內(nèi)審檢查表
- 風(fēng)篩式清選機(jī)的使用與維護(hù)
- 《計(jì)算流體力學(xué)CFD》
- 馬克思主義宗教觀課件
- 語文版九年級下冊課外閱讀練習(xí)
- 【課件】第11課+美術(shù)的曙光-史前與早期文明的美術(shù)+課件高中美術(shù)人教版(2019)美術(shù)鑒賞
評論
0/150
提交評論