10.07.2015 Views

2-3-4 Trees and Red/Black Trees

2-3-4 Trees and Red/Black Trees

2-3-4 Trees and Red/Black Trees

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Definition‣ 2-3-4 trees are very much like 2-3 treesbut form the basis of a balanced,binary tree representation called <strong>Red</strong>-<strong>Black</strong> (RB) trees which are commonlyused [used in C++ STL map & set]– We study them mainly to easeunderst<strong>and</strong>ing of RB trees‣ 2-3-4 Tree is a tree where– Non-leaf nodes have 1 value & 2 children or2 values & 3 children or 3 values & 4children– All leaves are at the same level‣ Like 2-3 trees, 2-3-4 trees are alwaysfull <strong>and</strong> thus have an upper bound ontheir height of log 2 (n)a 2 Node2 4a 4 Node5 10 20a 3 Node2 4a valid 2-3-4 tree5 10 207 2© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved2


2-3-4 Search <strong>Trees</strong>‣ Similar properties as a 2-3Search Tree‣ 4 Node:– Left subtree nodes are < l– Middle-left subtree > l <strong>and</strong> < r– Right subtree nodes are > rm l&&< rr>ra 4 Nodel m r l&&< m> m&&< r>r© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved4


2-3-4 Insertion Algorithm‣ Key: Rather than search down the tree <strong>and</strong> then possibly promote <strong>and</strong>break up 4-nodes on the way back up, split 4 nodes on the way down‣ To insert a value,– 1. If node is a 4-node• Split the 3 values into a left 2-node, a right 2-node, <strong>and</strong> promote the middleelement to the parent of the node (which definitely has room) attaching childrenappropriately• Continue on to next node in search order– 2a. If node is a leaf, insert the value– 2b. Else continue on to the next node in search tree order‣ Insert 60, 20, 10, 30, 25, 50, 80Key: 4-nodes getsplit as you walkdown thus, a parentwill always haveroom for a valueEmpty Add 60 Add 20Add 10Add 3020206020 6010 20 6010 6010 30 60© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved5


2-3-4 Insertion Algorithm‣ Key: Split 4 nodes on the way down‣ To insert a value,– 1. If node is a 4-node• Split the 3 values into a left 2-node, a right 2-node, <strong>and</strong> promote the middleelement to the parent of the node (which definitely has room) attaching childrenappropriately• Continue on to next node in search order– 2a. If node is a leaf, insert the value– 2b. Else continue on to the next node in search tree order‣ Insert 60, 20, 10, 30, 25, 50, 80Key: 4-nodes getsplit as you walkdown thus, a parentwill always haveroom for a valueAdd 2520Add 5020Split first,then add 5020 3010 25 30 605010 25 30 6010 25 50 60© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved6


2-3-4 Insertion Algorithm‣ Key: Split 4 nodes on the way down‣ To insert a value,– 1. If node is a 4-node• Split the 3 values into a left 2-node, a right 2-node, <strong>and</strong> promote the middleelement to the parent of the node (which definitely has room) attaching childrenappropriately• Continue on to next node in search order– 2a. If node is a leaf, insert the value– 2b. Else continue on to the next node in search tree order‣ Insert 60, 20, 10, 30, 25, 50, 80Key: 4-nodes getsplit as you walkdown thus, a parentwill always haveroom for a valueAdd 8020 308020 3010 25 50 6010 25 50 60 80© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved7


2-3-4 Insertion Exercise 1Add 5520 3010 25 50 60 80© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved8


2-3-4 Insertion Exercise 2Add 5820 30 6010 2550 55 80© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved9


2-3-4 Insertion Exercise 3Add 573020 6010 25 50 55 58 80© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved10


2-3-4 Insertion Exercise 3Resulting Tree3020 55 6010 25 50 57 58 80© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved11


2-3-4 Tree Resources‣ http://ultrastudio.org/en/2-3-4_tree© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved12


TREE ROTATIONS© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved13


BST Subtree Ranges‣ Consider a binary search tree, what range of values couldbe in the subtree rooted at each nodeWhat valuesmight be inthe subtreerooted here– At the root, any value could be in the "subtree"– At the first left child?– At the first right child?( )(-inf, inf)yzd( )(-inf,inf)( )axy( )( )x( )c( )bz( )( ) ( )a b( )c( )d© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved14


BST Subtree Ranges‣ Consider a binary search tree, what range of values couldbe in the subtree rooted at each node– At the root, any value could be in the "subtree"– At the first left child?– At the first right child?(-inf, inf)z(-inf, inf)x(-inf, z)yd(z, inf)(-inf, x)a(x, inf)y(-inf, y)x(y,z)c(x, y)bz(y, inf)(-inf, x)a(x,y)b(y,z)c(z,inf)d© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved15


Right Rotation‣ Define a right rotation as taking a left child, making it theparent <strong>and</strong> making the original parent the new right child‣ Where do subtrees a, b, c <strong>and</strong> d belong?– Use their ranges to reason about it…(-inf, inf)(-inf, z)yz(z, inf)dRightrotate of yxyz(-inf, y)x(y,z)c___ ______ ___(-inf, x)a(x,y)b© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved16


Right Rotation‣ Define a right rotation as taking a left child, making it theparent <strong>and</strong> making the original parent the new right child‣ Where do subtrees a, b, c <strong>and</strong> d belong?– Use their ranges to reason about it…(-inf, inf)(-inf, z)yz(z, inf)dRightrotate of yxyz(-inf, y)(-inf, x)ax(y,z)c(x,y)bab(-inf, x) (x,y)cd(y,z) (z, inf)© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved17


Left Rotation‣ Define a left rotation as taking a right child, making it theparent <strong>and</strong> making the original parent the new left child‣ Where do subtrees a, b, c <strong>and</strong> d belong?– Use their ranges to reason about it…(-inf, inf)xyzLeftrotate of y(-inf, x)ax(x, inf)y___ ______ ___(x, y)bz(y, inf)(y,z)c(z,inf)d© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved18


Left Rotation‣ Define a left rotation as taking a right child, making it theparent <strong>and</strong> making the original parent the new left child‣ Where do subtrees a, b, c <strong>and</strong> d belong?– Use their ranges to reason about it…(-inf, inf)xyzLeftrotate of y(-inf, x)ax(x, inf)yab(-inf, x) (x,y)cd(y,z) (z, inf)(x, y)b(y,z)cz(y, inf)(z,inf)d© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved19


Rotations‣ Define a right rotation as taking a left child, making it theparent <strong>and</strong> making the original parent the new right child‣ Where do subtrees a, b, <strong>and</strong> c belong?– Use their ranges to reason about it…(-inf, y)(-inf, x)a(-inf, inf)xy(y, inf)c(x,y)bRightrotate of xLeft rotateof y(-inf, inf)x(-inf, x)a(x, y)b(x, inf)y(y, inf)c© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved20


Rotation's Effect on Height‣ When we rotate, it serves to re-balance the treeyzhh+1Rightrotate of yxyzh+2h+3xchh h h hhh© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved21


"Balanced" Binary Search <strong>Trees</strong>RED BLACK TREES© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved22


<strong>Red</strong> <strong>Black</strong> <strong>Trees</strong>‣ A red-black tree is a binary search tree– Only 2 nodes (no 3- or 4-nodes)– Can be build from a 2-3-4 tree directly by converting each 3- <strong>and</strong>4- nodes to multiple 2-nodes‣ All 2-nodes means no wasted storage overheads‣ Yields a "balanced" BST‣ "Balanced" means that the height of an RB-Tree is at MOSTtwice the height of a 2-3-4 tree– Recall, height of 2-3-4 tree had an upper bound of log 2 (n)– Thus height or an RB-Tree is bounded by 2*log 2 n which is stillO(log 2 (n))© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved23


<strong>Red</strong> <strong>Black</strong> <strong>Trees</strong>‣ A <strong>Red</strong>-<strong>Black</strong> tree is a "transformed" 2-3-4 treeS = SmallM = MedianL = Large– 3- <strong>and</strong> 4- nodes get converted to 2 nodes as follows– <strong>Red</strong> nodes are always ones that would join with their parent to become a3- or 4-node in a 2-3-4 treea 4 Nodes m lsmla b c da 3 Nodeslslcorasl20 3010 25 50 60 80EquivalentRB-Tree© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserveda b302010 256050 80bc24


<strong>Red</strong>-<strong>Black</strong> Tree Properties‣ Valid RB-<strong>Trees</strong> maintain the invariants that…‣ 1. No path from root to leaf has two consecutive red nodes(i.e. a parent <strong>and</strong> its child cannot both be red)– Since red nodes are just the extra values of a 3- or 4-node from 2-3-4 trees you can't have 2 consecutive red nodes‣ 2. Every path from leaf to root has the same number ofblack children– Recall, 2-3-4 trees are full (same height from leaf to root for allpaths)– Also remember each 2, 3-, or 4- nodes turns into a black node plus0, 1, or 2 red node children‣ 3. (Usually) the root is black© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved25


<strong>Red</strong>-<strong>Black</strong> Insertion‣ Insertion Algorithm:– 1. Insert node into normal BST location (at aleaf location) <strong>and</strong> color it RED– 2a. If the node's parent is black (i.e. the leafused to be a 2-node) then DONE (i.e. younow have what was a 3- or 4-node)– 2b. Else perform fixTree transformations thenrepeat step 2 on the parent or gr<strong>and</strong>parent(whoever is red)‣ fixTree involves either– recoloring or– 1 or 2 rotations <strong>and</strong> recoloring‣ Which case of fixTree you performdepends on the color of the new node's"aunt/uncle"© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reservedparentxgr<strong>and</strong>parent10203040aunt/uncle26


fixTree Cases1.RecolorGGGPUPUNP G UN PUNcNcabab2.RecolorPGUPGUP N G UP NGUaNaN3.RecolorRootbcRbRcNote: For insertion/removalalgorithm we consider nonexistentleaf nodes as blacknodes© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved27


fixTree Cases4.1 Rotate /RecolorNPGcURightrotate of PaNbPcGUP GN ca bUaN P GbcUab5.2 Rotates /RecolorPGULeft rotateof PNGURightrotate of G& RecolorPNGaNPcabcUbcabP GP N GaNUa b cUb c© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reservedwww.cse.ohio-state.edu/~gurari/course/cis680Ch11.html28


Insertion‣ Insert 10, 20, 30, 15, 25, 12, 5, 3, 8Empty Insert 10 Insert 2010 10Insert 30Violates consec. reds10Case 4: Left rotate<strong>and</strong> recolor202020103030Insert 15Insert 2520202020103010301030103015Case 2:Recolor15Case 3:Recolorroot© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved1515 2529


‣ Insert 10, 20, 30, 15, 25, 12, 5, 3, 8Insert 12Insertion20202010301030123015 2512 251015 2512Case 5: RightRotate…15Case 5: …Right Rotate<strong>and</strong> recolorInsert 52020123012301015 251015 255Case 1:Recolor© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved5Recursive call "fix" on12 but it's parent isblack so we're done30


‣ Insert 10, 20, 30, 15, 25, 12, 5, 3, 8InsertionInsert 32020123012301015 25515 255Case 4: Rotate3 103© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved31


‣ Insert 10, 20, 30, 15, 25, 12, 5, 3, 8InsertionInsert 820201212301230520515 25515 253 1015303 103 108258Case 2: Recolor8Case 4: Rotate12© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved32


Insertion Exercise 1Insert 271253 108152025N30P27G© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved33


Insertion Exercise 1Insert 271212520G5203 1015303 101527825P8 25 30N27This is case 5.1. Left rotate around P2. Right rotate around N3. Recolor© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved34


Insertion Exercise 2Insert 401253 1082015A2527G30PN40© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved35


Insertion Exercise 2Insert 40121253 1082015A2527G30P5203 10 15 278 25 30N4040Aunt <strong>and</strong> Parent are thesame color. So recolor aunt,parent, <strong>and</strong> gr<strong>and</strong>parent.© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved36


Insertion Exercise 2A1253 10G1520P27N53 10121520278 25 308 25 304040Aunt <strong>and</strong> Parent are thesame color. So recolor aunt,parent, <strong>and</strong> gr<strong>and</strong>parent.© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved37


Insertion Exercise 3Insert 50125203 10 15 278 25 30G40P50N© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved38


Insertion Exercise 3Insert 5012125205203 10 15 278 25 30G40P50N3 10 15 278 25 403050Remember, empty nodes are black.Do a left rotation around P <strong>and</strong> recolor.© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved39


Insertion Exercise 4Insert 45125203 10 15 278 25 40AG30 50P45N© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved40


Insertion Exercise 4Insert 4512125205203 10 15 278 25 40AG30 50P3 10 15 278 25 40305045N45Aunt <strong>and</strong> Parent are the same color.Just recolor.© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved41


Insertion Exercise 453 1012A1520G27P8 25 4030N5045© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved42


Final Result125273 10204081525305045© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved43


Insertion Exercise 5Insert 91253 10PG89AN1520252730404550© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved44


Insertion Exercise 5125273 920408101525305045© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved45


RB TREE IMPLEMENTATION© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved46


‣ Implement private methods:– findMyUncle()– AmIaRightChild()– AmIaLeftChild()– RightRotate– LeftRotateHints• Need to change x's parent, y's parent, b's parent, x's right, y's left, x'sparent's left or right, <strong>and</strong> maybe root(-inf, inf)(-inf, inf)(-inf, y)xy(y, inf)cRightrotate of x(-inf, x)ax(x, inf)y(-inf, x) (x,y)a b© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights ReservedLeft rotateof y(x, y)bc(y, inf)47


Hints‣ You have to fix the tree after insertion if…‣ Watch out for traversing NULL pointers– node->parent->parent– However, if you need to fix the tree your gr<strong>and</strong>parent…‣ Cases break down on uncle's color– If an uncle doesn't exist (i.e. is NULL), he is (color?)…(-inf, inf)(-inf, inf)(-inf, y)xy(y, inf)cRightrotate of x(-inf, x)ax(x, inf)y(-inf, x) (x,y)a b© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights ReservedLeft rotateof y(x, y)bc(y, inf)48


FOR PRINT© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved49


fixTree Cases1.RecolorGGGPUPUNP G UN PUNcNcabab2.RecolorPGUPGUP N G UP NGUaNaN3.RecolorRootbcRbRcNote: For insertion/removalalgorithm we consider nonexistentleaf nodes as blacknodes© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reserved50


fixTree Cases4.1 Rotate /RecolorNPGcURightrotate of PaNbPcGUP GN ca bUaN P GbcUab5.2 Rotates /RecolorPGULeft rotateof PNGURightrotate of G& RecolorPNGaNPcabcUbcabP GP N GaNUa b cUb c© Copyright 2013 Brent Nash & Mark <strong>Red</strong>ekopp, All Rights Reservedwww.cse.ohio-state.edu/~gurari/course/cis680Ch11.html51

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!