Introduction to Algorithms Red-Black Trees My T. Thai @ UF Red-Black Trees A binary search tree, each node is colored red or back Red-black properties: 1. Every node is either red or black 2. The root is black 3. Every leaf (NIL) is black 4. If a node is red, then both its children are black 5. All simple paths from any node x to descendant leaves contain the same number of black nodes = black-height (bh(x)) My T. Thai [email protected] 2 Example My T. Thai [email protected] 3 Key property Proof: Claim 1: The subtree rooted at x (internal(x)) contains at least 2bh(x)-1 internal nodes Proof: Use induction and the property: internal(x) = internal(x.left) + internal(x .right) Claim 2: Any node of height h has black-height Proof: use property 4 From Claim 1&2: h(root) My T. Thai [email protected] 2bh(root) 4 Operator Querying operators: Search, Minimum, Maximum, Successor, Predecessor are the same as binary search tree Time: O(log n) Updating operators: need to modify to guarantee red-black properties: Insertion, Deletion: may violates some properties Rotations: tree-reconstructing operation that maintain the properties of red-black tree My T. Thai [email protected] 5 Rotations left and right rotations: inverse of each other Keep the properties of binary search tree Change the tree structure Time: O(1) My T. Thai [email protected] 6 Example My T. Thai [email protected] 7 Left rotation My T. Thai [email protected] 8 Insertion Key problem inserting z: What is the color of inserted node? The properties of red-black tree may be violated Solution: Insert z using the same procedure on binary search tree Fix violated properties: Change the structure of the tree using rotations Recolor nodes My T. Thai [email protected] 9 Insertion Possible violations of properties 1.OK 2.If z is root: easy to fix 3.OK 4.If z.p is red: both z and z.p are red 5.OK Change tree and fix property 4 iteratively My T. Thai [email protected] 10 Fix tree Case 1: z’s uncle y is red Change color of z.p, z.p.p and y then fix z.p.p z.p.p must be black My T. Thai [email protected] 11 Fix tree Case 2: y is black, z is the right child Left rotate at z.p case 3 Case 3: y is black, z is the left child Make z.p black, z.p.p red then rotate on z.p.p My T. Thai [email protected] 12 Fix tree My T. Thai [email protected] 13 Running time of Insertion Inserting new node using insert procedure (on binary search tree) takes O(log n) Fixing time: O(log n) Fix case 1 iteratively takes O(log n) time Fix case 2, 3: constant time Total running time: O(log n) My T. Thai [email protected] 14 Deletion Delete node z using the same procedure on binary search tree y is the removed node Possible violations if y is black: 2.y is root and x is red red root 4.y.p and x are both red 5.Any path containing y has 1 fewer black node My T. Thai [email protected] 15 Fix property 5 Correct by giving x an “extra black” Violate property 1: x points to either doubly black (if x.color = BLACK) or red & black (if x.color = RED) node Solution: x.color = RED turn it into a black node x points to the root remove the extra black x always points to a non-root doubly black node w is x’s sibling, w is not a T.nil 4 cases My T. Thai [email protected] 16 Fix tree Case 1: w is red w has black children sibling of x is black cases 2, 3, 4 My T. Thai [email protected] 17 Case 2: w is black and both of w’s children are black Take 1 black off x (⇒ singly black) and off w (⇒red) Move black to x.p My T. Thai [email protected] 18 Case 3: w is black, w’s left child is red, and w’s right child is black Exchange color of w and w’s left child then rotate Sibling of x is black case 4 My T. Thai [email protected] 19 Case 4: w is black, w’s left child is black, and w’s right child is red Make w be x.p’s color then make x.p and w’s right black Rotate on x.p then remove extra black on x My T. Thai [email protected] 20 RB-DELETE-FIXUP My T. Thai [email protected] 21 Running time of Deletion Delete a node using delete procedure (on binary search tree) takes O(log n) Fixing time: O(log n) Fix case 2 iteratively takes O(log n) time (in each iteration x moves up 1 level) Fix case 1, 3, 4: constant time Total running time: O(log n) My T. Thai [email protected] 22
© Copyright 2026 Paperzz