Presentation is loading. Please wait.

Presentation is loading. Please wait.

Red-Black Trees Motivations

Similar presentations


Presentation on theme: "Red-Black Trees Motivations"— Presentation transcript:

1 Red-Black Trees Motivations
worst case search in a binary search tree is O(n) if the tree is degenerate (that is, linear) Formally specify the characteristics of a degenerate tree if the data is entered in sorted order, this case will occur; even partially sorted order is bad we want to find a way to keep the tree approximately balanced no matter what the order of insertion is an approach called AVL trees that used local rotations was proposed in 1960 red-black trees also use local rotations but have slightly better performance (fewer rotations) than AVL trees

2 Red-Black Tree Properties
Every node is either red or black; all nil leaves are black If a node is red, then both children are black Every simple path from a node to any descendant leaf contains the same number of black nodes; we call this number bh(x), the black height from node x

3 Red-Black Tree Performance
a red-black tree with n internal nodes has height at most 2 lg(n+1), proof is given on page 274 the operations search, minimum, maximum, successor and predecessor can all be implemented in O(lg n) time we will show that the insert and delete operations that preserve the red-black tree property can also be executed in O(lg n) time

4 Rotations There are two rotations: left and right; they are local operations on a node that preserve the tree ordering In either configuration the ordering is: a x b y g Each node contains a color (1 bit), key, left, right, and p (parent) Rotations only change the pointer values

5 An Example of Left-Rotate
What happened to the height of this tree? Both rotations can be accomplished in O(1) time

6 The Insertion Operation
Initially the node will be inserted as in a binary search tree and colored red Rotations and recoloring will restore the red-black tree property The initial insertion can only violate the property requiring a red node having two black children a while loop will move the point of attention up the tree restoring this property while maintaining the black height property as invariant at some point, rotations will be performed and the loop will be exited

7 The Big Picture x and p[x] are red
x’s uncle y is also red, so p[x] and y are colored black and x moves up the tree x and p[x] are red, x is a right child, so a left rotation makes it a left child with p[x] red a right rotation and color changes completes the process

8 Code Overview The loop continues while x and p[x] are both red
The only case shown is when p[x] is a left child, the other cases are symetric Case 1 is applied as long as x’s uncle y is red In cases 2/3, x’s uncle is black; case 2 makes x a left child through rotation; case 3 uses recoloring and a right rotation to restore the red-black property

9 Case 1 Details x can either be a left or right child of a red parent
assuming uncle y is red, p[x] and y are colored black, and p[p[x]] is colored red; property 4 is preserved and p[p[x]] becomes the new x it is assumed the root is black (see line 18 in the code) so that p[p[x]] is known to exist

10 Cases 2/3 Details if x is a right child, case 2 makes it a left child
each of the subtrees, a, b, g, d, has a black root and the same black-height case 3 causes a right rotation and color changes, all preserving property 4 since property 3 is no longer violated, the while loop terminates

11 Complexity of Insertions
Case 1 can be applied at most O(lg n) times since the root, which is black, will eventually be reached if cases 2 and 3 never apply If case 1 does not apply, then there is either only one rotation (case 3 only) or two rotations (case 2 followed by case 3) before property 3 is restored and the loop is exited Thus the running time is no more than O(lg n) and at most there are two rotations; often times the tree requires no adjustments at all

12 The Deletion Operation
Deletion is only slightly more complex than insertion; it has complexity of O(lg n) The algorithm is easier to code if the empty tree is represented by an object nil[T] that has the same data fields as an ordinary node; its color is black Deletion is similar to deletion from a BST but we must call a separate routine called fixup to restore the four red-black properties

13 Deletion from a Binary Search Tree
Recall the cases that must be considered when deleting a node from a binary search tree; in the case of two children assume the successor node is involved

14 RB-Delete The code is similar to the delete operation for a BST
References to nil are replaced with the sentinnel nil[T] This allows the assignment in line 7 to be unconditional since the nil is like other nodes If the node y that is removed is black, the fixup routine restores the black height of the tree

15 Fixup - 1 Parameter x was the removed node’s sole child (may be nil) which has a double black count The loop moves x with its double count up the tree until: x points to a red node which is colored black x points to the root and the extra black count is discarded rotations and recoloring can solve the problem

16 Fixup - 2 line 2 determines x is a left child; symmetric code holds if it is a right child w points to x’s sibling which cannot be nil each of the four transformations preserves the number of black nodes (including the count of 2 for x) from the root to each subtree case 1 occurs when w is red; after the transformation it is black and case 2/3/4 holds case 2/3/4 occurs when w is black, cases are distinguished by the color of w’s children

17 Fixup - Cases 1 and 2 case 1: w is red and has black children; switch colors of w and p[x] then perform left rotation; now perform one of the other cases as appropriate case 2: w and two children are black, remove black from x (only has one black now) and w (becomes red); add the extra black to p[x] and repeat loop if we entered case 2 via case 1, the loop will terminate

18 Fixup - Cases 3 and 4 case 3: w is black, left child is red, right child is black; switch colors of w and left child then perform a right rotation on w; we have converted case 3 into case 4 case 4: w is black, right child is red; make color changes indicated and left rotate on p[x] set x’s new value to be the root to force loop termination

19 Complexity of Deletions
The cost of RB-Delete without fixup is at most O(lg n) since the tree is at most lg n high RB-Fixup cases 1, 3, 4 do not repeat the loop, so the amount of work is constant with at most three rotations case 2 moves x up to the root and can occur at most lg n times and with no rotations Fixup at most has lg n time and 3 rotations Therefore the overall complexity of RB-Delete with fixup is still O(lg n)


Download ppt "Red-Black Trees Motivations"

Similar presentations


Ads by Google