RecalltherulesforBSTdeletion RedBlackTrees Top-DownDeletion
Whatcangowrong? 1. Ifthedelete Ifthedeletenod nodeisre eisred? d? Notaproblem– noRBpropertiesviolated 2. Ifthedelete Ifthedeletedno dnodeisb deisblack? lack? Ifthenodeisnottheroot,deletingitwill changetheblack-heightalongsomepath
Bottom-Upvs.Top-Down • Bottom-Upisrecursive – BSTdeletiongoingdownthetree(windingup therecursion) – FixingtheRBpropertiescomingbackupthe tree(unwindingtherecursion)
1. Ifvertextobedeletedisaleaf,justdelete it. 2. Ifvertextobedeletedhasjustonechild, replaceitwiththatchild 3. Ifvertextobedeletedhastwochildren, replacethevalue ofbyit’sin-order predecessor’svaluethendeletetheinorderpredecessor(arecursivestep)
ThegoalofT-DDeletion • Todeletear Todeletearedle edleaf af • Howdoweensurethat’swhathappens? – Aswetraversethetreelookingfortheleafto delete,wechangeeverynodeweencounterto red. – IfthiscausesaviolationoftheRBproperties, wefixit
Terminology • MatchingWeisstextsection12.2 – Xisthenodebeingexamine Xisthenodebeingexamined d – TisX’ssibling TisX’ssibling – PisX’s(andT’s)parent PisX’s(andT’s)parent – RisT’srightchild RisT’srightchild – LisT’sleftchild LisT’sleftchild
• Top-Downisiterative – Restructurethetreeonthewaydownsowe don’thavetogobackup
• ThisdiscussionassumesXistheleftchildofP. Asusual,thereareleft-rightsymmetriccases.
1
BasicStrategy
Step1– Examinetheroot
• Aswetraversethetree,wechangeevery nodewevisit,X,toRed.
1. Ifbothoftheroot’schildrenareBlack a. MaketherootRed
• WhenwechangeXtoRed,weknow
b. MoveXtotheappropriatechildoftheroot c. Proceedtostep2
– PisalsoRed(wejustcamefromthere)
2. OtherwisedesignatetherootasXand proceedtostep2B.
– Tisblack(sincePisRed,it’schildrenare Black)
Step2– themaincase
Case2A XhastwoBlackChildren
Aswetraversedownthetree,wecontinually encounterthissituationuntilwereachthe nodetobedeleted XisBlack,PisRed,TisBlack
2A1.Thas2BlackChildren
P X
2A2.T’sleftchildisRed
T
WearegoingtocolorXRed,thenrecolor othernodesandpossiblydorotation(s) basedonthecolorofX’sandT’schildren 2A.Xhas2Blackchildren 2B.XhasatleastoneRedchild
2A3.T’srightchildisRed **ifbothofT’schildrenareRed, wecandoeither2A2or2A3
Case2A1 XandThave2BlackChildren
Case2A2 Xhas2BlackChildrenandT’sLeftChildisRed
P
RotateLaroundT,thenLaroundP RecolorXandPthencontinuedownthetree
P
L X
T
X
P
T X L
T
P
T X
L1
L2
JustrecolorX,PandTandmovedownthetree L1
L2
2
Case2B XhasatleastoneRedchild
Case2A3 Xhas2BlackChildrenandT’sRightChildisRed RotateTaroundP RecolorX,P,TandRthencontinuedownthetree
Continuedownthetreetothenextlevel IfthenewXisRed,continuedownagain
T
P X
T
IfthenewXisBlack(TisRed,PisBlack) R
P
RotateTaroundP RecolorPandT
R R1
X L
R2
Backtomaincase– step2
L R1
R2
Case2BDiagram
Step3
P X
Eventually,findthenodetobedeleted– aleaforanodewith onenon-nullchildthatisaleaf.
T
DeletetheappropriatenodeasaRedleaf Movedownthetree. P
Step4 ColortheRootBlack
P
X
T
T
X
IfmovetoBlackchild(2B2) IfmovetotheRedchild(2B1) RotateTaroundP;RecolorPandT Movedownagain Backtostep2,themaincase
Example1 Delete10fromthisRBTree
Example1(cont’d)
15
15
6
X
17 12
3 10
20
16 13
18
7
Step1– Roothas2Blackchildren.ColorRootRed Descendthetree,movingXto6
6
17 12
3 10
23
20
16 13
18
23
7
OneofX’schildrenisRed(case2B).Descenddownthe tree,arrivingat12.SincethenewX(12)isalsoRed(2B1), continuedownthetree,arrivingat10.
3
Example1(cont’d)
Example1(cont’d)
15
15 6
6
17 12
3 10
13
X
20
16 18
17 12
3 23
13
7
20
16 18
23
7
Step3-Since10isthenodetobedeleted,replaceit’svalue withthevalueofit’sonlychild(7)anddelete7’srednode
Thefinaltreeafter7hasreplaced10and7’srednode deletedand(step4)theroothasbeencoloredBlack.
Example2 Delete10fromthisRBTree
Example2(cont’d)
15
X
6
17 12
3 2
4
10
6 20
16
15
12
3 2
13
17
4
10
20
16 13
XhasatleastoneRedchild(case2B).Proceeddown thetree,arrivingat6.Since6isalsoRed(case2B1), continuedownthetree,arrivingat12.
Step1– therootdoesnothave2Blackchildren. Colortherootred,SetX=rootandproceedtostep2
Example2(cont’d)
Example2(cont’d) 15
15 6
3
6
P
T
X
4
10
12
16 13
Xhas2Blackchildren.X’ssibling(3)alsohas2blackchildren. Case2A1– recolorX,P,andTandcontinuedownthetree,arriving at10.
P
3
20 2
2
17
17
4
X
10
12
T
16
20
13
Xisnowtheleaftobedeleted,butit’sBlack,sobacktostep 2. Xhas2BlackchildrenandThas2Blackchildren– case2A1 RecolorX,PandT. Step3-- Nowdelete10asaredleaf. Step4-- Recolortherootblack
4
Example3 Delete11fromthisRBTree
Example2Solution 15
15 6
17 10 12
3
16
20 12
5 2
4
13 3 2
7 4
11 6
Validand unaffected Rightsubtree
13
9
Step1– roothas2Blackchildren.ColorRootred. SetXtoappropriatechildofroot(10)
Example3(cont’d)
Example3(cont’d)
15 10
10 12
5 3 2
7 4
15
X 5
11 6
3
13
9
2
XhasoneRedchild(case2B) Traversedownthetree,arrivingat12.
P
T
X
7 4
12
11 6
13
9
Sincewearrivedatablacknode(case2B2)assuringTisred andPisblack),rotateTaroundP,recolorTandP Backtostep2
Example3(cont’d)
Example3(cont’d) 15
15 5 5 10 3
7
10
P
3
X
T
4
6
9
12
12 2
2
P
7
11
13
NowXisBlackwithRedparentandBlacksibling. XandTbothhave2Blackchildren(case2A1) JustrecolorX,PandTandcontinuetraversal
4
6
9
11
X
13
T
Havingtraverseddownthetree,wearriveat11,theleafto bedeleted,butit’sBlack,sobacktostep2. XandTbothhavetwoBlackchildren.RecolorX,PandT. Step3-- delete11asaredleaf. Step4-- Recolortherootblack
5
Example3Solution 15
5 10 3
2
7
4
6
12
9
13
6