Data Structure: Chapter 8 Min Chen School of Computer Science and Engineering Seoul National University Motivation of Red-Black Trees Balanced and Unbalanced Trees AVL Trees Definition of Red-Black Trees Operations for Red-Black Trees Search Insertion Balanced and Unbalanced Trees 20 10 5 30 15 25 35 AVL-Tree Balance Factor: 0-1=-1 Unbalanced The balance factor of a node is the height of its left subtree minus the height of its right subtree A node with balance factor 1, 0, or -1 is considered balanced 60 Balance Factor: 2-0=2 40 50 30 45 AVL-Tree: Rotation Operation AVL-Tree: Rotation Operation 60 Left Right Case 50 40 40 60 60 50 45 45 30 30 30 45 Definition: a binary tree, satisfying: 1. Every node is colored either red or black 2. The root is black 3. If a node is red, its children must be black ▪ consecutive red nodes are disallowed 4. Every path from a node to a null reference must contain the same number of black nodes The insertion sequence is 10, 85, 15, 70, 20, 60, 30, 50, 65, 80, 90, 40, 5, 55 30 15 10 70 20 60 5 50 40 85 65 55 80 90 • Each path must contain the same number of black nodes. (Rule #4) • Consecutive red nodes are not allowed. (Rule #3) • The longest path is at most twice the length of the shortest path 2 B 1 N 22 B 1 2 B N 1 22 B log 2 B B log 2 2 B 2 B B log( N 1) log( N 1) 2 B 1 log( N 1) B log( N 1) 2 log( N 1) H 2 log( N 1) B = total black nodes from root to leaf N = total all nodes H = height All operations guaranteed logarithmic! A new node must be colored red Why? ▪ A new item is always inserted as a leaf in the tree ▪ If we color a new item black, then the number of black nodes from root would be different (violate property #4) If the parent is black, no problem. If the parent is red, we create two consecutive red nodes (violate property #3) ▪ Thus, we have to do some rotating/recolouring… X: new node P: parent S: sibling G: Grandparent Case after insertion: Consecutive red (P & X) Sibling of parent (S) is black X is “outer node” (left-left or right-right) P G P X A C B X S D E A G B C S D E X: new node P: parent S: sibling G: Grandparent Case after insertion: Consecutive red (P & X) Sibling of parent (S) is black X is “inner node” (left-right or left) X G P X A B P S D C E A G B C S D E Case after insertion: Consecutive red Sibling of parent is red Outer node (left-left or right-right) P G P X A C B X S D E A G B C S D But what if P’s parent is red? We have to keep going up the tree all the way to the root E The solution: prevent S from ever being red! Starting from the root (searching for insertion point) Never allow 2 red siblings If we see a node X with 2 red children, do a colour flip. X C1 X C2 C1 C2 X C1 X C2 C1 C2 Maintains property #4 Possible violation of #3: if X’s parent is red! ▪ Do single or double rotation ▪ X’s parent’s sibling can never be red! Set the root to black (to maintain property #2) If we do the colour flipping on the way down to the insertion point, we will never reach a condition where P & S are red! G G P X A S C B D P E X A S C B D E 30 15 10 5 70 20 60 18 50 40 85 65 55 80 90 30 15 10 5 2 70 20 60 18 50 40 85 65 55 80 90 30 15 5 2 70 20 10 60 18 50 40 85 65 55 80 90 30 15 5 2 70 20 10 60 18 50 40 65 55 45 85 80 90 30 15 5 2 70 20 10 60 18 Color-flip! 50 40 85 65 80 55 45 90 30 15 5 2 70 20 10 60 18 Color-flip! 50 40 85 65 80 55 45 90 30 15 5 2 70 20 10 60 18 50 40 85 65 80 55 45 90 30 15 5 2 60 50 20 10 18 40 70 55 85 65 80 45 90 The insertion sequence is 10, 85, 15, 70, 20, 60, 30, 50, 65, 80, 90, 40, 5, 55 Deletion in BST: only leaf nodes or nodes with one child are really deleted (Why?) If the deleted node is red: no problem (all properties maintained). Leaf nodes: Single child nodes: If node to be deleted is black violate property #4 Always ensure that the node to be deleted is red. Top-down traversal from root (looking for node to be deleted): P X A X: visited node P: parent S: sibling S B C D Idea: make sure that X is red! P is red (inductive invariant) X and S are black (result of property #3) P X A S B C D 2 cases: 1. Both X’s children (A & B) are black 2. X has at least one red child (A, B, or both) Depends on children of S (C & D): Both C & D are black: simply colour-flip: P P X A S B C X D A S B C D Case 1: Both X’s children are black Outer child of S (C) is Red: do a single rotation and recolour S P P X A S B C C X D A D B Inner child of S (C) is Red: do a double rotation and recolour C P X A P S B C X D A S D B Recurse down to X’s child If we land on a red node, fine. If we land on a black node, rotate sibling and parent: S P P X A S B C C X D A D B Red-Black trees use color as balancing information instead of height in AVL trees. An insertion may cause a local perturbation (two consecutive red nodes) The pertubation is either resolved locally (rotations), or propagated to a higher level in the tree by recoloring (color flip) O(1) for a rotation or color flip At most one restructuring per insertion. O(log n) color flips Total time: O(log n)