Download presentation
Presentation is loading. Please wait.
2
Red Black Trees CSC 172 SPRING 2004 LECTURE 18
3
Reading for next workshop Weiss 19.5 Learn this stuff On Quiz #4 & final you will be expected to generate BS, AVL, and R-B trees from some sequence of inserts & deletes
4
RED-BLACK TREES Red-Black Trees are BSTs Red-Black Trees maintain balance Red-Black Trees are an alternative to AVL trees Not as “bushy” Easier to implement Single top down pass can be used during insertion & deletion Tend to be faster performance wise JAVA’s TreeSet class uses Red-Black Trees
5
Red Black Tree BST with a coloring convention for each element Every node is either red or black Root is black Nodes are colored according to rules One rule involves paths Specifically, paths from nodes with no children or one child (nodes with nulls)
6
Two Rules Red Rule If an element is red, all of it’s children are black Path Rule The number of black elements must be the same in all paths from the root element to element with no children or with one child
7
Maintaining Balance New items must be colored red If an insertion results in a rule violation, we need to fix the tree Rotation Re-color
8
Example {10,85,15,70,20,60,30,50,65,80,90,40,5,55}
9
Figure 19.35 If S is black, a single rotation between parent and grandparent, with appropriate color changes, restores property 3 if X is an outside grandchild. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley
10
Figure 19.36 If S is black, a double rotation involving X, the parent, and the grandparent, with appropriate color changes, restores property 3 if X is an inside grandchild. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley
11
Figure 19.37 If S is red, a single rotation between parent and grandparent, with appropriate color changes, restores property 3 between X and P. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley
12
Figure 19.38 Color flip: Only if X’s parent is red do we continue with a rotation. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley
13
Figure 19.39 A color flip at 50 induces a violation; because the violation is outside, a single rotation fixes it. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley
14
Figure 19.40 Result of single rotation that fixes the violation at node 50 Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley
15
Figure 19.41 Insertion of 45 as a red node Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley
16
Figure 19.50 X has two black children, and both of its sibling’s children are black; do a color flip. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley
17
Figure 19.51 X has two black children, and the outer child of its sibling is red; do a single rotation. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley
18
Figure 19.52 X has two black children, and the inner child of its sibling is red; do a double rotation. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley
19
Figure 19.53 X is black, and at least one child is red; if we fall through to the next level and land on a red child, fine; if not, we rotate a sibling and parent. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley
20
Height of a Red-Black Tree Claim: Let y be the root of a subtree of a red-black tree. The number of black elements is the same in a path from y to any one of its descendants with no child or one child.
21
General case x y z1z1 z2z2 b1b1 b2b2 b0b0 In general, b 0 +b 1 =b 0 +b 2 So, b 1 =b 2 We define blackHeight(z) = bh(z) = The number of black elements in any path from z to any descendant with 0 or 1 child
22
Height of a red-black tree For any nonempty subtree of a red-black tree
23
Basis height(t) = 0 n(t) = 0 if the root is red n(t) = 1 if the root is black Either way 1 >= 2 bh(root(t)) - 1
24
Induction case 1 Let k be any nonnegative integer BTIH n(t) >= 2 bh(root(t)) - 1 for height(t) <= k If the root of t has one child, we must have bh(root) = 1
25
Induction case 2 The root of t has two children, (v 1,v 2 ) If the root is red bh(root) = bh(v 1 ) = bh(v 2 ) If the root is black bh(root) = bh(v 1 ) + 1 = bh(v 2 ) + 1 Either way bh(v1) >= bh(root(t)) – 1 bh(v2) >= bh(root(t)) – 1
26
Induction case 2 BTIH: n(leftTree(t)) >= 2 bh(v1) – 1 n(rightTree(t)) >= 2 bh(v2) – 1 The number of elements in t is one more than the number of elements in leftTree(t) + rightTree(t)
27
Ergo
28
Finally For any red-black tree with n elements height(t) is O(log n) By the red rule, at most half of the elements in a path from the root to the farthest leaf can be red. So, at least half those elements must be black bh(root(t)) >= height(t)/2 n(t) >= 2 bh(root(t)) – 1 n(t) >= 2 height(t)/2 – 1 So, height(t) <= 2 log 2 (n(t)+1)
29
AVL vs RB AVL : height < 1.75 log 2 (n) Red-Black height <= 2 log 2 (n+1) So, AVLs are “bushier” that red-black. However, maintaining a red-black is simpler Which is why Java’s TreeSet class uses red-black
30
Helpers private static final boolean RED = false; private static final boolean BLACK= true; private static boolean colorOf(Entry p) { return (p==null?BLACK:p.color); } private static void setColor(Entry p,boolean c) { if(p!=null) p.color – c; } private static Entry parentOf(Entry p) { return (p==null?null:p.parent); }
31
Insertions on red-black trees Suppose we insert Entry x We set x’s color to red Do we need to recolor & rotate? If x is root, “no” If x’s parent is BLACK, “no” So, loop while(x!= root && x.parent.color == RED)
32
Insertions on red-black trees FixAfterInsertion 1. Let t be (ref) the parent of the new insertion 2. Create a new Entry object pointer to by t.left or t.right 3. Set new Entry’s fields 4. Recolor and restructure 5. Set the root to BLACK
33
Aunt’s & Uncles Because of rotation we need to consider the color of the sibling of x’s parent. When x is parent’s left child: y = x.parent.parent.right; // could be null
34
Case 1:colorOf(y) == RED 45 4050 41 45 4050 41 x y setColor(parentOf(x),BLACK); setColor(y,BLACK); setColor(parentOf(parentOf(x)),RED) x=parentOf(x); //keep looping x
35
Case 2:colorOf(y) = BLACK && x is RC 45 40 41 x=parentOf(x); rotateLeft(x); 45 41 40 x x Y is null
36
Case 3:colorOf(y) = BLACK && x is LC setColor(parentOf(x),BLACK); setColor(parentOf(parentOf(x)),RED); if(parentOf(parentOf(x) )!= null) rotateRight(parentOf(parentOf(x)); 45 41 40 x Y is null 45 41 40 x
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.