Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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)
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
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).