balanced search trees 3.4 – 3.7 team names majed suhaim ahmed sulaiman m alharbi
Post on 02-Jan-2016
Embed Size (px)
Balanced Search Trees3.4 3.7
Team names Majed SuhaimAhmed Sulaiman M AlharbiRed-Black TreesDefinition: a binary search tree with nodes colored red and black such that:the paths from the root to any leaf have the same number of black nodes,there are no two consecutive red nodes, If a node is red, then both of its children are blackthe root is black.
Definition: The black-height of a node, x, in a red-black tree is the number of black nodes on any path to a leaf, not counting x.The height of red-black treeBlack-Height of the root = 24Black-Height of the root = 3Height-balanced treesWe have to maintain the following balancedness property
- Each path from the root to a leaf contains the same number of black nodes7318102226118bh =2bh =2bh =1bh =1bh =1bh =1bh =1Rotation
A rotation is a local operation in a search tree that preserves in-order traversal key ordering.
Bottom-Up Rebalancing for Red-Black Trees* The idea for insertion in a red-black tree is to insert like in a binary search tree and then reestablish the color properties through a sequence of recoloring and rotationsThe rules are as follows:1. If other is red, color current and other black and upper red.2. If current = upper->left 2.1 If current->right->color is black, perform a right rotation around upper and color upper->right red. 2.2 If current->right->color is red, perform a left rotation around current followed by a right rotation around upper, and color upper->right and upper->left black and upper red.3. If current = upper->right 3.1 If current->left->color is black, perform a left rotation around upper and color upper->left red. 3.2 If current->left->color is red, perform a right rotation around current followed by a left rotation around upper, and color upper->right and upper->left black and upper red.* We have 3 cases for insertion
Case 1: Recolor (uncle is red)
PGUPGUCase 2: Double Rotate: X around P then X around G.Recolor G and XXPGUSXPGSUCase 3: Single Rotate P around GRecolor P and GXPGUSPXGSUAnalysis of Insertion
A red-black tree has O(log n) heightSearch for insertion location takes O(log n) time because we visit O(log n) nodesAddition to the node takes O(1) timeRotation or recoloring takes O(log n) time because we perform* O(log n) recoloring, each taking O(1) time, and* at most one rotation taking O(1) time- Thus, an insertion in a red-black tree takes O(log n) timeDeleting a node from a red-black tree is a bit more complicated than inserting a node.
If the node is red?Not a problem no RB properties violated
If the node is black?deleting it will change the black-height along some path* We have some cases for deletionPSUVCase A:- Vs sibling, S, is Red Rotate S around P and recolor S & P
deletePSVPSVRotate S around PPVSRecolor S & P
PSUVCase B:- Vs sibling, S, is black and has two black children.Recolor S to be ReddeleteRed or Black and dont carePSVPSVRecolor S to be Red
PSUVCase C:- S is black Ss RIGHT child is RED (Left child either color)Rotate S around PSwap colors of S and P, and color Ss Right child BlackdeletePSVPSVRotate S around PPSVRecolor: Swap colors of S and P, and color Ss Right child BlackPSUVdeleteCase D:- S is Black, Ss right child is Black and Ss left child is Redi) Rotate Ss left child around Sii) Swap color of S and Ss left childPSVPSVRotate Ss left child around SPSVRecolor: Swap color of S and Ss left childAnalysis of deletion
A red-black tree has O(log n) heightSearch for deletion location takes O(log n) timeThe swaping and deletion is O(1).Each rotation or recoloring is O(1).Thus, the deletion in a red-black tree takes O(log n) timeRed Black TreesTop-Down InsertionReview of Bottom-Up InsertionIn B-Up insertion, ordinary BST insertion was used, followed by correction of the tree on the way back up to the rootThis is most easily done recursivelyInsert winds up the recursion on the way down the tree to the insertion pointFixing the tree occurs as the recursion unwindsTop-Down Insertion StrategyIn T-Down insertion, the corrections are done while traversing down the tree to the insertion point.When the actual insertion is done, no further corrections are needed, so no need to traverse back up the tree.So, T-Down insertion can be done iteratively which is generally faster- So, walking down the tree to perform the insert and then walking back up to perform the rotates is double the work.-here (top-down approach), shows how this double work can be avoided for the Red-Black tree as follows.
25Goal of T-D InsertionInsertion is always done as a leaf (as in ordinary BST insertion)Recall from the B-Up flow chart that if the uncle of a newly inserted node is black, we restore the RB tree properties by one or two local rotations and recoloring we do not need to make changes further up the tree
Goal (2)Therefore, the goal of T-D insertion is to traverse from the root to the insertion point in such a way that RB properties are maintained, and at the insertion point, the uncle is Black.
That way we may have to rotate and recolor, but not propagate back up the treePossible insertion configurationsX (Red or Black)YZIf a new node is inserted as a child of Y or Z, there is no problem since the new nodes parent is blackPossible insertion configurationsXYZIf new node is child of Z, no problem since Z is black.If new node is child of Y, no problem since the new nodes uncle (Z) is black do a few rotations and recolor. done29Possible insertion configurationsXYZIf new node is inserted as child of Y or Z, its uncle will be red and we will have to go back up the tree. This is the only case we need to avoid.Top-Down TraversalXYZAs we traverse down the tree and encounter this case, we recolor and possible do some rotations.There are 3 cases.Remember the goal to create an insertion point at which the parent of the new node is Black, or the uncle of the new node is black.If on the way down we come across a node X with two red children, colour-flip it immediately! But what if X's parent is also red? We break the invariant!
31Case 1 Xs Parent is BlackXZYPXZPJust recolor and continue down the treeYCase 2Xs Parent is Red (so Grandparent is Black) and X and P are both left/right childrenRotate P around GColor P blackColor G redNote that Xs uncle, U, must be black because it (a) was initially black, or (b) would have been made black when we encountered G (which would have had two red children -- Xs Parent and Xs uncle)Case 2 diagramsXZYPGUSXZYPGUSRotate P around G. Recolor X, Y, Z, P and GCase 3Xs Parent is Red (so Grandparent is Black) and X and P are opposite childrenRotate P around GColor P blackColor G redAgain note that Xs uncle, U, must be black because it (a) was initially black, or (b) would have been made black when we encountered G (which would have had two red children -- Xs Parent and Xs uncle)Case 3 Diagrams (1 of 2)XZYPGUSXYSPGUZStep 1 recolor X, Y and Z. Rotate X around P.Case 3 Diagrams (2 of 2)XYSPGUZPYSXGUZStep 2 Rotate X around G. Recolor X and GTop-Down Insert SummaryPXYZCase 1P is BlackJust RecolorPXYZCase 2P is RedX & P both left/rightPXYZGPXYZGRecolorX,Y,ZPXYZGRotate P around GRecolor P,GCase 3P is RedX and P are opposite childrenPXYZGRecolor X,Y,Z Rotate X around PXPYZGRotate X around GRecolor X, GRecolor X,Y,ZXPYZG39
Insert 60 3940
Change the coloers 4041
The result 4142
Then we insert 60 42Another Example43
Insert 50 4344
Color Change 4445
Another Rotate 4849
Color Change 4950
This the result.51Red Black TreesTop-Down DeletionRecall the rules for BST deletionIf a node to be deleted is a leaf, just delete it.If a node to be deleted has just one child, replace it with that childIf a node to be deleted has two children, replace the value of by its in-order predecessors value then delete the in-order predecessor (a recursive step)
What can go wrong?If the delete node is red?
Not a problem no RB properties violated
If the deleted node is black?
If the node is not the root, deleting it will change the black-height along some path
TerminologyMatching Weiss text section 12.2X is the node being examinedT is Xs siblingP is Xs (and Ts) parentR is Ts right childL is Ts left child.Basic StrategyAs we traverse the tree, we change every node we visit, X, to Red.When we change X to Red, we knowP is also Red (we just came from there)T is black (since P is Red, its children are Black)Step 1 Examine the rootIf both of the roots children are BlackMake the root RedMove X to the appropriate child of the rootProceed to step 2Otherwise designate the root as X and proceed to step 2B.Step 2 the main case
As we traverse down the tree, we continually encounter this situation until we reach the node to be deletedX is Black, P is Red, T is Black
We are going to color X Red, then recolor other nodes and possibly do rotation(s) based on the color of Xs and Ts children2A. X has 2 Black children2B. X has at least one Red child
PTXCase 2AX has two Black Children2A1. T has 2 Black Children2A2. Ts left child is Red2A3. Ts right child is Red
** if both of Ts children are Red, we can do either 2A2 or 2A3Case 2A1X and T have 2 Black Children
PTXPTXJust recolor X, P and T and move down the treeCase 2A2PTXLX has 2 Black Children and Ts Left Child is RedRotate L around T, then L around PRecolor X and P then continue down the tree
L1L2PTXLL1L2Case 2A3PTXX has 2 Black Children and Ts Right Child is RedRotate T around PRecolor X, P, T and R then continue down the tree
R1R2PRXTR2R1RLLCase 2BX has at least one Red child Continue down the tree to the next leve