Week5 PDF
Week5 PDF
Submitted by
Raja Rajeshwari Anugula & Srujana Tiruveedhi
Contents
RED – BLACK Trees
Properties
Rotations
Insertion
Deletion
RED BLACK TREES
A special class of binary trees that avoids the worst-case
behavior of O(n) that we can see in “plain” binary search
trees
Balanced: height is O(lg n), where n is the number of
nodes
Operations will take O(lg n) time in the worst case.
Consist of one extra bit of storage per node; its color,
which can be either RED or BLACK.
Each node of the tree contains fields: color, key, left,
right, parent.
Red-Black Trees ensure that longest path is no more
than twice as long as any other path
RB Tree Properties
RB Trees
Points to remember:
All leaves are empty (nil) and colored black.
We may use a single sentinel, T.nil, for all the leaves of
red-black tree T
T.nil.color is black
Root’s parent is also nil
All other attributes of binary search trees are inherited
by red-black trees (key, left, right, and p)
key of T.nil is not utilized
A D
B
a b c
Removing the Violations
Insertion, Discussion
Insertion, Case 1: y is red
Insertion, Case 2 & 3: y is black
z is a right child
z is a left child
RED BLACK TREES
Example:
11 Apply 11
Case 1b
2 14 2 14
7 15 7 15
1 1
5 8 8
Insert 5
Node
4
4 4
RED BLACK TREES
Example:
11 Apply 11
Case 2
2 14 7 14
7 15 8 15
1 2
5 8
1 5
4
4
RED BLACK TREES
Example:
11 Apply 7
Case 3
7 14 2 11
8 15
2 5 8 14
1
4 15
5
1
4
Insertion, Analysis
Deletion
RB-DELETE calls a RB-
TRANSPLANT procedure.
Then it calls RB-DELTE-
FIXUP because we could
have violated a red-black
property
TREE-MINIMUM returns the
minimum key (the minimum
key of a binary search tree is
located at the leftmost node)
y or x is the successor of z
RB-DELETE: Discussion
RB-Transplant RB-TRANSPLANT replaces the
subtree rooted at u by the
subtree rooted at v:
Changes u’s parent to v’s
parent (unless u is the root, in
which case it makes v the
root)
u’s parent gets v as either its
left or right child, depending
on whether u was a left or
right child
Doesn’t update v.left or v.right
RB-DELETE: Violations
RB-DELETE-FIXUP
RB-DELETE-FIXUP, The Idea
Note:
When a black node is deleted and replaced by a black child, the
child is marked as doubly black
When a black node is deleted and replaced by a red child (or
vice verse), the child is marked as red & black node
RB-DELETE-FIXUP, Case 1
RB-DELETE-FIXUP, Case 2
RB-DELETE-FIXUP, Case 3
RB-DELETE-FIXUP, Case 4
RB-DELETE, Analysis
HW
Exercises
13.1-3, 13.1-4
13.2-4
13.3-3, 13.3-4
13.4-6