480 likes | 680 Views
Red-Black Trees. Black-Height of the tree = 4. Red-Black Trees. Definition: A red-black tree is a binary search tree where: Every node is either red or black. Each NULL pointer is considered to be a black node If a node is red, then both of its children are black.
E N D
Red-Black Trees • Definition: A red-black tree is a binary search tree where: • Every node is either red or black. • Each NULL pointer is considered to be a black node • If a node is red, then both of its children are black. • Every path from a node to a leaf contains the same number of black nodes. • Definition: The black-height of a node, n, in a red-black tree is the number of black nodes on any path to a leaf, not counting nulls.
A valid Red-Black Tree Black-Height = 2
Theorem 1 – Any red-black tree with root x, has at least n = 2bh(x) – 1 internal nodes, where bh(x) is the black height of node x. Proof: by induction on height of x.
Theorem 2 – In a red-black tree, at least half the nodes on any path from the root to a leaf must be black. Proof – If there is a red node on the path, there must be a corresponding black node.
Theorem 3 – In a red-black tree, no path from any node, N, to a leaf is more than twice as long as any other path from N to any other leaf. Proof: By definition, every path from a node to any leaf contains the same number of black nodes. By Theorem 2, a least ½ the nodes on any such path are black. Therefore, there can no more than twice as many nodes on any path from N to a leaf as on any other path. Therefore the length of every path is no more than twice as long as any other path
Theorem 4 – A red-black tree with n internal nodes has height h <= 2 lg(n + 1). Proof: Let h be the height of the red-black tree with root x. By Theorem 2, bh(x) >= h/2 From Theorem 1, n >= 2bh(x) - 1 Therefore n >= 2 h/2 – 1 n + 1 >= 2h/2 lg(n + 1) >= h/2 2lg(n + 1) >= h
Bottom –Up Insertion • Insert node as usual in BST • Color the Node RED • What Red-Black property may be violated? • Every node is Red or Black • Leaf nodes are Black NULLS • If node is Red, both children must be Black • Every path from node to descendant leaf must contain the same number of Blacks
Bottom Up Insertion • Insert node; Color it RED; X is pointer to it • Cases 0: X is the root -- color it black 1: Both parent and uncle are red -- color parent and uncle black, color grandparent red, point X to grandparent, check new situation (recoloring may have created a problem) 2 (zig-zag): Parent is red, but uncle is black. X and its parent are opposite type children -- color grandparent red, color X black, rotate left on parent, rotate right on grandparent 3 (zig-zig): Parent is red, but uncle is black. X and its parent are both left or both right children -- color parent black, color grandparent red, rotate right on grandparent
G X P U G X P U Case 1 – U is Red Just Recolor see if there is a new problem at G
Note, that if both kids of G were red, we would have recolored. If both kids were black, there would be no problem. G P U X X S Case 2 – Zig-Zag (X and P are opposite children) Double Rotate X around P; X around G Recolor G and X as can’t rotate a black from a common path without compensating P G S U
G P U S P X X G Case 3 – Zig-Zig (X and P are both left kids) Single Rotate P around G Recolor P and G as we’ve rotated a black from common path U S
11 Insert 4 into this R-B Tree 14 2 15 1 7 5 8 Red node Black node
Insertion Practice Insert the values 2, 1, 4, 5, 9, 3, 6, 7 into an initially empty Red-Black Tree
Asymptotic Cost of Insertion • O(lg n) to descend to insertion point • O(1) to do insertion • O(lg n) to ascend and readjust == worst case only for case 1 • Total: O(log n)
Red-Black Trees Bottom-Up Deletion
Recall “ordinary” BST Delete • If vertex to be deleted is a leaf, just delete it. • If vertex to be deleted has just one child, replace it with that child • If vertex to be deleted has two children, replace the value of by it’s in-order predecessor’s value then delete the in-order predecessor (a recursive step)
Bottom-Up Deletion • Do ordinary BST deletion. Eventually a “case 1” or “case 2“ will be done (leaf or just one child). If deleted node, D, is a leaf, think of deletion as replacing with the NULL pointer, V. If D had one child, V, think of deletion as replacing D with V. • What can go wrong??
Which RB Property may be violated after deletion? • If D is red?Not a problem – no RB properties violated • If D is black?If D is not the root, deleting it will change the black-height along some path
Fixing the problem • Think of V as having an “extra” unit of blackness. This extra blackness must be absorbed into the tree (by a red node), or propagated up to the root and out of the tree. • There are four cases – our examples and “rules” assume that V is a left child. There are symmetric cases for V as a right child
Terminology • The node just deleted was D • The node that replaces it is V, which has an extra unit of blackness (it was black and if deleted, will create an imbalance) • The parent of V is P • The sibling of V is S Black Node Red or Black and don’t care Red Node
Bottom-Up DeletionCase 1 • V’s sibling, S, is Red • Rotate S around P and recolor S & P • NOT a terminal case – One of the other cases will now apply • All other cases apply when S is Black
Case 1 Diagram S P Rotate P S V+ V+ S Recolor P V+
Bottom-Up DeletionCase 2 • V’s sibling, S, is black and has two black children. • Recolor S to be Red (subtract 1 black from each) • P absorbs V’s extra blackness • If P is Red, we’re done • If P is Black, it now has extra blackness and problem has been propagated up the tree
Case 2 diagram Recolor and absorb P+ P S S V V+ Either extra black absorbed by P or P now has extra blackness
Bottom-Up DeletionCase 3 • S is black • S’s RIGHT child is RED (Left child either color) • Rotate S around P • Swap colors of S and P, and color S’s Right child Black • This is the terminal case – we’re done
Case 3 diagrams S P Rotate P S V+ V S P Recolor V
Bottom-Up DeletionCase 4 • S is Black, S’s right child is Black and S’s left child is Red • Rotate S’s left child around S • Swap color of S and S’s left child • Now in case 3
Case 4 Diagrams P P S V+ V+ P S Rotate V+ S Recolor
65 50 80 10 70 90 60 62 Perform the following deletions, in the order specified Delete 90, Delete 80, Delete 70
Red Black Trees Top-Down Insertion
Review of Bottom-Up Insertion • In Bottom-Up insertion, “ordinary” BST insertion was used, followed by correction of the tree on the way back up to the root • This is most easily done recursively • Insert winds up the recursion on the way down the tree to the insertion point • Fixing the tree occurs as the recursion unwinds
Top-Down Insertion Strategy • In Top-Down insertion, the corrections are done while traversing down the tree to the insertion point. • When the actual insertion is done, no further corrections are needed, so no need to traverse back up the tree. • So, Top-Down insertion can be done iteratively which is generally faster
Goal of T-D Insertion • Insertion is always done as a leaf (as in ordinary BST insertion) • Recall from the Bottom-Up discussion that if the uncle of a newly inserted node is black, we restore the RB tree properties by one or two local rotations and recoloring – we do not need to make changes further up the tree
Goal (2) • Therefore, the goal of top down insertion is to traverse from the root to the insertion point in such a way that RB properties are maintained, and at the insertion point, the uncle is Black. • That way we may have to rotate and recolor, but not propagate back up the tree
Possible insertion configurations X (Red or Black) Y Z If a new node is inserted as a child of Y or Z, there is no problem since the new node’s parent is black
Possible insertion configurations X Y Z If new node is child of Z, no problem since Z is black. If new node is child of Y, no problem since the new node’s uncle (Z) is black – do a few rotations and recolor…. done
Possible insertion configurations X Y Z If new node is inserted as child of Y or Z, it’s uncle will be red and we will have to go back up the tree. This is the only case we need to avoid.
Top-Down Traversal As we traverse down the tree and encounter this case, we recolor and possible do some rotations. There are 3 cases. X Z Y Remember the goal – to create an insertion point at which the parent of the new node is Black, or the uncle of the new node is black.
Case 1 – X’s Parent is Black P P X X Z Y Y Z Just recolor and continue down the tree
Case 2 • X’s Parent is Red (so Grandparent is Black) and X and P are both left/right children • Rotate P around G • Color P black • Color G red • Note that X’s uncle, U, must be black because it (a) was initially black, or (b) would have been made black when we encountered G (which would have had two red children -- X’s Parent and X’s uncle)
Case 2 diagrams G P P U G X X S Z Z S U Y Y Rotate P around G. Recolor X, Y, Z, P and G
Case 3 • X’s Parent is Red (so Grandparent is Black) and X and P are opposite children • Rotate P around G • Color P black • Color G red • Again note that X’s uncle, U, must be black because it (a) was initially black, or (b) would have been made black when we encountered G (which would have had two red children -- X’s Parent and X’s uncle)
Case 3 Diagrams (1 of 2) G G X U P U P Z S X Y S Y Z Step 1 – recolor X, Y and Z. Rotate X around P.
Case 3 Diagrams (2 of 2) X G X U G P P Z Z U S Y Y S Step 2 – Rotate X around G. Recolor X and G
An exercise – insert F D T W L P Z J V E K
Top-Down Insert Summary Case 1 P is Black Just Recolor Recolor X,Y,Z P P X X Y Z Y Z Case 2P is RedX & P both left children (or both right children) Rotate P around GRecolor P,G P G G RecolorX,Y,Z G X P P Y Z X X Y Y Z Z G G X Case 3P is RedX and P are opposite children Recolor X,Y,Z Rotate X around P G Rotate X around GRecolor X, G X P P P X Y Z Y Z Y Z