Sei sulla pagina 1di 31

Introduction to Algorithms

Second Edition
by
Cormen, Leiserson, Rivest & Stein

Chapter 13
Red Black Tree
• A red-black tree is a binary search tree with
one extra bit of storage per node: its color,
which can be either RED of BLACK.
• Each node of tree now contains the field
color, key, left, right and p.
• If a child or the parent of a node does not
exist, the corresponding pointer field of the
node contains the value NIL.
Properties of red-black tree
• A binary search tree is a red-black tree if it
satisfies the following red-black 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.
5. For each node, all paths from the node to
descendant leaves contains the same number of
black nodes.
RED-BLACK TREE
Theorem: A red-black tree with n internal nodes
has height at most 2lg(n+1) or h <= 2log(n+1)

• We start by showing that the subtree rooted at any


node x contains at least 2bh(x)-1 internal nodes.
• We prove this clam by induction on the height of x.
– If the height of x is 0, then x must be a leaf, and the
subtree rooted at x indeed contains at least 2bh(x)-1 = 20-
1=0 internal nodes.
– For inductive step, consider a node x that has positive
height and is an internal node with two children.
– (2bh(x)-1-1) + (2bh(x)-1-1 )+1= = 2•2bh(x)-1 - 1 =2bh(x)-1
Cont..
• Thus at the root of the red-black tree:
n  2bh(root) - 1 (Why?)
According to property 5, 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
• n+1>=2h/2 (taking log)
• Log(n+1)>=h/2
• h <= 2log(n+1) (Hence proof)
Rotation
• The search tree operation TREE-INSERT and TREE-
DELETE, when run on a red-black tree with n keys,
take O(log n) time.
– Because they modify the tree, the result may violate the rad-
black properties.
• To restore these properties, we must change the colors
of some of the nodes in the tree and also change the
pointer structure.
• We change the pointer structure through rotation.
• There are two kinds of rotation:
– Left rotation
– Right rotation
Left Rotation
• When we do the left rotation on a node x, we assume that
its right child y is not nil[T];
• x may be any node in the tree whose right child is not
nil[T].
• The left rotation “pivots” around the link from x to y.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Left-Rotation Algorithm

//Set the parent of y left child as x

//Check x is left or right child

//Set y as right child of parent of x.


Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

•We can insert a node an n-


node RB tree in O(logn) time.

•RB Tree Insertion is slightly


modified version of BST.
•Insert a new node z in RB
Tree T and we color z red.
RB-Insert-Fixup (T, z)

There are two rules :


1) z is inserted in left subtree
Case 1(a) : z’s uncle y is red & z is right child.
Case1(b) : z’s uncle y is red & z is left child
Case 2 : z’s uncle y is black & z is a right child
Case 3 : z’s uncle y is black & z is a left child
2) Z is inserted in right subtree
Case 4(a) : Z’s uncle y is red & z is right child
Case4(b) : z’s uncle y is red & z is left child
Case 5 : z’s uncle y is black & z is a left child
Case 6 : z’s uncle y is black & z is a right child
RB-Insert-Fixup (T, z)
There are two rules :

1) z is inserted in left subtree 2) Z is inserted in right subtree


Case 1(a) : z’s uncle y is red & z is Case 4(a) : z’s uncle y is red & z is
right child. left child
i. Change the color & Find the new i. Same as case 1(a)
z Case4(b) : z’s uncle y is red & z is
Case1(b) : z’s uncle y is red & z is left right child
child i. Same as case 1(b)
i. Change the color & Find the new Case 5 : z’s uncle y is black & z is a
z left child
Case 2 : z’s uncle y is black & z is a i. Find the new z & Perform right
right child rotation
i. Find the new z & Perform left Case 6 : z’s uncle y is black & z is a
rotation right child
Case 3 : z’s uncle y is black & z is a i. Change color & Perform left
left child
rotation
i. Change color & Perform right
rotation
//Left subtree
// set uncle y
RB Insert Fixup
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Question??
Draw a Red-Black tree for the following
sequence:
1) 5, 10, 15, 25, 20 and 30
2) 41 ,38 ,31 ,12 ,19 and 8
3) 10, 90, 5, 20, 6 and 9
Analysis
• Running time of RB-Insert is O(logn)
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

RB-Delete
• The code is similar
to the delete.
operation for a BST
• If the node y that is
removed is black,
the fixup routine
restores the black
height of the tree.
Pointer changed-Red-black
trees -- Delete
• When y is eliminated, x becomes the new child of
y's parent.
• If y was red, no properties are violated.
• If y was black, we need to restructure the tree
• If we transfer y's blackness to x
– if x was red, it becomes black.
• all properties are now satisfied
– if x was black, it becomes doubly black.
• the red-or-black property is violated!
Red-black trees -- Delete
• To solve the double-black problem:
– propagate the extra "blackness" up the tree until
• we find a red node
– then, just make it black
-- OR --
• we reach the root
– then, just eliminate the extra black
-- OR --
• the problem gets fixed by rotating
Red-black trees -- Delete
• There are four cases we need to consider, some of
which reduce to others.
• In all of these cases, the next action depends on
the color of x’s sibling, w, and its children.
Case-1
• x sibling w is red (if color[w] ==RED)
– color[w]=BLACK
– color[p[x]] = RED
– LEFT-ROTATE(T, p[x])
– w=right[p[x]]

D B

Case 2, 3 and 4 occur when node w is black


Case-2
• x siblings w is black, and both of w children
are black
– color[w]=RED
– x=p[x]

D
Case-3
• x sibling w is black, w is left child is red,
and w right child is black
– color[left[w]] = BLACK
– color[w] = RED
– RIGHT-ROTATE(T, w)
– w=right[p[x]]

C D
Case-4
• x sibling w is black and w right child is red
– color[w] = color[p[x]]
– color[p[x]] = BLACK
– color[right[w]] = BLACK
– LEFT-ROTATE(T, p[X])
– x = root[T]

E
Analysis
• Running time of RB delete is O(logn)
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
University Questions
2015-16
Draw BSTs of height 2, 3 and 4 on the set of keys {10, 4, 5, 16, 1, 17, 21 }.
2014-15
What do you mean by augmenting data structure? Explain it with suitable example.
2013-14
Discuss insertion case of RB Tree.
How to augment the two data structure and also write an algorithm for retrieving an
element with given rank augmented RB tree.
2012-13
Write the pseudo code R-B-Inset-Fixup ( T, x)……….For inserting x in a RB Tree
2011-12
Prove that the height of RB Tree is 2log2(n+1).
What are the basic steps of Augmenting? Augment the BST.
2010-11
Show that RB tree to be resulted after successively inserting the values
10, 90, 5, 20, 6, 9
into an initially empty RB tree
2009-10
Explain RB tree and its various properties. Write an algorithm for inserting a node into an
n-node RB tree.
2008-09
Show that a RB tree with n-internal nodes has height at most 2log2(n+1).

Potrebbero piacerti anche