You are on page 1of 12

Red-Black Tree | (Introduction)

Red-Black Tree is a self-balancing Binary Search Tree (BST) where every node follows
following rules.

1) Every node has a color either red or black.


2) Root of tree is always black.
3) There are no two adjacent red nodes (A red node cannot have a red parent or red child).
4) Every path from a node (including root) to any of its descendant NULL node has the same
number of black nodes.
5) Every leaf (NIL) is black.

Why Red-Black Trees?


Most of the BST operations (e.g., search, max, min, insert, delete.. etc) take O(h) time where h
is the height of the BST. The cost of these operations may become O(n) for a skewed Binary
tree. If we make sure that height of the tree remains O(Logn) after every insertion and deletion,
then we can guarantee an upper bound of O(Logn) for all these operations. The height of a
Red-Black tree is always O(Logn) where n is the number of nodes in the tree.

Comparison with AVL Tree


The AVL trees are more balanced compared to Red-Black Trees, but they may cause more
rotations during insertion and deletion. So if your application involves many frequent insertions
and deletions, then Red Black trees should be preferred. And if the insertions and deletions are
less frequent and search is a more frequent operation, then AVL tree should be preferred over
Red-Black Tree.
How does a Red-Black Tree ensure balance?
A simple example to understand balancing is, a chain of 3 nodes is not possible in the Red-
Black tree. We can try any combination of colours and see all of them violate Red-Black tree
property.
A chain of 3 nodes is nodes is not possible in Red-Black Trees.
Following are NOT Red-Black Trees
30 30 30
/ \ / \ / \
20 NIL 20 NIL 20 NIL
/ \ / \ / \
10 NIL 10 NIL 10 NIL
Violates Violates Violates
Property 4. Property 4 Property 3

Following are different possible Red-Black Trees with above 3 keys


20 20
/ \ / \
10 30 10 30
/ \ / \ / \ / \
NIL NIL NIL NIL NIL NIL NIL NIL

From the above examples, we get some idea how Red-Black trees ensure balance. Following is
an important fact about balancing in Red-Black Trees.
Black Height of a Red-Black Tree :
Black height is number of black nodes on a path from root to a leaf. Leaf nodes are also
counted black nodes. From above properties 3 and 4, we can derive, a Red-Black Tree of
height h has black-height >= h/2.
Number of nodes from a node to its farthest descendant leaf is no more than twice as the
number of nodes to the nearest descendant leaf.

Operations on RB Trees:
The search-tree operations TREE-INSERT and TREE-DELETE, when runs on a red-black tree
with n keys, take O (log n) time. Because they customize the tree, the conclusion may violate
the red-black properties. To restore these properties, we must change the color of some of the
nodes in the tree and also change the pointer structure .

1. Rotation:
Restructuring operations on red-black trees can generally be expressed more clearly in details
of the rotation operation.

Clearly, the order (Ax By C) is preserved by the rotation operation. Therefore, if we start with a
BST and only restructure using rotation, then we will still have a BST i.e. rotation do not break
the BST-Property.
LEFT ROTATE (T, x)
1. y ← right [x]
2. right [x] ← left [y]
3. p [left[y]] ← x
4. p[y] ← p[x]
5. If p[x] = nil [T]
then root [T] ← y
else if x = left [p[x]]
then left [p[x]] ← y
else right [p[x]] ← y
6. left [y] ← x.
7. p [x] ← y.

Example: Draw the complete binary tree of height 3 on the keys {1, 2, 3... 15}. Add the NIL
leaves and color the nodes in three different ways such that the black heights of the resulting
trees are: 2, 3 and 4.

Solution:

Tree with black-height-2


Tree with black-height-3

Tree with black-height-4


Red Black Tree Insertion Rules
1-If tree is empty, create new node as root node with color black

2-If tree is not empty, create new node as leaf node with color red

3-If parent of new node is black then exit

4-If parent of new node is red, then check the color of parents sibling of new node

a-If color is black or null then do suitable rotation & recolor

b-If color is red then recolor & also check if parents parent of new node is not root node then recolor
it & recheck

2. Insertion:
o Insert the new node the way it is done in Binary Search Trees.
o Color the node red
o If an inconsistency arises for the red-black tree, fix the tree according to the type of
discrepancy.

A discrepancy can decision from a parent and a child both having a red color. This type of
discrepancy is determined by the location of the node concerning grandparent, and the color of
the sibling of the parent.

RB-INSERT (T, z)
1. y ← nil [T]
2. x ← root [T]
3. while x ≠ NIL [T]
4. do y ← x
5. if key [z] < key [x]
6. then x ← left [x]
7. else x ← right [x]
8. p [z] ← y
9. if y = nil [T]
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 [T]
15. right [z] ← nil [T]
16. color [z] ← RED
17. RB-INSERT-FIXUP (T, z)
After the insert new node, Coloring this new node into black may violate the black-height
conditions and coloring this new node into red may violate coloring conditions i.e. root is black
and red node has no red children. We know the black-height violations are hard. So we color
the node red. After this, if there is any color violation, then we have to correct them by an RB-
INSERT-FIXUP procedure.

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 color [p[z]] ← BLACK //Case 1
6. color [y] ← BLACK //Case 1
7. color [p[p[z]] ← RED //Case 1
8. z ← p[p[z]] //Case 1
9. else if z= right [p[z]]
10. then z ← p [z] //Case 2
11. LEFT-ROTATE (T, z) //Case 2
12. color [p[z]] ← BLACK //Case 3
13. color [p [p[z]]] ← RED //Case 3
14. RIGHT-ROTATE (T,p [p[z]]) //Case 3
15. else (same as then clause)
With "right" and "left" exchanged
16. color [root[T]] ← BLACK

Example: Show the red-black trees that result after successively inserting the keys
41,38,31,12,19,8 into an initially empty red-black tree.

Solution:

Insert 41
Insert 19
Thus the final tree is
3. Deletion:
First, search for an element to be deleted

o If the element to be deleted is in a node with only left child, swap this node with one
containing the largest element in the left subtree. (This node has no right child).
o If the element to be deleted is in a node with only right child, swap this node with the one
containing the smallest element in the right subtree (This node has no left child).
o If the element to be deleted is in a node with both a left child and a right child, then swap
in any of the above two ways. While swapping, swap only the keys but not the colors.
o The item to be deleted is now having only a left child or only a right child. Replace this
node with its sole child. This may violate red constraints or black constraint. Violation of
red constraints can be easily fixed.
o If the deleted node is black, the black constraint is violated. The elimination of a black
node y causes any path that contained y to have one fewer black node.
o Two cases arise:
o The replacing node is red, in which case we merely color it black to make up for
the loss of one black node.
o The replacing node is black.

The strategy RB-DELETE is a minor change of the TREE-DELETE procedure. After splicing out
a node, it calls an auxiliary procedure RB-DELETE-FIXUP that changes colors and performs
rotation to restore the red-black properties.

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]
9. then root [T] ← x
10. else if y = left [p[y]]
11. then left [p[y]] ← x
12. else 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
17. then RB-delete-FIXUP (T, x)
18. return y
RB-DELETE-FIXUP (T, x)
1. while x ≠ root [T] and color [x] = BLACK
2. do if x = left [p[x]]
3. then w ← right [p[x]]
4. if color [w] = RED
5. then color [w] ← BLACK //Case 1
6. color [p[x]] ← RED //Case 1
7. LEFT-ROTATE (T, p [x]) //Case 1
8. w ← right [p[x]] //Case 1
9. If color [left [w]] = BLACK and color [right[w]] = BLACK
10. then color [w] ← RED //Case 2
11. x ← p[x] //Case 2
12. else if color [right [w]] = BLACK
13. then color [left[w]] ← BLACK //Case 3
14. color [w] ← RED //Case 3
15. RIGHT-ROTATE (T, w) //Case 3
16. w ← right [p[x]] //Case 3
17. color [w] ← color [p[x]] //Case 4
18. color p[x] ← BLACK //Case 4
19. color [right [w]] ← BLACK//Case 4
20. LEFT-ROTATE (T, p [x]) //Case 4
21. x ← root [T] //Case 4
22. else (same as then clause with "right" and "left" exchanged)
23. color [x] ← BLACK

Example: In a previous example, we found that the red-black tree that results from successively
inserting the keys 41,38,31,12,19,8 into an initially empty tree. Now show the red-black trees
that result from the successful deletion of the keys in the order 8, 12, 19,31,38,41.

Solution:
Delete 38

Delete 41
Lemma: A red-black tree with n internal nodes has height at most 2 lg(n + 1).
Proof We first show that the subtree rooted at any node x contains at least 2bh(x) -1 internal
nodes. We prove this claim by induction on the height of x. If the height of x is 0, then x must be
a leaf (NIL), and the subtree rooted at x indeed contains at least 2bh(x)-1 = 20 - 1 = 0 internal
nodes. 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. Since the height of a child of x is less than the
height of x itself, we can apply the inductive hypothesis to conclude that each child has at least
2bh(x) -1 -1 internal nodes. Thus, the subtree rooted at x contains at least (2bh(x)-1 -1) + (2bh(x)-1 -1) +
1 = 2bh(x)- 1 internal nodes, which proves the claim.

To complete the proof of the lemma, let h be the height of the tree. According to property 3, at
least half the nodes on any simple path from the root to a leaf, not including the root, must be
black. Consequently, the black-height of the root must be at least h/2; thus,

n 2h/2 - 1.

Moving the 1 to the left-hand side and taking logarithms on both sides yields lg (n + 1) h/2,
or h 21g (n + 1).

An immediate consequence of this lemma is that the dynamic-set


operations SEARCH, MINIMUM, MAXIMUM, SUCCESSOR, and PREDECESSOR can be
implemented in O(lg n) time on red-black trees, since they can be made to run in O(h) time on a
search tree of height h and any red-black tree on n nodes is a search tree with height O(lg n).
Although the algorithms TREE-INSERT and TREE-DELETE run in O(lg n) time when given a
red-black tree as input, they do not directly support the dynamic-set
operations INSERT and DELETE, since they do not guarantee that the modified binary search
tree will be a red-black tree. however, that these two operations can indeed be supported
in O(lg n) time.

You might also like