Red-Black Trees ! Motivation: a binary search tree that is guaranteed to be balanced (operations take time in the ( ) O lg n worst-case). Red-Black Trees ! A red-black tree is a binary search tree where each node has a color attribute: either red or black and the following properties are satisfied: – Every node is either red or black. – Every leaf (empty child) is black. – Both children of a red node are black. – Every simple path from a node to a descendant leaf contains the same number of black nodes. 1 2 The height of Red-Black Trees Rotations ! Lemma: A red-black tree with n internal nodes has right height at most y x 2 lg( n + 1 ) left ! Definition: Black-height, bh(x), is the number of black nodes on any path from x to a leaf (not counting x x y itself). A C ! Proof: The subtree rooted at any node x contains at least internal nodes (by induction on bh ( x ) 2 − 1 C A B B height of x). The black-height of the root is at least h/2, and thus, h / 2 2 − 1 ≤ n ! Note: rotations preserve the inorder key ordering in a h ≤ 2 lg( n + 1 ) binary search tree. 3 4
Red-Black Insert (RB-Insert) RB-Insert: Case 1 ! If x has both a red parent (B) and a red uncle (D), ! Use ordinary binary search tree insertion, and color re-color the parent and the uncle in black, and the the new node red. grandparent (C) in red: ! If any of the red-black properties have been violated, fix the resulting tree using recoloring and rotations. x C C B D B D ! Which of the four properties can possibly be violated? x A A – Each node is still red or black; empty children are still black. – Each path still has the same number of black nodes. – But! What if the parent of the new node is also red? ! Now the grandparent (C) may be in violation… ! If C is the root, we can simply color it black. 5 6 RB-Insert: Case 2 RB-Insert: Case 3 ! If x is the right child of a red parent and has a black ! If x is the left child of a red parent and has a black uncle, perform a left rotation (at A): uncle, perform a right rotation (at C) and re-color: C C C B A D B D B D A C x x A A x B x D ! This brings us into a configuration handled by Case 3 ! After Case 3, there is no longer a violation! 7 8
RB-Insert: Pseudocode Red-Black Remove (RB-Remove) RB-Insert(RBTree T, RBNode x) ! Similar to ordinary binary search tree removal. BST-Insert(T, x); x.color = RED; ! Removing a red node does not violate any of the four while (x != T.root && x.parent.color == RED) { red-black properties. if (CaseOne(x)) { HandleCaseOne(x); ! Removing a black node requires a fix-up, since the x = x.parent.parent; number of black nodes along some paths has } else changed. if (CaseTwo(x)) { ! The fix-up considers four different cases. x = x.parent; LeftRotate(T, x); } HandleCaseThree(x); } T.root.color = BLACK; 9 10 RB-Remove: Case 1 RB-Remove: Case 2 ! Case 1 is transformed into one of the cases 2, 3, or 4 ! Case 2 allows x to move one level up the tree by re- by exchanging the color of the nodes B and D and coloring D to RED: performing a left rotation: x B D B B x w D x w D D A B E A A x w A C C E C E C E 11 12
RB-Remove: Case 3 RB-Remove: Case 4 ! Case 3 is transformed to case 4 by exchanging the ! In case 4, the violation is resolved by changing some colors of nodes C and D and performing a right colors and performing a left rotation (without rotation : violating the red-black properties): B B B D w w w x D x C x D E A A A B A C C E D C E E 13 14
More recommend