12.Red-black_trees
12.Red-black_trees
Right-Rotate(T,y)
• Idea:
– Pivots around the link from x to y
– Makes y the new root of the sub-tree
– x becomes y’s left child
– y’s left child becomes x’s right child
Example: LEFT-ROTATE
LEFT-ROTATE(T, x)
1. y ← right[x] ►Set y
2. right[x] ← left[y] ► y’s left sub-tree becomes x’s right sub-tree
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
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
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Properties violations
• Property 1 (each node black or red): hold
• Property 2: (root is black), not, if z is root
(and colored red).
• Proper 3: (each leaf is black sentinel): hold.
• Property 4: (the child of a red node must be
black), not, if z’s parent is red.
• Property 5: same number of blacks: hold
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Case 2: z’s uncle is black and z is a right child. Case 3: z’s uncle is black and z is a left child
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.