Sie sind auf Seite 1von 12

BinarySearchTree

BinarySearchtreeisabinarytreeinwhicheachinternalnodexstoresanelementsuch
thattheelementstoredintheleftsubtreeofxarelessthanorequaltoxandelementsstored intherightsubtreeofxaregreaterthanorequaltox.Thisiscalledbinarysearchtree property. Thebasicoperationsonabinarysearchtreetaketimeproportionaltotheheightofthetree. Foracompletebinarytreewithnoden,suchoperationsrunsin (lgn)worstcasetime.If thetreeisalinearchainofnnodes,however,thesameoperationstakes time.

(n)worstcase

TheheightoftheBinarySearchTreeequalsthenumberoflinksfromtherootnode tothedeepestnode.

ImplementationofBinarySearchTree
BinarySearchTreecanbeimplementedasalinkeddatastructureinwhicheachnodeisan objectwiththreepointerfields.Thethreepointerfieldsleft,rightandppointtothenodes

correspondingtotheleftchild,rightchildandtheparentrespectivelyNILinanypointer fieldsignifiesthatthereexistsnocorrespondingchildorparent.Therootnodeistheonly nodeintheBTSstructurewithNILinitspfield.


InorderTreeWalk
Duringthistypeofwalk,wevisittherootofasubtreebetweentheleftsubtreevisitand rightsubtreevisit.

INORDERTREEWALK(x) Ifx NILthen INORDERTREEWALK(left[x]) printkey[x] INORDERTREEWALK(right[x])

Ittakes (n)timetowalkatreeofnnodes.NotethattheBinarySearchTreeproperty allowsustoprintoutalltheelementsintheBinarySearchTreeinsortedorder.

PreorderTreeWalk
Inwhichwevisittherootnodebeforethenodesineithersubtree.

PREORDERTREEWALK(x) IfxnotequalNILthen PRINTkey[x] PREORDERTREEWALK(left[x]) PREORDERTREEWALK(right[x])

PostorderTreeWalk
Inwhichwevisittherootnodeafterthenodesinitssubtrees.

POSTORDERTREEWALk(x) IfxnotequalNILthen POSTORDERTREEWALK(left[x]) PREORDERTREEWALK(right[x]) PRINTkey[x]


IttakesO(n)timetowalk(inorder,preorderandpastorder)atreeofnnodes.

BinarySearchTreepropertyVsHeapProperty

Inaheap,anodeskeyisgreaterthanequaltobothofitschildren'skeys.Inbinarysearch tree,anode'skeyisgreaterthanorequaltoitschild'skeybutlessthanorequaltoright child'skey.Furthermore,thisappliestoentiresubtreeinthebinarysearchtreecase.Itis veryimportanttonotethattheheappropertydoesnothelpprintthenodesinsortedorder becausethispropertydoesnottellusinwhichsubtreethenextitemis.Iftheheapproperty couldusedtoprintthekeys(aswehaveshownabove)insortedorderinO(n)time,this wouldcontradictourknownlowerboundoncomparisonsorting. Thelaststatementimpliesthatsincesortingnelementstakes(nlgn)timeintheworst caseinthecomparisonmodel,anycomparisonbasedalgorithmforconstructingaBinary SearchTreefromarbitrarylistnelementstakes(nlgn)timeintheworstcase. Wecanshowthevalidityofthisargument(incaseyouarethinkingofbeating(nlgn) bound)asfollows:letc(n)betheworstcaserunningtimeforconstructingabinarytreeof asetofnelements.GivenannnodeBST,theinorderwalkinthetreeoutputsthekeysin sortedorder(shownabove).Sincetheworstcaserunningtimeofanycomputationbased sortingalgorithmis(nlgn),wehave c(n)+O(n)=(nlgn)

Therefore,c(n)=(nlgn).

QueryingaBinarySearchTree
ThemostcommonoperationsperformedonaBSTissearchingforakeystoredinthetree. OtheroperationsareMINIMUM,MAXIMUM,SUCCESSORandPREDESESSOR.These operationsruninO(h)timewherehistheheightofthetreei.e.,histhenumberoflinks rootnodetothedeepestnode. TheTREESEARCH(x,k)algorithmsearchesthetreerootatxforanodewhosekeyvalue equalsk.ItreturnsapointertothenodeifitexistsotherwiseNIL

TREESEARCH(x,k)

ifx=NIL.OR.k=key[x] thenreturnx ifk<key[x] thenreturnTREESEARCH (left[x],k) elsereturnTREESEARCH (right[x],k)


Clearly,thisalgorithmrunsinO(h)timewherehistheheightofthetree. Theiterativeversionofabovealgorithmisveryeasytoimplement.

ITERATIVETREESEARCH(x,k)
1. whilexnotequalNIL.AND.key

key[x]do 2. ifk<key[x] 3. thenxleft[x] 4. elsexright[x] 5. returnx

TheTREEMINIMUN(x)algorithmreturnsapointtothenodeofthetreeatxwhosekey valueistheminimumofallkeysinthetree.DuetoBSTproperty,anminimumelementcan alwaysbefoundbyfollowingleftchildpointersfromtherootuntilNILisuncountered.

TREEMINIMUM(x) whileleft[x]NILdo xleft[x] returnx

Clearly,itrunsinO(h)timewherehistheheightofthetree.AgainthankstoBST property,anelementinabinarysearchtreewhosekeyisamaximumcanalwaysbefound byfollowingrightchildpointersfromrootuntilaNILisencountered.

TREEMAXIMUM(x) whileright[x]NILdo xright[x] returnx


Clearly,itrunsinO(h)timewherehistheheightofthetree. TheTREESUCCESSOR(x)algorithmreturnsapointertothenodeinthetreewhosekey valueisnexthigherthankey[x].

TREESUCCESSOR(x) ifright[x]NIL thenreturnTREEMINIMUM (right[x]) elseyp[x] whileyNIL.AND.x= right[y]do xy yp[y] returny NotethatalgorithmTREEMINIMUM,TREMAXIMUM,TREESUCCESSOR,and
TREEPREDESSORneverlookatthekeys. AninordertreewalkofannnodeBSTcanbeimplementedin (n)timebyfindingthe minimumelementinthetreewithTREEMINIMUM(x)algorithmandthenmakingn1 callstoTREESUCCESSOR(x).

AnotherwayofImplementingInorderwalkonBinarySearch

Tree Algorithm findtheminimumelementinthetreewith TREEMINIMUM Maken1callstoTREESUCCESSOR

Letusshowthatthisalgorithmrunsin (n)time.ForatreeT,letmTbethenumberof edgesthataretraversedbytheabovealgorithm.TherunningtimeofthealgorithmforTis (mT).Wemakefollowingclaim: mTiszeroifThasatmostonenodeand2erotherwise, whereeis thenumberofedgesinthetreeandristhelengthofthe pathfrom roottothenodeholdingthemaximumkey. Notethate=n1foranytreewithatleastonenode.Thisallowsustoprovetheclaim byinductionone(andtherefore,onn).

BasecaseSupposethate=0.Then,eitherthetreeisemptyorconsistsonlyofasingle node.So,e=r=0.Therefore,theclaimholds. InductivestepSupposee>0andassumethattheclaimholdsforalle'<e.LetTbe abinarysearchtreewitheedges.Letxbetheroot,andT1andT2respectivelybetheleft andrightsubtreeofx.SinceThasatleastoneedge,eitherT1orT2respectivelyis nonempty.Foreachi=1,2,leteibethenumberofedgesinTi,pithenodeholdingthe maximumkeyinTi,andrithedistancefrompitotherootofTi.Similarly,lete,pandr bethecorrespoundingvaluesforT.FirstassumethatbothT1andT2arenonempty.Thene


=e1+e2+2,p=p2,andr=r2+1.Theactionoftheenumerationisasfollows: Uponbeingcalled,theminimumtree(x)traversestheleftbranchofxandentersT1.

OncetherootofT1isvisited,theedgesofT1aretraversedasifT1istheinputtree. Thissituationwilllastuntilp1isvisisted. WhentheTreeSuccessoriscalledformp1.Theupwardpathfromp1andxis traversedandxisdiscoveredtoholdthesuccessor. WhenthetreeSuccessorcalledfromx,therightbranchofxistaken. OncetherootofT2isvisited,theedgesofT2aretraversedasifT2istheinputtree. Thissituationwilllastuntilp2isreached,wherebythealgorithmhalts. Bytheaboveanalysis,thenumberofedgesthataretraversedbytheabovealgorithm,mT, is

mT=1+(2e1r1)+(r1+1)+1+(2e2r2) =2(e1+e2+2)(r2+1) =2er


Therefore,theclaimclearlyholdsforthiscase. NextsupposethatT2isemply.Sincee>0,T1isnonempty.Thene=e1+1.Sincex doesnothavearightchild,xholdsthemaximum.Therefore,p=xandr=0.Theaction oftheenumerationalgorithmisthefirsttwosteps.Therefore,thenumberofedgesthatare traversedbythealgorithminquestionis

mT=1+(2e1r1)+(r1+1) =2(e1+1)0 =2er


Therefore,theclaimholdsforthiscase. Finally,assumethatT1isempty.ThenT2isnonempty.Itholdsthate=e2+1,p=

p2,andr=r2+1.Thistimexholdstheminimumkeyandtheactionofthe
enumerationalgorithmisthelasttwosteps.Therefore,thenumberofedgesthatare traversedbythealgorithmis

mT=1+(2e2r2)

=2(e2+1)(r2+1) =2er
Therefore,theclaimholdsforthiscase. Theclaimisprovensincee=n1,mT

2n.Ontheotherhand,atleastoneedgehas tobetraversedwhengoingfromonnodetoanother,somT n1.Therefore,the runningtimeoftheabovealgorithmis (n).


ConsideranybinarysearchtreeTandletybetheparentofaleafz.Ourgoalistoshow thatkey[y]is

eitherthesmallestkeyinTlargerthankey[x] orthelargestkeyintheTsmallerthankey[x].

ProofSupposethatxisaleftchildofy.Sincekey[y] key[x],onlywehaveto showthatthereisnonodezwithkey[y]>key[z]>key[x].Assume,tothecontrary,that thereissuchaz.Choosezsothatitholdsthesmallestkeyamongsuchnodes.Notefor everynodeu z,x,key[z] dey[u]ifandonlyifkey[x] key[u].Ifwesearchkey[z], thenthesearchpathisidenticaltothatofkey[x]untilthepathrearcheszorx.Sincexisa leaf(meaningithasnochildren),thesearchpathneverreachesx.Therefore,zisan ancestorofx.Sinceyistheparentofx(itisgiven,incaseyou'veforgotton!)andisnotz, zhastobeanancestorofy.So,key[y]>dey[z]>dey[x].However,weareassuming key[y]>key[z]>key[x],sothisisclearlyimpossible.Therefore,thereisnosuchz.
Thecasewhenxisarightchildofyiseasy.Hint:symmetric.

INSERTION
ToinsertanodeintoaBST

1. findaleafsttheappropriateplaceand 2. connectthenodetotheparentoftheleaf.

TREEINSERT(T,z) yNIL xroot[T] whilexNILdo yx ifkey[z]<key[x] thenxleft[x] elsexright[x] p[z]y ify=NIL thenroot[T]z elseifkey[z]<key[y] thenleft[y]z elseright[y]z

Likeotherprimitiveoperationsonsearchtrees,thisalgorithmbeginsattherootofthetree andtracesapathdownward.Clearly,itrunsinO(h)timeonatreeofheighth.

Sorting
Wecansortagivensetofnnumbersbyfirstbuildingabinarysearchtreecontainingthese numberbyusingTREEINSERT(x)procedurerepeatedlytoinsertthenumbersonebyone andthenprintingthenumbersbyaninordertreewalk.

Analysis
Bestcaserunningtime

PrintingtakesO(n)timeandninsertioncostO(lgn)each(treeisbalanced,halfthe insertionsareatdepthlg(n)1).ThisgivesthebestcaserunningtimeO(nlgn).

Worstcaserunningtime PrintingstilltakesO(n)timeandninsertioncostingO(n)each(treeisasinglechain ofnodes)isO(n2).Theninsertioncost1,2,3,...n,whichisarithmeticsequence soitisn2/2.

Deletion
RemovinganodefromaBSTisabitmorecomplex,sincewedonotwanttocreateany "holes"inthetree.Ifthenodehasonechildthenthechildissplicedtotheparentofthe node.Ifthenodehastwochildrenthenitssuccessorhasnoleftchildcopythesuccessor intothenodeanddeletethesuccessorinsteadTREEDELETE(T,z)removesthenode pointedtobyzfromthetreeT.ITreturnsapointertothenoderemovedsothatthenode canbeputonafreenodelist,etc.

TREEDELETE(T,z)
1. ifleft[z]=NIL.OR.right[z]=NIL 2. thenyz 3. elseyTREESUCCESSOR(z) 4. ifleft[y]NIL 5. thenxleft[y] 6. elsexright[y] 7. ifxNIL 8. thenp[x]p[y] 9. ifp[y]=NIL 10. thenroot[T]x 11. elseify=left[p[y]] 12. thenleft[p[y]]x

13. elseright[p[y]]x 14. ifyz 15. thenkey[z]key[y] 16. ifyhasotherfield,copythem,too 17. returny

TheprocedurerunsinO(h)timeonatreeofheighth.

Das könnte Ihnen auch gefallen