Red-Black Tees 8 00 Goodich, Tamassia Red-Black Tees 1
Fom (,) to Red-Black Tees A ed-black tee is a epesentation of a (,) tee by means of a binay tee whose nodes ae coloed ed o black In compaison with its associated (,) tee, a ed-black tee has same logaithmic time pefomance simple implementation with a single node type 5 7 5 OR 5 7 00 Goodich, Tamassia Red-Black Tees
Red-Black Tees A ed-black tee can also be defined as a binay seach tee that satisfies the following popeties: Root Popety: the oot is black Extenal Popety: eey leaf is black Intenal Popety: the childen of a ed node ae black Depth Popety: all the leaes hae the same black depth 9 15 1 1 7 00 Goodich, Tamassia Red-Black Tees
Height of a Red-Black Tee Theoem: A ed-black tee stoing n enties has height O(log n) Poof: The height of a ed-black tee is at most twice the height of its associated (,) tee, which is O(log n) The seach algoithm fo a binay seach tee is the same as that fo a binay seach tee By the aboe theoem, seaching in a ed-black tee takes O(log n) time 00 Goodich, Tamassia Red-Black Tees
Insetion To pefom opeation put(k, o), we execute the insetion algoithm fo binay seach tees and colo ed the newly inseted node unless it is the oot We pesee the oot, extenal, and depth popeties If the paent of is black, we also pesee the intenal popety and we ae done Else ( is ed ) we hae a double ed (i.e., a iolation of the intenal popety), which equies a eoganiation of the tee Example whee the insetion of causes a double ed: 8 8 00 Goodich, Tamassia Red-Black Tees 5
Remedying a Double Red Conside a double ed caused by adding new child, paent, and let w be the sibling of (i.e. s aunt ) Case 1: w is black The double ed is an incoect eplacement of a -node Restuctuing: we change the -node eplacement Case : w is ed The double ed coesponds to an oeflow Recoloing: we pefom the equialent of a split w 7 w 7 7 7.... 00 Goodich, Tamassia Red-Black Tees
Restuctuing A estuctuing emedies a child-paent double ed when the paent ed node has a black sibling It is equialent to estoing the coect eplacement of a -node The intenal popety is estoed and the othe popeties ae peseed w 7 7 w 7 7........ 00 Goodich, Tamassia Red-Black Tees 7
Restuctuing (cont.) Thee ae fou estuctuing configuations depending on whethe the double ed nodes ae left o ight childen 00 Goodich, Tamassia Red-Black Tees 8
Recoloing A ecoloing emedies a child-paent double ed when the paent ed node has a ed sibling The paent and its sibling w become black and the gandpaent u becomes ed, unless it is the oot It is equialent to pefoming a split on a 5-node The double ed iolation may popagate to the gandpaent u w 7 w 7 7 7 00 Goodich, Tamassia Red-Black Tees 9
Analysis of Insetion Algoithm put(k, o) 1. We seach fo key k to locate the insetion node. We add the new enty (k, o) at node and colo ed. while doublered() if isblack(sibling(paent())) estuctue() etun else { sibling(paent() is ed } ecolo() Recall that a ed-black tee has O(log n) height Step 1 takes O(log n) time because we isit O(log n) nodes Step takes O(1) time Step takes O(log n) time because we pefom O(log n) ecoloings, each taking O(1) time, and at most one estuctuing taking O(1) time Thus, an insetion in a edblack tee takes O(log n) time 00 Goodich, Tamassia Red-Black Tees 10
Deletion To pefom opeation ease(k), we fist execute the deletion algoithm fo binay seach tees Let be the intenal node emoed, w the extenal node emoed (thee must be at least one), and the sibling of w If eithe o was ed, we colo black and we ae done Else ( and wee both black) we colo double black, which is a iolation of the depth, popety equiing a eoganiation of the tee Example whee the deletion of 8 causes a double black: 8 w 00 Goodich, Tamassia Red-Black Tees 11
Remedying a Double Black The algoithm fo emedying a double black node with sibling y consides thee cases Case 1: y is black and has a ed child We pefom a estuctuing, equialent to a tansfe, ecolo, and we ae done Case : y is black and its childen ae both black We pefom a ecoloing, equialent to a fusion, which may popagate up the double black iolation Case : y is ed We pefom an adjustment, equialent to choosing a diffeent epesentation of a -node, afte which eithe Case 1 o Case applies Deletion in a ed-black tee takes O(log n) time 00 Goodich, Tamassia Red-Black Tees 1
Deletion: Case 1 Case 1: sibling y of is black and has a ed child We pefom a estuctuing, equialent to a tansfe, and we ae done y y x 8 x 8 y x 8 y x 8 00 Goodich, Tamassia Red-Black Tees 1
Deletion: Case Case : sibling y is black and its childen ae both black We pefom a ecoloing, equialent to a fusion, which may popagate up the double black iolation y y 00 Goodich, Tamassia Red-Black Tees 1
Deletion: Case Case : sibling y of is ed We pefom an adjustment, equialent to a estuctuing, which conets the stuctue to a fom of Case 1 o Case Take child of y on same side as y is of x: do tinode estuctuing, then ecolo x ed, y black. Sibling of is now black: hae Case 1 o y x y 8 x 8 00 Goodich, Tamassia Red-Black Tees 15
Red-Black Tee Reoganiation Insetion emedy double ed Red-black tee action (,) tee action esult estuctuing ecoloing Deletion change of -node epesentation split emedy double black Red-black tee action (,) tee action esult double ed emoed double ed emoed o popagated up estuctuing tansfe double black emoed ecoloing adjustment fusion change of -node epesentation double black emoed o popagated up estuctuing o ecoloing follows 00 Goodich, Tamassia Red-Black Tees 1