Download presentation
Presentation is loading. Please wait.
Published byEmory Flynn Modified over 9 years ago
1
Red Black Trees
2
History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in 1962. They came up with the AVL tree. In 1970, J. E. Hopcroft invented the 2-3 trees (another balancing Search Tree. The Red Black Tree was first invented by Rudolf Bayer (1972), who called them “symmetric binary B-trees”. In 1978, Leo J Guibas and Robert Sedgewick came up with the term, Red Black Binary Tree.
3
Rudolf Bayer He has been a Professor of informatics at the Technical University of Munich since 1972. –Informatics is a science concerned with collection, classification, manipulation, storage, retrieval and dissemination of information. He if famous for inventing 2 sorting structures, B-trees (now called red black trees), with Edward M. McCreight. And UB-tress (with Volker Markl). –UB-Trees is another type of balanced tree used for storing and for efficiently retrieving multidimensional data.
4
4 Red-Black Trees “Balanced” binary trees guarantee an O(lgn) running time on the basic dynamic-set operations Red-black-tree –Binary tree with an additional attribute for its nodes: color which can be red or black –Constrains the way nodes can be colored on any path from the root to a leaf Ensures that no path is more than twice as long as another the tree is balanced –The nodes inherit all the other attributes from the binary-search trees: key, left, right, p
5
Red-Black Trees (RBT) Red-Black tree: BST in which each node is colored red or black. Constraints on the coloring and connection of nodes ensure that no root to leaf path is more than twice as long as any other, so tree is approximately balanced. Each RBT node contains fields left, right, parent, color, and key. LEFTLEFT PARENT RIGHTRIGHT KEY COLOR
6
6 Red-Black-Trees Properties 1.Every node is either red or black 2.The root is black 3.Every leaf ( NIL ) is black 4.If a node is red, then both its children are black No two red nodes in a row on a simple path from the root to a leaf 5.For each node, all paths from the node to descendant leaves contain the same number of black nodes
7
7 Example: RED-BLACK-TREE For convenience we use a sentinel NIL[T] to represent all the NIL nodes at the leafs –NIL[T] has the same fields as an ordinary node –Color[NIL[T]] = BLACK –The other fields may be set to arbitrary values 26 1741 3047 3850 NIL
8
8 Black-Height of a Node Height of a node: the number of edges in a longest path to a leaf Black-height of a node x: bh(x) is the number of black nodes (including NIL) on the path from x to leaf, not counting x 26 1741 3047 3850 NIL h = 4 bh = 2 h = 3 bh = 2 h = 2 bh = 1 h = 1 bh = 1 h = 1 bh = 1 h = 2 bh = 1 h = 1 bh = 1
9
9 Properties of Red-Black-Trees Claim –Any node with height h has black-height ≥ h/2 Proof –By property 4, at most h/2 red nodes on the path from the node to a leaf –Hence at least h/2 are black 26 1741 3047 3850 Property 4: if a node is red then both its children are black
10
10 Properties of Red-Black-Trees Claim –The subtree rooted at any node x contains at least 2 bh(x) - 1 internal nodes Proof: By induction on height of x Basis: height[x] = 0 x is a leaf ( NIL[T] ) bh(x) = 0 # of internal nodes: 2 0 - 1 = 0 NIL x
11
11 Properties of Red-Black-Trees Inductive step: Let height ( x) = h and bh(x) = b Any child y of x has: –bh (y) = 26 1741 3047 3850 b (if the child is red), or b - 1 (if the child is black)
12
12 Properties of Red-Black-Trees Want to prove: –The subtree rooted at any node x contains at least 2 bh(x) - 1 internal nodes Internal nodes for each child of x : 2 bh(x) - 1 - 1 The subtree rooted at x contains at least: (2 bh(x) - 1 – 1) + (2 bh(x) - 1 – 1) + 1 = 2 · (2 bh(x) - 1 - 1) + 1 = 2 bh(x) - 1 internal nodes x l r
13
13 Properties of Red-Black-Trees Lemma: A red-black tree with n internal nodes has height at most 2lg(n + 1). Proof: n Add 1 to both sides and then take logs: n + 1 ≥ 2 b ≥ 2 h/2 lg(n + 1) ≥ h/2 h ≤ 2 lg(n + 1) root l r height(root) = h bh(root) = b number n of internal nodes ≥ 2 b - 1 ≥ 2 h/2 - 1 since b h/2
14
14 Operations on Red-Black-Trees The non-modifying binary-search-tree operations MINIMUM, MAXIMUM, SUCCESSOR, PREDECESSOR, and SEARCH run in O(h) time –They take O(lgn) time on red-black trees What about TREE-INSERT and TREE-DELETE? –They will still run on O(lgn) –We have to guarantee that the modified tree will still be a red-black tree
15
15 INSERT INSERT: what color to make the new node? Red? Let’s insert 35! –Property 4: if a node is red, then both its children are black Black? Let’s insert 14! –Property 5: all paths from a node to its leaves contain the same number of black nodes 26 1741 3047 3850
16
16 DELETE DELETE: what color was the node that was removed? Red? 1.Every node is either red or black 2.The root is black 3.Every leaf ( NIL ) is black 4.If a node is red, then both its children are black 5.For each node, all paths from the node to descendant leaves contain the same number of black nodes OK! OK! Does not create two red nodes in a row OK! Does not change any black heights 26 1741 3047 3850
17
17 DELETE DELETE: what color was the node that was removed? Black? 1.Every node is either red or black 2.The root is black 3.Every leaf ( NIL ) is black 4.If a node is red, then both its children are black 5.For each node, all paths from the node to descendant leaves contain the same number of black nodes OK! Not OK! Could create two red nodes in a row Not OK! Could change the black heights of some nodes 26 1741 3047 3850 Not OK! If removing the root and the child that replaces it is red
18
18 Rotations Operations for restructuring the tree after insert and delete operations on red-black trees Rotations take a red-black-tree and a node within the tree and: –Together with some node re-coloring they help restore the red-black-tree property –Change some of the pointer structure –Do not change the binary-search tree property Two types of rotations: –Left & right rotations
19
19 Left Rotations Assumptions for a left rotation on a node x : –The right child of x (y) is not NIL –Root’s parent is NIL Idea: –Pivots around the link from x to y –Makes y the new root of the subtree –x becomes y ’s left child –y ’s left child becomes x ’s right child
20
20 LEFT-ROTATE(T, x) 1.y ← right[x] ►Set y 2.right[x] ← left[y] ► y’s left subtree becomes x’s right subtree 3.if left[y] NIL 4. then p[left[y]] ← x ► Set the parent relation from left[y] to x 5.p[y] ← p[x] ► The parent of x becomes the parent of y 6.if p[x] = NIL 7. then root[T] ← y 8. else if x = left[p[x]] 9. then left[p[x]] ← y 10. else right[p[x]] ← y 11.left[y] ← x ► Put x on y’s left 12.p[x] ← y ► y becomes x’s parent
21
21 Example: LEFT-ROTATE
22
22 Right Rotations Assumptions for a right rotation on a node x : –The left child of y (x) is not NIL –Root’s parent is NIL Idea: –Pivots around the link from y to x –Makes x the new root of the subtree –y becomes x ’s right child –x ’s right child becomes y ’s left child
23
23 Insertion Goal: –Insert a new node z into a red-black-tree Idea: –Insert node z into the tree as for an ordinary binary search tree –Color the node red –Restore the red-black-tree properties Use an auxiliary procedure RB-INSERT-FIXUP
24
24 RB-INSERT (T, z) 1. y ← NIL 2. x ← root[T] 3. while x NIL 4. do y ← x 5. if key[z] < key[x] 6. then x ← left[x] 7. else x ← right[x] 8. p[z] ← y Initialize nodes x and y Throughout the algorithm y points to the parent of x Go down the tree until reaching a leaf At that point y is the parent of the node to be inserted Sets the parent of z to be y 26 1741 3047 3850
25
25 RB-INSERT (T, z) 9. if y = NIL 10. then root[T] ← z 11. else if key[z] < key[y] 12. then left[y] ← z 13. else right[y] ← z 14. left[z] ← NIL 15. right[z] ← NIL 16. color[z] ← RED 17. RB-INSERT-FIXUP (T, z) The tree was empty: set the new node to be the root Otherwise, set z to be the left or right child of y, depending on whether the inserted node is smaller or larger than y’s key Set the fields of the newly added node Fix any inconsistencies that could have been introduced by adding this new red node 26 1741 3047 3850
26
26 RB Properties Affected by Insert 1.Every node is either red or black 2.The root is black 3.Every leaf ( NIL ) is black 4.If a node is red, then both its children are black 5.For each node, all paths from the node to descendant leaves contain the same number of black nodes OK! If z is the root not OK OK! 26 1741 47 38 50 If p(z) is red not OK z and p(z) are both red OK!
27
27 RB-INSERT-FIXUP – Case 1 z’s “uncle” (y) is red Idea: (z is a right child) p[p[z]] (z’s grandparent) must be black: z and p[z] are both red Color p[z] black Color y black Color p[p[z]] red –Push the “red” violation up the tree Make z = p[p[z]]
28
CS 477/67728 RB-INSERT-FIXUP – Case 1 z ’s “uncle” (y) is red Idea: ( z is a left child) p[p[z]] ( z ’s grandparent) must be black: z and p[z] are both red color[ p[z]] black color[ y] black color p[p[z]] red z = p[p[z]] –Push the “red” violation up the tree Case1
29
29 RB-INSERT-FIXUP – Case 3 Case 3: z ’s “uncle” (y) is black z is a left child Case 3 Idea: color[ p[z]] black color[ p[p[z]]] red RIGHT-ROTATE(T, p[p[z]]) No longer have 2 reds in a row p[z] is now black Case3
30
30 RB-INSERT-FIXUP – Case 2 Case 2: z ’s “uncle” ( y ) is black z is a right child Idea: z p[z] LEFT-ROTATE( T, z) now z is a left child, and both z and p[z] are red case 3 Case 2Case 3 Case2
31
31 RB-INSERT-FIXUP (T, z) 1.while color[p[z]] = RED 2. do if p[z] = left[p[p[z]]] 3. then y ← right[p[p[z]]] 4. if color[y] = RED 5. then Case1 6. else if z = right[p[z]] 7. then Case2 8. Case3 9. else (same as then clause with “right” and “left” exchanged) 10.color[root[T]] ← BLACK The while loop repeats only when case1 is executed: O(lgn) times Set the value of x’s “uncle” We just inserted the root, or The red violation reached the root
32
32 Example 11 Insert 4 214 1 15 7 8 5 4 y 11 214 1 15 7 8 5 4 z Case 1 y z and p[z] are both red z’s uncle y is red z z and p[z] are both red z’s uncle y is black z is a right child Case 2 11 2 14 1 15 7 8 5 4 z y Case 3 z and p[z] are red z’s uncle y is black z is a left child 11 2 14 1 15 7 8 5 4 z
33
33 Analysis of RB-INSERT Inserting the new element into the tree O(lgn) RB-INSERT-FIXUP –The while loop repeats only if CASE 1 is executed –The number of times the while loop can be executed is O(lgn) Total running time of RB-INSERT: O(lgn)
34
Deletion Deletion from a Binary Search tree: IF the node to delete has 2 children, we find the successor node, move it’s information into the node we want to delete, and remove the successor node. ELSE we remove the node with the given value. In either case, the node that is actually removed will have 1 or 0 children. If it has a child, we set the child’s parent to the parent of the removed node. Else, if no children, we set the nil (leaf) to be the child of the parent of the removed node.
35
Deletion Deletion from a red black tree, is similar to deletion for a binary search tree, with a few exception: –Always set the parent of a deleted node, to be the parent of one of the deleted nodes children. –Red black fix-up method called if removed node is black. After a deletion of a red node (no violations occur): –No black-heights have been affected. The simple path from any node to a leaf contain the same # of black nodes. –No red nodes have been made adjacent (parent and child both red). –Deleted node is not the root since the root is black.
36
Delete Con’t After Deletion of a Black node a restore function must be called to fix red- black properties that might be violated. There are 3 possible initial violations. –If deleted node was the root, a red child might now be the root, Violation of property 2. –If both the parent of removed node, and a child of removed node are red, we have a violation of property 4. –The removal of a black node can cause the black-height of one path to be shorter (by 1), violating property 5. –We correct the problem of rule 5 by adding an extra “black” to the node passed into the fix-up procedure. This leads to violations in rules 1 since this node is now neither red or black. The goal now is to move the extra black to the root of the tree, and then get rid of the extra black.
37
Chapter 13P.37 13.4 Deletion RB-DELETE(T,z) 1 if left[z] = nil[T] or right[z] = nil[T] 2 then y ← z 3 else y ← TREE-SUCCESSOR(z) 4 if left[y] ≠ nil[T] 5 then x ← left[y] 6 else x ← right[y] 7 p[x] ← p[y] 8 if p[y] = nil[T]
38
Chapter 13P.38 9 then root[T] ← x 10 else if y = left[p[y]] 11then left[p[y]] ← x 12else right[p[y]] ← x 13 if y ≠ z 14 then key[z] ← key[y] 15 copy y’s satellite data into z 16 if color[y] = BLACK 17then RB-DELETE-FIXUP(T, x) 18 return y
39
Chapter 13P.39 RB-DELETE-FIXUP(T, x) RB-DELETE-FIXUP 1while x ≠ root[T] and color[x] = BLACK 2 do if x = left[p[x]] 3then w ← right[p[x]] 4 if color[w] = RED 5then color[w] ← BLACK Case1 6 color[p[x]] = RED Case1 7 LEFT-ROTATE(T,p[x]) Case1 8 w ← right[p[x]] Case1
40
Chapter 13P.40 9if color[left[w]] = BLACK and color[right[w]= BLACK 10 then color[w] ← RED Case2 11 x ← p[x] Case2 12 else if color[right[w]] = BLACK 13 then color[left[w]] ← BLACK Case3 14 color[w] ← RED Case3 15 RIGHT-ROTATE(T,w) Case3 16w ← right[p[x]] Case3 17 color[w] ← color[p[x]] Case4
41
Chapter 13P.41 18 color[p[x]] ← BLACK Case4 19 color[right[w]] ← BLACK Case4 20 LEFT-ROTATE(T,p[x]) Case4 21 x ← root[T] Case4 22 else (same as then clause with “right” and “left” exchanged) 23 color[x] ← BLACK
42
Chapter 13P.42 the case in the while loop of RB-D ELETE -F IXUP
43
Chapter 13P.43
44
44 65 50 80 10 60 7090 62 Perform the following deletions, in the order specified Delete 90, Delete 80, Delete 70
45
45 65 50 10 60 80 62 Delete 90: Case 2 70
46
46 65 50 10 60 70 62 Delete 80: Case 0
47
47 Delete 70: Case 1, then Case 3, then Case 4 50 65 10 6060 62 + 50 65 10 62 + 60 50 62 10 60 65 after Case 1 after Case 3 after Case 4
48
48 Red-Black Trees - Summary Operations on red-black-trees: –SEARCH O(h) –PREDECESSOR O(h) –SUCCESOR O(h) –MINIMUM O(h) –MAXIMUM O(h) –INSERT O(h) –DELETE O(h) Red-black-trees guarantee that the height of the tree will be O(lgn)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.