380 likes | 828 Views
Data Structure: Chapter 8. Min Chen School of Computer Science and Engineering Seoul National University. AVL Trees & Red-Black Trees. Content. Motivation of Red-Black Trees Balanced and Unbalanced Trees AVL Trees Definition of Red-Black Trees Operations for Red-Black Trees Search
E N D
Data Structure: Chapter 8 Min Chen School of Computer Science and Engineering Seoul National University AVL Trees & Red-Black Trees
Content • Motivation of Red-Black Trees • Balanced and Unbalanced Trees • AVL Trees • Definition of Red-Black Trees • Operations for Red-Black Trees • Search • Insertion
Motivation of Red-Black Tree (1) • Balanced and Unbalanced Trees 20 10 30 15 5 35 25
Motivation of Red-Black Tree (2) Balance Factor: 0-1=-1 • AVL-Tree • 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 Unbalanced 60 Balance Factor: 2-0=2 40 50 30 45
Motivation of Red-Black Tree (2) • AVL-Tree: Rotation Operation
Motivation of Red-Black Tree (2) • AVL-Tree: Rotation Operation 60 Left Right Case 60 50 50 40 40 40 60 50 30 30 45 30 45 45
Definition of Red-Black Trees • Definition: a binary tree, satisfying: • Every node is colored either red or black • The root is black • If a node is red, its children must be black • consecutive red nodes are disallowed • Every path from a node to a null reference must contain the same number of black nodes
Example of Red-Black Trees • The insertion sequence is • 10, 85, 15, 70, 20, 60, 30, 50, 65, 80, 90, 40, 5, 55 30 15 70 10 20 60 85 5 50 65 80 90 40 55
Red-Black Trees: Properties • 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
Red-Black Trees: Properties • B = total black nodes from root to leaf • N = total all nodes • H = height All operations guaranteed logarithmic!
Insertion in Red-Black Trees • 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…
Case after insertion: Consecutive red (P & X) Sibling of parent (S) is black X is “outer node” (left-left or right-right) X: new node P: parent S: sibling G: Grandparent P G X P S G X C C S D E A B D E A B Single Rotation
X: new node P: parent S: sibling G: Grandparent X G P P S G X A C S D E A B D E B C Double Rotation • Case after insertion: • Consecutive red (P & X) • Sibling of parent (S) is black • X is “inner node” (left-right or left)
P G X P S G X C C S D E A B D E A B Single Rotation (bottom-up) • Case after insertion: • Consecutive red • Sibling of parent is red • Outer node (left-left or right-right) But what if P’s parent is red? We have to keep going up the tree all the way to the root
X X C1 C2 C1 C2 Top-Down Insertion • 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 X C1 C2 C1 C2 Color Flip • 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)
G G P S P S X C X D E C D E A B A B Color Flip (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!
18 Example: Insert 18 30 15 70 10 20 60 85 5 50 65 80 90 40 55
18 2 Example: Insert 2 30 15 70 10 20 60 85 5 50 65 80 90 40 55
18 Example: Insert 2 30 15 70 5 20 60 85 10 2 50 65 80 90 40 55
18 45 Example: Insert 45 (Illustration) 30 15 70 5 20 60 85 10 2 50 65 80 90 40 55
18 Color-flip! 45 Example: Insert 45 (Top-Down Color Flip) 30 15 70 5 20 60 85 10 2 50 65 80 90 40 55
18 Color-flip! 45 Example: Insert 45 (Top-Down Color Flip) 30 15 70 5 20 60 85 10 2 50 65 80 90 40 55
18 45 Example: Insert 45 (Single Rotate) 30 15 70 5 20 60 85 10 2 50 65 80 90 40 55
18 45 Example: Insert 45 (Single Rotate) 30 15 60 50 5 20 70 85 10 2 65 40 55 80 90
Red-Black Tree Insertion: Exercise • The insertion sequence is 10, 85, 15, 70, 20, 60, 30, 50, 65, 80, 90, 40, 5, 55
Red-Black Tree: Deletion • 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:
P X S B A C D Top-Down Deletion • 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): X: visited node P: parent S: sibling Idea: make sure that X is red!
P X S B A C D Possible cases • P is red (inductive invariant) • X and S are black (result of property #3) • 2 cases: • 1. Both X’s children (A & B) are black • 2. X has at least one red child (A, B, or both)
P P X X S S B B A A D D C C Case 1: Both X’s children are black • Depends on children of S (C & D): • Both C & D are black: simply colour-flip:
P S X S P D X B C A C D B A Case 1: Both X’s children are black • Outer child of S (C) is Red: do a single rotation and recolour
C P S P X X S D B A B A D C Case 1: Both X’s children are black • Inner child of S (C) is Red: do a double rotation and recolour
Case 2: One/Both of X’s children is red • 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 D X S X C B A D C B A
Summary • 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)