2-3-4 Trees ad ed- Black Trees erm 204
2-3-4 Trees evealed Nodes store 1, 2, or 3 keys ad have 2, 3, or 4 childre, respectively All leaves have the same depth k b e h r a c d f i l m p s x 1 -- lo( N + 1) heiht lo( N + 1) 2 erm 205
2-3-4 Tree Nodes Itroductio of odes with more tha 1 key, ad more tha 2 childre 2-ode: same as a biary ode <a >a a b 3-ode: 2 keys, 3 liks <a >b a >a <b 4-ode: 3 keys, 4 liks a b c <a >c >a <b >b <c erm 206
Why 2-3-4? Why ot miimize heiht by maximizi childre i a d-tree? Let each ode have d childre so that we et (lo d N) search time! iht? lo d N = lo N/lo d That meas if d = N 1/2, we et a heiht of 2 However, searchi out the correct child o each level requires (lo N 1/2 ) by biary search 2 lo N 1/2 = (lo N) which is ot as ood as we had hoped for! 2-3-4-trees will uaratee (lo N) heiht usi oly 2, 3, or 4 childre per ode erm 207
Isertio ito 2-3-4 Trees Isert the ew key at the lowest iteral ode reached i the search 2-ode becomes 3-ode d d 3-ode becomes 4-ode f d d f What about a 4-ode? We ca t isert aother key! erm 208
Top ow Isertio I our way dow the tree, wheever we reach a 4-ode, we break it up ito two 2- odes, ad move the middle elemet up ito the paret ode e e f d f d Now we ca perform the isertio usi oe of the previous two cases ice we follow this method from the root dow to the leaf, it is called top dow isertio d e f erm 209
plitti the Tree As we travel dow the tree, if we ecouter ay 4-ode we will break it up ito 2-odes. This uaratees that we will ever have the problem of iserti the middle elemet of a former 4- ode ito its paret 4-ode. c t a f i l p r x Whoa, cowboy c t a f i l p r x erm 210
c t a f i l p r x c t Whoa, cowboy a f i l p r x erm 211
c t a f i l p r x Whoa, cowboy c i t a f l p r x c i t a f l p r x erm 212
Time omplexity of Isertio i 2-3-4 Trees Time complexity: A search visits (lo N) odes A isertio requires (lo N) ode splits ach ode split takes costat time Hece, operatios earch ad Isert each take time (lo N) Notes: Istead of doi splits top-dow, we ca perform them bottom-up starti at the isertio ode, ad oly whe eeded. This is called bottom-up isertio. A deletio ca be performed by fusi odes (iverse of splitti), ad takes (lo N) time erm 213
Beyod 2-3-4 Trees What do we kow about 2-3-4 Trees? Balaced iskel (lo N) search time bert ifferet ode structures oberto a we et 2-3-4 tree advataes i a biary tree format??? Welcome to the world of ed-black Trees!!! erm 214
ed-black Tree A red-black tree is a biary search tree with the followi properties: edes are colored red or black o two cosecutive red edes o ay root-leaf path same umber of black edes o ay root-leaf path (= black heiht of the tree) edes coecti leaves are black Black de ed de erm 215
2-3-4 Tree volutio Note how 2-3-4 trees relate to red-black trees 2-3-4 ed-black or Now we see red-black trees are just a way of represeti 2-3-4 trees! erm 216
More ed-black Tree Properties N # of iteral odes L # leaves (= N + 1) H heiht B black heiht Property 1: 2 B N + 1 4 B Property 2: Property 3: 1 -- lo( N+ 1) B lo( N + 1) 2 lo( N + 1) H 2 lo( N + 1) This implies that searches take time (lon)! erm 217
Isertio ito ed-black Trees 1.Perform a stadard search to fid the leaf where the key should be added 2.eplace the leaf with a iteral ode with the ew key 3.olor the icomi ede of the ew ode red 4.Add two ew leaves, ad color their icomi edes black 5.If the paret had a icomi red ede, we ow have two cosecutive red edes! We must reoraize tree to remove that violatio. What must be doe depeds o the sibli of the paret. G G erm 218
Isertio - Plai ad imple Let: be the ew ode p be its paret be its radparet ase 1: Icomi ede of p is black No violatio p TP! Pretty easy, huh? Well... it ets messier... erm 219
estructuri ase 2: Icomi ede of p is red, ad its sibli is black Uh oh! p Much Better! p We call this a riht rotatio No further work o tree is ecessary Iorder remais uchaed Tree becomes more balaced No two cosecutive red edes! erm 220
More otatios imilar to a riht rotatio, we ca do a left rotatio... p p imple, huh? erm 221
ouble otatios What if the ew ode is betwee its paret ad radparet i the iorder sequece? We must perform a double rotatio (which is o more difficult tha a sile oe) p p This would be called a left-riht double rotatio erm 222
Last of the otatios Ad this would be called a riht-left double rotatio p p erm 223
Bottom-Up ebalaci ase 3: Icomi ede of p is red ad its sibli is also red p p We call this a promotio Note how the black depth remais uchaed for all of the descedats of This process will cotiue upward beyod if ecessary: reame as ad repeat. erm 224
ummary of Isertio Iftwo red edes are preset, we do either a restructuri (with a simple or double rotatio) ad stop, or apromotio ad cotiue Arestructuri takes costat time ad is performed at most oce. It reoraizes a off-balaced sectio of the tree. Promotios may cotiue up the tree ad are executed (lo N) times. The time complexity of a isertio is (lon). erm 225
A xample tart by iserti ito a empty tree Now, let s isert U B... erm 226
A ool xample erm 227
A Ubelievable xample U h No! What should we do? U erm 228
ouble otatio U U erm 229
B A Beautiful xample U What ow? U B erm 230
otatio U B B U erm 231
A uper xample B U B U Holy osecutive ed des, Batma! We could ve placed it o either side erm 232
B U Use the Bat-Promoter!! BIFF! B U erm 233
otatio B U The UN lab ad ed-bat trees are safe......for ow!!! U B erm 234