红黑树
1. Properties of red-black trees
1.1 difinition
1.2 black-height
The black-height of any node x, denoted by bh(x), is the number of black nodes on any simple path from x ( x not included ) down to a leaf. bh(Tree) = bh(root).
1.3 Lemma
A red-black tree with n internal nodes has height at most 2lg( n + 1 ).
proof :
s
i
z
e
o
f
(
x
)
>
=
2
b
h
(
x
)
−
1
sizeof(x) >= 2^{bh(x)}-1
sizeof(x)>=2bh(x)−1
tips :sizeof(x) means the subtree rooted at any node x contains at least internal nodes. prove by induction
1.
h
(
x
)
=
0
,
s
i
z
e
o
f
(
x
)
=
2
0
−
1
=
1
−
1
=
0
1.h(x) = 0, sizeof(x) = 2^0 - 1 = 1 - 1 = 0
1.h(x)=0,sizeof(x)=20−1=1−1=0
2. h ( x ) > 0 2.h(x) > 0 2.h(x)>0
For the inductive step, consider a node x that has positive height and is an internal node with two children. Each child has a black-height of either bh(x) or bh(x) - 1, depending on whether its color is red or black, respectively.
there are three situations:
- red - black : bh(chlid) = bh(x) - 1
- balck - red : bh(child) = bh(x)
- black - black: bh(child) = bh(x) - 1
3. s i z e o f ( c h l i d ) = 2 b h ( x ) − 1 − 1 3. sizeof(chlid) = 2^{bh(x) - 1} - 1 3.sizeof(chlid)=2bh(x)−1−1
4. s i z e o f ( x ) > = 2 ∗ s i z e o f ( c h i l d ) + 1 = 2 b h ( x ) − 1 4.sizeof(x) >= 2*sizeof(child) + 1 = 2^{bh(x)} - 1 4.sizeof(x)>=2∗sizeof(child)+1=2bh(x)−1
b h ( T r e e ) > = h ( T r e e ) / 2 bh(Tree) >= h(Tree) / 2 bh(Tree)>=h(Tree)/2
s i z e o f ( T ) = n = 2 b h ( T ) − 1 > = 2 h ( T ) / 2 − 1 sizeof(T) = n = 2^{bh(T)}-1 >= 2^{h(T)/2} - 1 sizeof(T)=n=2bh(T)−1>=2h(T)/2−1
h < = 2 l g ( n + 1 ) h<= 2lg(n + 1) h<=2lg(n+1)
1.4 Time Analysis
The time complexity of Red - Black tree is O(lgn) because the height is O(lgn) and each can run in O(h) time on a binary search of height h.
2.Rotations
Code
Left Rotate(T,x)
{
y = x.right; // set y
x.right = y.left; // turn y’s left subtree into x’s right subtree
if(y.left != T.NULL) y.left.p = x;
y.p = x.p // link x’s parent to y
if(x.p == T.NULL) T.root = y;
else if(x == x.p.left) x.p.left = y;
else x.p.right = y;
y.left = x; // put x on y’s left
x.p = y;
}
3. Insertion
Code
3.1 case 1——z’s uncle y is red
Because z.p.p is black, we can color both z.p and y black, thereby fixing the problem of z and z.p both being red, and we can color z.p.p red, thereby maintaining property 5. We then repeat the while loop with z.p.p as the new node z. The pointer z moves up two levels in the tree.
3.2 Case 2: z’s uncle y is black and z is a right child
3.3 Case 3: z’s uncle y is black and z is a left child
- Case 2 makes z point to z.p, which is red. No further change to z or its color occurs in cases 2 and 3.
- Case 3 makes z.p black, so that if z.p is the root at the start of the next iteration, it is black.
- As in case 1, properties 1, 3, and 5 are maintained in cases 2 and 3. Since node z is not the root in cases 2 and 3, we know that there is no violation of property 2. Cases 2 and 3 do not introduce a violation of property 2, since the only node that is made red becomes a child of a black node by the rotation in case 3. Cases 2 and 3 correct the lone violation of property 4, and they do not introduce another violation.
3.4 Aanlysis
Insert——O(lgn)
RB-INSERT-FIXUP——the while loop repeats only if case 1 occurs, and then the pointer z moves two levels up the tree. The total number of times the while loop can be executed is therefore O(lg n). Thus, RB-INSERT takes a total of O(lg n) time. Moreover, it never performs more than two rotations, since the while loop terminates if case 2 or case 3is executed.
4.Deletion
Code
4.1 Case 1 : x’s sibling w is red
Case 1 (lines 5–8 of RB-DELETE-FIXUP ) occurs when node w, the sibling of node x, is red. Since w must have black children, we can switch the colors of w and x.p and then perform a left-rotation on x.p without violating any of the red-black properties. The new sibling of x, which is one of w’s children prior to the rotation, is now black, and thus we have converted case 1 into case 2, 3, or 4. Cases 2, 3, and 4 occur when node w is black; they are distinguished by the colors of w’s children.
4.2 Case 2 : x’s sibling w is black, and both of w’s children are black
In case 2 (lines 10–11 of RB-DELETE-FIXUP), both of w’s children are black. Since w is also black, we take one black off both x and w, leaving x with only one black and leaving w red. To compensate for removing one black from x and w, we would like to add an extra black to x.p, which was originally either red or black. We do so by repeating the while loop with x.p as the new node x. Observe that if we enter case 2 through case 1, the new node x is red-and-black, since the original x.p was red. Hence, the value c of the color attribute of the new node x is RED, and the loop terminates when it tests the loop condition. We then color the new node x (singly) black in line 23.
4.3 Case 3 : x’s sibling w is black, w’s left child is red, and w’s right child is black
Case 3 (lines 13–16) occurs when w is black, its left child is red, and its right child is black. We can switch the colors of w and its left child w.left and then perform a right rotation on w without violating any of the red-black properties. The new sibling w of x is now a black node with a red right child, and thus we have transformed case 3 into case 4.
4.4 Case 4 : x’s sibling w is black, and w’s right child is red
Case 4 (lines 17–21) occurs when node x’s sibling w is black and w’s right child is red. By making some color changes and performing a left rotation on x.p, we can remove the extra black on x, making it singly black, without violating any of the red-black properties. Setting x to be the root causes the while loop to terminate when it tests the loop condition.
4.5 Analysis
Since the height of a red-black tree of n nodes is O(lg n), the total cost of the procedure without the call to RB-DELETE-FIXUP takes O(lg n) time. Within RB-DELETE-FIXUP, each of cases 1, 3, and 4 lead to termination after performing a constant number of color changes and at most three rotations. Case 2 is the only case in which the while loop can be repeated, and then the pointer x moves up the tree at most O(lg n) times, performing no rotations. Thus, the procedure RB-DELETE-FIXUP takes O(lg n) time and performs at most three rotations, and the overall time for RB-DELETE is therefore also O(lg n).