SlideShare a Scribd company logo
1 of 87
Download to read offline
Algorithms

Sandeep Kumar Poonia
Head Of Dept. CS/IT
B.E., M.Tech., UGC-NET
LM-IAENG, LM-IACSIT,LM-CSTA, LM-AIRCC, LM-SCIEI, AM-UACEE
Algorithms
BST
Red Black Tree
Binary search trees
● Binary search trees are an important data

structure for dynamic sets.
● Accomplish many dynamic-set operations in
O(h) time, where h=height of tree.
● we represent a binary tree by a linked data
structure in which each node is an object.
● T:root points to the root of tree T .
Binary search trees
● Each node contains the attributes
■ key (and possibly other satellite data).
■ left: points to left child.
■ right: points to right child.

■ p: points to parent. T.root.p = NIL.
Binary search trees
● Stored keys must satisfy the binary-search-

tree property.
■ If y is in left subtree of x, then y.key <= x.key.
■ If y is in right subtree of x, then y.key >= x.key.
Binary search trees

(a) A binary search tree on 6 nodes with height 2.
(b) A less efficient binary search tree with height 4 that
contains the same keys.
Binary Search Trees

A binary search tree

Not a binary search tree
Binary search trees
● The binary-search-tree property allows us to

print out all the keys in a binary search tree in
sorted order by a simple recursive algorithm,
called an inorder tree walk.
It takes ‚
time to
walk an n-node
binary search tree
Binary search trees
● A: prints elements in sorted (increasing) order

● This is called an inorder tree walk
■ Preorder tree walk: print root, then left, then right
■ Postorder tree walk: print left, then right, then root
Tree traversal
● Used to print out the data in a tree in a certain

order
● Pre-order traversal
■ Print the data at the root
■ Recursively print out all data in the left subtree
■ Recursively print out all data in the right subtree
Preorder, Postorder and Inorder
● Preorder traversal
■ node, left, right
■ prefix expression
○ ++a*bc*+*defg
Preorder, Postorder and Inorder
● Postorder traversal
■ left, right, node
■ postfix expression
○ abc*+de*f+g*+

● Inorder traversal
■ left, node, right.
■ infix expression
○ a+b*c+d*e+f*g
Binary search trees
the running time of
TREE-SEARCH is
O(h), where h is
the height of the
tree.
Binary search trees
insert
● Proceed down the tree as you would with a find
● If X is found, do nothing (or update something)
● Otherwise, insert X at the last spot on the path traversed

● Time complexity = O(height of the tree)
delete
● When we delete a node, we need to consider

how we take care of the children of the deleted
node.
■ This has to be done such that the property of the

search tree is maintained.
delete
Three cases:
(1) the node is a leaf
■ Delete it immediately

(2) the node has one child
■ Adjust a pointer from the parent to bypass that node
delete
(3) the node has 2 children
■ replace the key of that node with the minimum element at the right

subtree
■ delete the minimum element
○ Has either no child or only right child because if it has a left child, that

left child would be smaller and would have been chosen. So invoke
case 1 or 2.

●

Time complexity = O(height of the tree)
Balanced Binary Search Trees
A binary search tree can implement any of the basic dynamic-set
operations in O(h) time. These operations are O(lgn) if tree is
“balanced”.
There has been lots of interest in developing algorithms to keep binary
search trees balanced, including
1st type: insert nodes as is done in the BST insert, then rebalance tree
 Red-Black trees
 AVL trees
 Splay trees
2nd type: allow more than one key per node of the search tree:
 2-3 trees
 2-3-4 trees
 B-trees
Red-Black Trees (RBT)
Red-Black tree: BST in which each node is colored red or black.

Constraints on the coloring and connection of nodes ensure that
no root to leaf path is more than twice as long as any other, so
tree is approximately balanced.
Each RBT node contains fields left, right, parent, color, and key.

L
E
F
T

PARENT
KEY
COLOR

R
I
G
H
T
Red-Black Trees
● Red-black trees:
■ Binary search trees augmented with node color
■ Operations designed to guarantee that the height

h = O(lg n)
● First: describe the properties of red-black trees
● Then: prove that these guarantee h = O(lg n)
● Finally: describe operations on red-black trees
Red-Black Properties
● The red-black properties:

1. Every node is either red or black
2. Every leaf (NULL pointer) is black
○ Note: this means every “real” node has 2 children

3. If a node is red, both children are black
○ Note: can’t have 2 consecutive reds on a path

4. Every path from node to descendent leaf contains
the same number of black nodes
5. The root is always black
Red-Black Trees
● Put example on board and verify properties:

1.
2.
3.
4.

Every node is either red or black
Every leaf (NULL pointer) is black
If a node is red, both children are black
Every path from node to descendent leaf contains
the same number of black nodes
5. The root is always black
● black-height: # black nodes on path to leaf
■ Label example with h and bh values
Black Height bh(x)
Black-height of a node x: bh(x) is the number of black nodes (including
the NIL leaf) on the path from x to a leaf, not counting x itself.
2 20
2 18
1 17

0

0

1 22

1 19

0

Every node has a
black-height, bh(x).
For all NIL leaves,
bh(x) = 0.

1 21 1 25

0

0

0 0

0

For root x,
bh(x) = bh(T).
Height of a Red-black Tree
h=4
26 bh=2

● Example:
● Height of a node:
■ Number of edges in a
longest path to a leaf.
● Black-height of a node

17

h=1
bh=1

h=2 30
bh=1

bh(x) is the number of
black nodes on path
from x to leaf, not
counting x.
nil[T]

h=3
41 bh=2

h=1
bh=1
38

h=2
47 bh=1
h=1 50
bh=1
Height of Red-Black Trees
● What is the minimum black-height of a node
●
●
●
●

with height h?
A height-h node has black-height  h/2
Proof : By property 4,
h/2 nodes on the path from the node to a leaf
are red.
Hence
are black.
Height of Red-Black Trees
● The subtree rooted at any node x contains >= 2bh(x)_1

internal nodes.
● Proof :By induction height of x =0x is a leafbh(x)=0.
The subtree rooted at x has 0 internal nodes. 20 -1 = 0.
● Let the height of x be h and bh(x) = b.
● Any child of x has height h -1 and
● black-height either b (if the child is red) or
● b -1 (if the child is black)
● By induction each child has >= 2bh(x)-1-1 internal nodes
● Thus, the subtree rooted at x contains >= 2(2bh(x)-1-1)+1
●
= 2bh(x)-1(internal Nodes)
Height of Red-Black Trees
● Theorem: A red-black tree with n internal

nodes has height h  2 lg(n + 1)
● How do you suppose we’ll prove this?
● Proof: The subtree rooted at any node x contains
● >= 2bh(x)_1 internal nodes.
● A height-h node has black-height  h/2

n  2h/2 -1
n + 1 2h/2
lg(n+1)  h/2  h  2lg(n+1)

● Thus
●
●
RB Trees: Worst-Case Time
● So we’ve proved that a red-black tree has

O(lg n) height
● Corollary: These operations take O(lg n) time:
■ Minimum(), Maximum()
■ Successor(), Predecessor()
■ Search()

● Insert() and Delete():
■ Will also take O(lg n) time
■ But will need special care since they modify tree
Red-Black Trees: An Example
● Color this tree:

7
5

9
12

Red-black properties:
1.
Every node is either red or black
2.
Every leaf (NULL pointer) is black
3.
If a node is red, both children are black
4.
Every path from node to descendent leaf
contains the same number of black nodes
5.
The root is always black
Red-Black Trees:
The Problem With Insertion
● Insert 8
■ Where does it go?

7
5

9
12

1.
2.
3.
4.
5.

Every node is either red or black
Every leaf (NULL pointer) is black
If a node is red, both children are black
Every path from node to descendent leaf
contains the same number of black nodes
The root is always black
Red-Black Trees:
The Problem With Insertion
● Insert 8
■ Where does it go?
■ What color

7
5

should it be?

1.
2.
3.
4.
5.

Every node is either red or black
Every leaf (NULL pointer) is black
If a node is red, both children are black
Every path from node to descendent leaf
contains the same number of black nodes
The root is always black

9
8

12
Red-Black Trees:
The Problem With Insertion
● Insert 8
■ Where does it go?
■ What color

7
5

should it be?

1.
2.
3.
4.
5.

Every node is either red or black
Every leaf (NULL pointer) is black
If a node is red, both children are black
Every path from node to descendent leaf
contains the same number of black nodes
The root is always black

9
8

12
Red-Black Trees:
The Problem With Insertion
● Insert 11
■ Where does it go?

7
5

9
8

1.
2.
3.
4.
5.

Every node is either red or black
Every leaf (NULL pointer) is black
If a node is red, both children are black
Every path from node to descendent leaf
contains the same number of black nodes
The root is always black

12
Red-Black Trees:
The Problem With Insertion
● Insert 11
■ Where does it go?
■ What color?

7
5

9
8

12
11

1.
2.
3.
4.
5.

Every node is either red or black
Every leaf (NULL pointer) is black
If a node is red, both children are black
Every path from node to descendent leaf
contains the same number of black nodes
The root is always black
Red-Black Trees:
The Problem With Insertion
● Insert 11
■ Where does it go?
■ What color?
○ Can’t be red! (#3)

7
5

9
8

12
11

1.
2.
3.
4.
5.

Every node is either red or black
Every leaf (NULL pointer) is black
If a node is red, both children are black
Every path from node to descendent leaf
contains the same number of black nodes
The root is always black
Red-Black Trees:
The Problem With Insertion
● Insert 11
■ Where does it go?
■ What color?
○ Can’t be red! (#3)
○ Can’t be black! (#4)

1.
2.
3.
4.
5.

7
5

Every node is either red or black
Every leaf (NULL pointer) is black
If a node is red, both children are black
Every path from node to descendent leaf
contains the same number of black nodes
The root is always black

9
8

12
11
Red-Black Trees:
The Problem With Insertion
● Insert 11
■ Where does it go?
■ What color?
○ Solution:
recolor the tree

1.
2.
3.
4.
5.

7
5

Every node is either red or black
Every leaf (NULL pointer) is black
If a node is red, both children are black
Every path from node to descendent leaf
contains the same number of black nodes
The root is always black

9
8

12
11
Red-Black Trees:
The Problem With Insertion
● Insert 10
■ Where does it go?

7
5

9
8

12
11

1.
2.
3.
4.
5.

Every node is either red or black
Every leaf (NULL pointer) is black
If a node is red, both children are black
Every path from node to descendent leaf
contains the same number of black nodes
The root is always black
Red-Black Trees:
The Problem With Insertion
● Insert 10
■ Where does it go?
■ What color?

7
5

9
8

12
11

1.
2.
3.
4.
5.

Every node is either red or black
Every leaf (NULL pointer) is black
If a node is red, both children are black
Every path from node to descendent leaf
contains the same number of black nodes
The root is always black

10
Red-Black Trees:
The Problem With Insertion
● Insert 10
■ Where does it go?

7
5

■ What color?
○ A: no color! Tree
is too imbalanced
○ Must change tree structure
to allow recoloring
■ Goal: restructure tree in

O(lg n) time

9
8

12
11
10
RB Trees: Rotation
● Our basic operation for changing tree structure

is called rotation:
y
x

x

rightRotate(y)

C

A

y

leftRotate(x)

A

B

B

C

● Does rotation preserve inorder key ordering?
● What would the code for rightRotate()

actually do?
RB Trees: Rotation
y
x
A

x

rightRotate(y)

C

A

B

y
B

● A lot of pointer manipulation
■ x keeps its left child
■ y keeps its right child

■ x’s right child becomes y’s left child
■ x’s and y’s parents change

C
RB Trees: Rotation
y
x
A

x

leftRotate(x)
rightRotate(y)

C

A

B

y
B

● A lot of pointer manipulation
■ x keeps its left child
■ y keeps its right child

■ y’s right child becomes x’s left child
■ x’s and y’s parents change

C
RB Trees: Rotation
Left-Rotate (T, x)
1. y  right[x] // Set y.
2. right[x]  left[y] //Turn y’s left subtree into x’s right subtree.
3. if left[y]  nil[T ]
4.
then p[left[y]]  x
5. p[y]  p[x]
// Link x’s parent to y.
6. if p[x] == nil[T ]
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
RB Trees: Rotation
right-Rotate (T, y)
1. x  right[y]
2. left[y]  right[x]
3. If right[x]  nil[T ]
4.
then p[right[x]]  y
5. p[x]  p[y]
6. if p[y] == nil[T ]
7.
then root[T ]  x
8.
else if y == left[p[y]]
9.
then left[p[y]]  x
10.
else right[p[y]]  x
11. right[x]  y
12. p[y]  x
Rotation Example
● Rotate left about 9:
7

5

9
8

12
11
Rotation Example
● Rotate left about 9:
7

5

12
9
8

11
Red-Black Trees: Insertion
● Remember:

1. Insert nodes one at a time, and after every
Insertion balance the tree.
2. Every node inserted starts as a Red node.
3. Consult the cases, Every time two Red nodes
touch must rebalance at that point.
4. The root will always be Black.
Red-Black Trees: Insertion
● If we insert, what color to make the new node?

■ Red? Might violate property 3(If a node is red, both
children are black).

■ Black? Might violate property 4(Every path from node
to descendent leaf contains the same number of black nodes).

Insertion: the basic idea
■ Insert x into tree, color x red

■ Only r-b property 3 might be violated (if p[x] red)
○ If so, move violation up tree until a place is found where it
can be fixed
■ Total time will be O(lg n)
Reminder: Red-black Properties
1. Every node is either red or black

2. Every leaf (NULL pointer) is black
3. If a node is red, both children are black
4. Every path from node to descendent leaf

contains the same number of black nodes
5. The root is always black
Insertion in RB Trees
● Insertion must preserve all red-black properties.

● Should an inserted node be colored Red? Black?
● Basic steps:
■ Use Tree-Insert from BST (slightly modified) to

insert a node x into T.
○ Procedure RB-Insert(x).

■ Color the node x red.

■ Fix the modified tree by re-coloring nodes and

performing rotation to preserve RB tree property.
○ Procedure RB-Insert-Fixup.
Algorithm: Insertion
We have detected a need for balance when z is red and his parent too.

• If z has a red uncle: colour the parent and uncle black, and
grandparent red.

z

Balanced trees: Red-black trees
Algorithm: Insertion
We have detected a need for balance when z is red and his parent too.

• If z has a red uncle: colour the parent and uncle black, and
grandparent red.
• If z is a left child and has a black uncle: colour the parent black and
the grandparent red, then rotateRight(z.parent.parent)

Balanced trees: Red-black trees
Algorithm: Insertion
We have detected a need for balance when z is red and his parent too.

• If z has a red uncle: colour the parent and uncle black, and
grandparent red.
right child and has black uncle: colour the parent black and
• If z is a left child and has a a black uncle, then rotateLeft(z.parent)
andgrandparent red, then rotateRight(z.parent.parent)
the

Balanced trees: Red-black trees
Algorithm: Insertion
Let’s insert 4, 7, 12, 15, 3 and 5.

4

7
1
2

Double red violation!

It also shows it’s
unbalanced…

Balanced trees: Red-black trees
Algorithm: Insertion
Let’s insert 4, 7, 12, 15, 3 and 5.

7
4

What should we do?

Nothing, no
double red.

3

12
5

15

Double red violation.
We can’t have a better
balance, and there is a
red uncle…

Balanced trees: Red-black trees
Insertion
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

RB-Insert(T, z) Contd.
14. left[z]  nil[T]
15. right[z]  nil[T]
16. color[z]  RED
17. RB-Insert-Fixup (T, z)

How does it differ from the TreeInsert procedure of BSTs?
Which of the RB properties might
be violated?
Fix the violations by calling RBInsert-Fixup.
Insertion – Fixup
● Which property might be violated?
1. OK(Every node is either red or black)
2. If z is the root, then there’s a violation. Otherwise,

OK(The root is black)
3. OK(Every leaf (NIL) is black)
4. If z.p is red, there’s a violation: both z and z.p are
red(If a node is red, then both its children are black)
● OK(For each node, all simple paths from the node to descendant
leaves contain the same number of black nodes)
● Remove the violation by calling RB-INSERT-FIXUP:
Insertion – Fixup
● Problem: we may have one pair of consecutive

reds where we did the insertion.
● Solution: rotate it up the tree and away…

Three cases have to be handled…
Insertion – Fixup
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
Insertion – Fixup
RB-Insert-Fixup(T, z) (Contd.)
9.
else if z == right[p[z]] // color[y]  RED
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 (if p[z] = right[p[p[z]]])(same as 10-14
16.
with “right” and “left” exchanged)
17. color[root[T ]]  BLACK
Insertion – Fixup

A node z ’after insertion. Because both z and its parent z.p are red, a
violation of property 4 occurs. Since z’s uncle y is red, case 1 in the
code applies. We recolor nodes and move the pointer z up the tree
Insertion – Fixup

Once again, z and its parent are both red, but z’s uncle y
is black. Since z is the right child of z.p, case 2 applies.
We perform a left rotation,
Insertion – Fixup

Now, z is the left child of its parent, and case 3 applies.
Recoloring and right rotation yield the tree
Insertion – Fixup
Correctness
Loop invariant:
● At the start of each iteration of the while loop,
■ z is red.
■ If p[z] is the root, then p[z] is black.
■ There is at most one red-black violation:
○ Property 2: z is a red root, or
○ Property 4: z and p[z] are both red.
Correctness – Contd.
● Initialization: 

● Termination: The loop terminates only if p[z] is

black. Hence, property 4 is OK.
The last line ensures property 2 always holds.
● Maintenance: We drop out when z is the root (since
then p[z] is sentinel nil[T ], which is black). When we
start the loop body, the only violation is of property 4.
■ There are 6 cases, 3 of which are symmetric to the other 3.

We consider cases in which p[z] is a left child.
■ Let y be z’s uncle (p[z]’s sibling).
Case 1 – uncle y is red

p[p[z]]

new z
C

C

p[z]

y

A

D
z



A





B





D







B

z is a right child here.
Similar steps if z is a left child.





● p[p[z]] (z’s grandparent) must be black, since z and p[z] are both red and there

are no other violations of property 4.
● Make p[z] and y black

 now z and p[z] are not both red. But property 5

might now be violated.
● Make p[p[z]] red

 restores property 5.

● The next iteration has p[p[z]] as the new z (i.e., z moves up 2 levels).
Case 1 – uncle y is red

We take the same action whether z is a right child or z is a left child.
Each subtree has a black root, and each has the same black-height.
The code for case 1 changes the colors of some nodes, preserving property
5: all downward simple paths from a node to a leaf have the same number
of blacks. The while loop continues with node z’s grandparent z.p.p as
the new z. Any violation of property 4 can now occur only between the new
z, which is red, and its parent, if it is red as well.
Case 2 – y is black, z is a right child
C

C

p[z]

p[z]

 y

A

 y

B

z



z

B







A





● Left rotate around p[z], p[z] and z switch roles  now z is a left

child, and both z and p[z] are red.
● Takes us immediately to case 3.
Case 3 – y is black, z is a left child
B

C
p[z]

 y

B
z



A



A



C







● Make p[z] black and p[p[z]] red.
● Then right rotate on p[p[z]]. Ensures property 4 is maintained.
● No longer have 2 reds in a row.
● p[z] is now black  no more iterations.


Cases 2 and 3 of the procedure RB-INSERT-FIXUP

As in case 1, property 4 is violated in either case 2 or case 3 because z
and its parent z.p are both red. Each subtree has a black root , and each
has the same black-height. We transform case 2 into case 3 by a left
rotation, which preserves property 5: all downward simple paths from a
node to a leaf have the same number of blacks. Case 3 causes some
color changes and a right rotation, which also preserve property 5. The
while loop then terminates, because property 4 is satisfied: there are no
longer two red nodes in a row.
Algorithm Analysis
● O(lg n) time to get through RB-Insert up to the

call of RB-Insert-Fixup.
● Within RB-Insert-Fixup:
■ Each iteration takes O(1) time.

■ Each iteration but the last moves z up 2 levels.
■ O(lg n) levels  O(lg n) time.
■ Thus, insertion in a red-black tree takes O(lg n) time.
■ Note: there are at most 2 rotations overall.
Deletion
● Deletion, like insertion, should preserve all the

RB properties.
● The properties that may be violated depends on
the color of the deleted node.
■ Red – OK. Why?

■ Black?

● Steps:
■ Do regular BST deletion.
■ Fix any violations of RB properties that may result.
Deletion
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] // Do this, even if x is nil[T]
Deletion
RB-Delete (T, z) (Contd.)
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

The node passed to
the fixup routine is the
lone child of the
spliced up node, or
the sentinel.
RB Properties Violation
● If the delete node is red?

Not a problem – no RB properties violated
● If y is black, we could have violations of redblack properties:
■ Prop. 1. OK.
■ Prop. 2. If y is the root and x is red, then the root has

become red.
■ Prop. 3. OK.
■ Prop. 4. Violation if p[y] and x are both red.
■ Prop. 5. Any path containing y now has 1 fewer black
RB Properties Violation
● Prop. 5. Any path containing y now has 1 fewer black

node.
■ Correct by giving x an “extra black.”
■ Add 1 to count of black nodes on paths containing x.
■ Now property 5 is OK, but property 1 is not.

■ x is either doubly black (if color[x] == BLACK) or red &

black (if color[x] == RED).
■ The attribute color[x] is still either RED or BLACK. No
new values for color attribute.
■ In other words, the extra blackness on a node is by virtue of
x pointing to the node.
● Remove the violations by calling RB-Delete-Fixup.
Deletion – Fixup
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
RB-Delete-Fixup(T, x) (Contd.)
/* x is still left[p[x]] */
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
Deletion – Fixup
● Idea: Move the extra black up the tree until x points
to a red & black node  turn it into a black node,
● x points to the root  just remove the extra black, or
● We can do certain rotations and recolorings and

finish.
● Within the while loop:
■ x always points to a nonroot doubly black node.
■ w is x’s sibling.
■ w cannot be nil[T ], since that would violate property 5 at

p[x].
● 8 cases in all, 4 of which are symmetric to the other.
Case 1 – w is red
p[x]
B

w

x
A



D
B

D



x
C



E

 





A



E

new
w





C





● w must have black children.
● Make w black and p[x] red (because w is red p[x] couldn’t have

been red).
● Then left rotate on p[x].
● New sibling of x was a child of w before rotation  must be
black.
● Go immediately to case 2, 3, or 4.
Case 2 – w is black, both w’s children
p[x]
are black new x
c
B

w

x
A



B

D

A




C



E

 

c

D


C





E

 

● Take 1 black off x ( singly black) and off w ( red).
● Move that black to p[x].
● Do the next iteration with p[x] as the new x.
● If entered this case from case 1, then p[x] was red  new x is
red & black  color attribute of new x is RED  loop

terminates. Then new x is made black in the last line.


Case 3 – w is black, w’s left child is
c
red, w’s right child is black
c
B
w

x
A



B

x

D

A




C



E

 

new w



C



D




E


● Make w red and w’s left child black.
● Then right rotate on w.

● New sibling w of x is black with a red right child  case 4.


Case 4 – w is black, w’s right child is
red
c
B

w

x
A



D
B

D


C



c’

 

x
E





E

A



C









● Make w be p[x]’s color (c).
● Make p[x] black and w’s right child black.
● Then left rotate on p[x].

● Remove the extra black on x ( x is now singly black) without

violating any red-black properties.
● All done. Setting x to root causes the loop to terminate.

More Related Content

What's hot

Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
ghhgj jhgh
 

What's hot (20)

Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
 
Binary Tree Traversal
Binary Tree TraversalBinary Tree Traversal
Binary Tree Traversal
 
1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
Binary tree
Binary  treeBinary  tree
Binary tree
 
Splay Tree
Splay TreeSplay Tree
Splay Tree
 
Binary search tree operations
Binary search tree operationsBinary search tree operations
Binary search tree operations
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Dijkstra
DijkstraDijkstra
Dijkstra
 
Trees
TreesTrees
Trees
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First Search
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
 
Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)
 
Spanning trees
Spanning treesSpanning trees
Spanning trees
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
 
Trees (data structure)
Trees (data structure)Trees (data structure)
Trees (data structure)
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
 

Similar to Red black tree

lecture 14
lecture 14lecture 14
lecture 14
sajinsc
 
lecture 13
lecture 13lecture 13
lecture 13
sajinsc
 
Advanced data structures and implementation
Advanced data structures and implementationAdvanced data structures and implementation
Advanced data structures and implementation
Umang Gupta
 

Similar to Red black tree (20)

lecture14.ppt
lecture14.pptlecture14.ppt
lecture14.ppt
 
Red Black Trees
Red Black TreesRed Black Trees
Red Black Trees
 
lecture 14
lecture 14lecture 14
lecture 14
 
Red black trees
Red black treesRed black trees
Red black trees
 
16 rbtrees
16 rbtrees16 rbtrees
16 rbtrees
 
lecture 13
lecture 13lecture 13
lecture 13
 
Red black trees
Red black treesRed black trees
Red black trees
 
Balance tree. Short overview
Balance tree. Short overviewBalance tree. Short overview
Balance tree. Short overview
 
Advanced data structures and implementation
Advanced data structures and implementationAdvanced data structures and implementation
Advanced data structures and implementation
 
part4-trees.ppt
part4-trees.pptpart4-trees.ppt
part4-trees.ppt
 
Red-black trees
Red-black treesRed-black trees
Red-black trees
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
 
15-btrees.ppt
15-btrees.ppt15-btrees.ppt
15-btrees.ppt
 
Balanced Tree(AVL Tree,Red Black Tree)
Balanced Tree(AVL Tree,Red Black Tree)Balanced Tree(AVL Tree,Red Black Tree)
Balanced Tree(AVL Tree,Red Black Tree)
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Chapter 5_Trees.pdf
Chapter 5_Trees.pdfChapter 5_Trees.pdf
Chapter 5_Trees.pdf
 
Tree and binary tree
Tree and binary treeTree and binary tree
Tree and binary tree
 
Avl tree
Avl treeAvl tree
Avl tree
 
Tree.pptx
Tree.pptxTree.pptx
Tree.pptx
 

More from Dr Sandeep Kumar Poonia

Memetic search in differential evolution algorithm
Memetic search in differential evolution algorithmMemetic search in differential evolution algorithm
Memetic search in differential evolution algorithm
Dr Sandeep Kumar Poonia
 
Performance evaluation of different routing protocols in wsn using different ...
Performance evaluation of different routing protocols in wsn using different ...Performance evaluation of different routing protocols in wsn using different ...
Performance evaluation of different routing protocols in wsn using different ...
Dr Sandeep Kumar Poonia
 

More from Dr Sandeep Kumar Poonia (20)

Soft computing
Soft computingSoft computing
Soft computing
 
An improved memetic search in artificial bee colony algorithm
An improved memetic search in artificial bee colony algorithmAn improved memetic search in artificial bee colony algorithm
An improved memetic search in artificial bee colony algorithm
 
Modified position update in spider monkey optimization algorithm
Modified position update in spider monkey optimization algorithmModified position update in spider monkey optimization algorithm
Modified position update in spider monkey optimization algorithm
 
Enhanced local search in artificial bee colony algorithm
Enhanced local search in artificial bee colony algorithmEnhanced local search in artificial bee colony algorithm
Enhanced local search in artificial bee colony algorithm
 
RMABC
RMABCRMABC
RMABC
 
Memetic search in differential evolution algorithm
Memetic search in differential evolution algorithmMemetic search in differential evolution algorithm
Memetic search in differential evolution algorithm
 
Improved onlooker bee phase in artificial bee colony algorithm
Improved onlooker bee phase in artificial bee colony algorithmImproved onlooker bee phase in artificial bee colony algorithm
Improved onlooker bee phase in artificial bee colony algorithm
 
Comparative study of_hybrids_of_artificial_bee_colony_algorithm
Comparative study of_hybrids_of_artificial_bee_colony_algorithmComparative study of_hybrids_of_artificial_bee_colony_algorithm
Comparative study of_hybrids_of_artificial_bee_colony_algorithm
 
A novel hybrid crossover based abc algorithm
A novel hybrid crossover based abc algorithmA novel hybrid crossover based abc algorithm
A novel hybrid crossover based abc algorithm
 
Multiplication of two 3 d sparse matrices using 1d arrays and linked lists
Multiplication of two 3 d sparse matrices using 1d arrays and linked listsMultiplication of two 3 d sparse matrices using 1d arrays and linked lists
Multiplication of two 3 d sparse matrices using 1d arrays and linked lists
 
Sunzip user tool for data reduction using huffman algorithm
Sunzip user tool for data reduction using huffman algorithmSunzip user tool for data reduction using huffman algorithm
Sunzip user tool for data reduction using huffman algorithm
 
New Local Search Strategy in Artificial Bee Colony Algorithm
New Local Search Strategy in Artificial Bee Colony Algorithm New Local Search Strategy in Artificial Bee Colony Algorithm
New Local Search Strategy in Artificial Bee Colony Algorithm
 
A new approach of program slicing
A new approach of program slicingA new approach of program slicing
A new approach of program slicing
 
Performance evaluation of different routing protocols in wsn using different ...
Performance evaluation of different routing protocols in wsn using different ...Performance evaluation of different routing protocols in wsn using different ...
Performance evaluation of different routing protocols in wsn using different ...
 
Enhanced abc algo for tsp
Enhanced abc algo for tspEnhanced abc algo for tsp
Enhanced abc algo for tsp
 
Database aggregation using metadata
Database aggregation using metadataDatabase aggregation using metadata
Database aggregation using metadata
 
Performance evaluation of diff routing protocols in wsn using difft network p...
Performance evaluation of diff routing protocols in wsn using difft network p...Performance evaluation of diff routing protocols in wsn using difft network p...
Performance evaluation of diff routing protocols in wsn using difft network p...
 
Lecture28 tsp
Lecture28 tspLecture28 tsp
Lecture28 tsp
 
Lecture27 linear programming
Lecture27 linear programmingLecture27 linear programming
Lecture27 linear programming
 
Lecture26
Lecture26Lecture26
Lecture26
 

Recently uploaded

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 

Recently uploaded (20)

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 

Red black tree

  • 1. Algorithms Sandeep Kumar Poonia Head Of Dept. CS/IT B.E., M.Tech., UGC-NET LM-IAENG, LM-IACSIT,LM-CSTA, LM-AIRCC, LM-SCIEI, AM-UACEE
  • 3. Binary search trees ● Binary search trees are an important data structure for dynamic sets. ● Accomplish many dynamic-set operations in O(h) time, where h=height of tree. ● we represent a binary tree by a linked data structure in which each node is an object. ● T:root points to the root of tree T .
  • 4. Binary search trees ● Each node contains the attributes ■ key (and possibly other satellite data). ■ left: points to left child. ■ right: points to right child. ■ p: points to parent. T.root.p = NIL.
  • 5. Binary search trees ● Stored keys must satisfy the binary-search- tree property. ■ If y is in left subtree of x, then y.key <= x.key. ■ If y is in right subtree of x, then y.key >= x.key.
  • 6. Binary search trees (a) A binary search tree on 6 nodes with height 2. (b) A less efficient binary search tree with height 4 that contains the same keys.
  • 7. Binary Search Trees A binary search tree Not a binary search tree
  • 8. Binary search trees ● The binary-search-tree property allows us to print out all the keys in a binary search tree in sorted order by a simple recursive algorithm, called an inorder tree walk. It takes ‚ time to walk an n-node binary search tree
  • 9. Binary search trees ● A: prints elements in sorted (increasing) order ● This is called an inorder tree walk ■ Preorder tree walk: print root, then left, then right ■ Postorder tree walk: print left, then right, then root
  • 10. Tree traversal ● Used to print out the data in a tree in a certain order ● Pre-order traversal ■ Print the data at the root ■ Recursively print out all data in the left subtree ■ Recursively print out all data in the right subtree
  • 11. Preorder, Postorder and Inorder ● Preorder traversal ■ node, left, right ■ prefix expression ○ ++a*bc*+*defg
  • 12. Preorder, Postorder and Inorder ● Postorder traversal ■ left, right, node ■ postfix expression ○ abc*+de*f+g*+ ● Inorder traversal ■ left, node, right. ■ infix expression ○ a+b*c+d*e+f*g
  • 13. Binary search trees the running time of TREE-SEARCH is O(h), where h is the height of the tree.
  • 15.
  • 16. insert ● Proceed down the tree as you would with a find ● If X is found, do nothing (or update something) ● Otherwise, insert X at the last spot on the path traversed ● Time complexity = O(height of the tree)
  • 17. delete ● When we delete a node, we need to consider how we take care of the children of the deleted node. ■ This has to be done such that the property of the search tree is maintained.
  • 18. delete Three cases: (1) the node is a leaf ■ Delete it immediately (2) the node has one child ■ Adjust a pointer from the parent to bypass that node
  • 19. delete (3) the node has 2 children ■ replace the key of that node with the minimum element at the right subtree ■ delete the minimum element ○ Has either no child or only right child because if it has a left child, that left child would be smaller and would have been chosen. So invoke case 1 or 2. ● Time complexity = O(height of the tree)
  • 20. Balanced Binary Search Trees A binary search tree can implement any of the basic dynamic-set operations in O(h) time. These operations are O(lgn) if tree is “balanced”. There has been lots of interest in developing algorithms to keep binary search trees balanced, including 1st type: insert nodes as is done in the BST insert, then rebalance tree  Red-Black trees  AVL trees  Splay trees 2nd type: allow more than one key per node of the search tree:  2-3 trees  2-3-4 trees  B-trees
  • 21. Red-Black Trees (RBT) Red-Black tree: BST in which each node is colored red or black. Constraints on the coloring and connection of nodes ensure that no root to leaf path is more than twice as long as any other, so tree is approximately balanced. Each RBT node contains fields left, right, parent, color, and key. L E F T PARENT KEY COLOR R I G H T
  • 22. Red-Black Trees ● Red-black trees: ■ Binary search trees augmented with node color ■ Operations designed to guarantee that the height h = O(lg n) ● First: describe the properties of red-black trees ● Then: prove that these guarantee h = O(lg n) ● Finally: describe operations on red-black trees
  • 23. Red-Black Properties ● The red-black properties: 1. Every node is either red or black 2. Every leaf (NULL pointer) is black ○ Note: this means every “real” node has 2 children 3. If a node is red, both children are black ○ Note: can’t have 2 consecutive reds on a path 4. Every path from node to descendent leaf contains the same number of black nodes 5. The root is always black
  • 24. Red-Black Trees ● Put example on board and verify properties: 1. 2. 3. 4. Every node is either red or black Every leaf (NULL pointer) is black If a node is red, both children are black Every path from node to descendent leaf contains the same number of black nodes 5. The root is always black ● black-height: # black nodes on path to leaf ■ Label example with h and bh values
  • 25. Black Height bh(x) Black-height of a node x: bh(x) is the number of black nodes (including the NIL leaf) on the path from x to a leaf, not counting x itself. 2 20 2 18 1 17 0 0 1 22 1 19 0 Every node has a black-height, bh(x). For all NIL leaves, bh(x) = 0. 1 21 1 25 0 0 0 0 0 For root x, bh(x) = bh(T).
  • 26. Height of a Red-black Tree h=4 26 bh=2 ● Example: ● Height of a node: ■ Number of edges in a longest path to a leaf. ● Black-height of a node 17 h=1 bh=1 h=2 30 bh=1 bh(x) is the number of black nodes on path from x to leaf, not counting x. nil[T] h=3 41 bh=2 h=1 bh=1 38 h=2 47 bh=1 h=1 50 bh=1
  • 27. Height of Red-Black Trees ● What is the minimum black-height of a node ● ● ● ● with height h? A height-h node has black-height  h/2 Proof : By property 4, h/2 nodes on the path from the node to a leaf are red. Hence are black.
  • 28. Height of Red-Black Trees ● The subtree rooted at any node x contains >= 2bh(x)_1 internal nodes. ● Proof :By induction height of x =0x is a leafbh(x)=0. The subtree rooted at x has 0 internal nodes. 20 -1 = 0. ● Let the height of x be h and bh(x) = b. ● Any child of x has height h -1 and ● black-height either b (if the child is red) or ● b -1 (if the child is black) ● By induction each child has >= 2bh(x)-1-1 internal nodes ● Thus, the subtree rooted at x contains >= 2(2bh(x)-1-1)+1 ● = 2bh(x)-1(internal Nodes)
  • 29. Height of Red-Black Trees ● Theorem: A red-black tree with n internal nodes has height h  2 lg(n + 1) ● How do you suppose we’ll prove this? ● Proof: The subtree rooted at any node x contains ● >= 2bh(x)_1 internal nodes. ● A height-h node has black-height  h/2 n  2h/2 -1 n + 1 2h/2 lg(n+1)  h/2  h  2lg(n+1) ● Thus ● ●
  • 30. RB Trees: Worst-Case Time ● So we’ve proved that a red-black tree has O(lg n) height ● Corollary: These operations take O(lg n) time: ■ Minimum(), Maximum() ■ Successor(), Predecessor() ■ Search() ● Insert() and Delete(): ■ Will also take O(lg n) time ■ But will need special care since they modify tree
  • 31. Red-Black Trees: An Example ● Color this tree: 7 5 9 12 Red-black properties: 1. Every node is either red or black 2. Every leaf (NULL pointer) is black 3. If a node is red, both children are black 4. Every path from node to descendent leaf contains the same number of black nodes 5. The root is always black
  • 32. Red-Black Trees: The Problem With Insertion ● Insert 8 ■ Where does it go? 7 5 9 12 1. 2. 3. 4. 5. Every node is either red or black Every leaf (NULL pointer) is black If a node is red, both children are black Every path from node to descendent leaf contains the same number of black nodes The root is always black
  • 33. Red-Black Trees: The Problem With Insertion ● Insert 8 ■ Where does it go? ■ What color 7 5 should it be? 1. 2. 3. 4. 5. Every node is either red or black Every leaf (NULL pointer) is black If a node is red, both children are black Every path from node to descendent leaf contains the same number of black nodes The root is always black 9 8 12
  • 34. Red-Black Trees: The Problem With Insertion ● Insert 8 ■ Where does it go? ■ What color 7 5 should it be? 1. 2. 3. 4. 5. Every node is either red or black Every leaf (NULL pointer) is black If a node is red, both children are black Every path from node to descendent leaf contains the same number of black nodes The root is always black 9 8 12
  • 35. Red-Black Trees: The Problem With Insertion ● Insert 11 ■ Where does it go? 7 5 9 8 1. 2. 3. 4. 5. Every node is either red or black Every leaf (NULL pointer) is black If a node is red, both children are black Every path from node to descendent leaf contains the same number of black nodes The root is always black 12
  • 36. Red-Black Trees: The Problem With Insertion ● Insert 11 ■ Where does it go? ■ What color? 7 5 9 8 12 11 1. 2. 3. 4. 5. Every node is either red or black Every leaf (NULL pointer) is black If a node is red, both children are black Every path from node to descendent leaf contains the same number of black nodes The root is always black
  • 37. Red-Black Trees: The Problem With Insertion ● Insert 11 ■ Where does it go? ■ What color? ○ Can’t be red! (#3) 7 5 9 8 12 11 1. 2. 3. 4. 5. Every node is either red or black Every leaf (NULL pointer) is black If a node is red, both children are black Every path from node to descendent leaf contains the same number of black nodes The root is always black
  • 38. Red-Black Trees: The Problem With Insertion ● Insert 11 ■ Where does it go? ■ What color? ○ Can’t be red! (#3) ○ Can’t be black! (#4) 1. 2. 3. 4. 5. 7 5 Every node is either red or black Every leaf (NULL pointer) is black If a node is red, both children are black Every path from node to descendent leaf contains the same number of black nodes The root is always black 9 8 12 11
  • 39. Red-Black Trees: The Problem With Insertion ● Insert 11 ■ Where does it go? ■ What color? ○ Solution: recolor the tree 1. 2. 3. 4. 5. 7 5 Every node is either red or black Every leaf (NULL pointer) is black If a node is red, both children are black Every path from node to descendent leaf contains the same number of black nodes The root is always black 9 8 12 11
  • 40. Red-Black Trees: The Problem With Insertion ● Insert 10 ■ Where does it go? 7 5 9 8 12 11 1. 2. 3. 4. 5. Every node is either red or black Every leaf (NULL pointer) is black If a node is red, both children are black Every path from node to descendent leaf contains the same number of black nodes The root is always black
  • 41. Red-Black Trees: The Problem With Insertion ● Insert 10 ■ Where does it go? ■ What color? 7 5 9 8 12 11 1. 2. 3. 4. 5. Every node is either red or black Every leaf (NULL pointer) is black If a node is red, both children are black Every path from node to descendent leaf contains the same number of black nodes The root is always black 10
  • 42. Red-Black Trees: The Problem With Insertion ● Insert 10 ■ Where does it go? 7 5 ■ What color? ○ A: no color! Tree is too imbalanced ○ Must change tree structure to allow recoloring ■ Goal: restructure tree in O(lg n) time 9 8 12 11 10
  • 43. RB Trees: Rotation ● Our basic operation for changing tree structure is called rotation: y x x rightRotate(y) C A y leftRotate(x) A B B C ● Does rotation preserve inorder key ordering? ● What would the code for rightRotate() actually do?
  • 44. RB Trees: Rotation y x A x rightRotate(y) C A B y B ● A lot of pointer manipulation ■ x keeps its left child ■ y keeps its right child ■ x’s right child becomes y’s left child ■ x’s and y’s parents change C
  • 45. RB Trees: Rotation y x A x leftRotate(x) rightRotate(y) C A B y B ● A lot of pointer manipulation ■ x keeps its left child ■ y keeps its right child ■ y’s right child becomes x’s left child ■ x’s and y’s parents change C
  • 46. RB Trees: Rotation Left-Rotate (T, x) 1. y  right[x] // Set y. 2. right[x]  left[y] //Turn y’s left subtree into x’s right subtree. 3. if left[y]  nil[T ] 4. then p[left[y]]  x 5. p[y]  p[x] // Link x’s parent to y. 6. if p[x] == nil[T ] 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
  • 47. RB Trees: Rotation right-Rotate (T, y) 1. x  right[y] 2. left[y]  right[x] 3. If right[x]  nil[T ] 4. then p[right[x]]  y 5. p[x]  p[y] 6. if p[y] == nil[T ] 7. then root[T ]  x 8. else if y == left[p[y]] 9. then left[p[y]]  x 10. else right[p[y]]  x 11. right[x]  y 12. p[y]  x
  • 48. Rotation Example ● Rotate left about 9: 7 5 9 8 12 11
  • 49. Rotation Example ● Rotate left about 9: 7 5 12 9 8 11
  • 50. Red-Black Trees: Insertion ● Remember: 1. Insert nodes one at a time, and after every Insertion balance the tree. 2. Every node inserted starts as a Red node. 3. Consult the cases, Every time two Red nodes touch must rebalance at that point. 4. The root will always be Black.
  • 51. Red-Black Trees: Insertion ● If we insert, what color to make the new node? ■ Red? Might violate property 3(If a node is red, both children are black). ■ Black? Might violate property 4(Every path from node to descendent leaf contains the same number of black nodes). Insertion: the basic idea ■ Insert x into tree, color x red ■ Only r-b property 3 might be violated (if p[x] red) ○ If so, move violation up tree until a place is found where it can be fixed ■ Total time will be O(lg n)
  • 52. Reminder: Red-black Properties 1. Every node is either red or black 2. Every leaf (NULL pointer) is black 3. If a node is red, both children are black 4. Every path from node to descendent leaf contains the same number of black nodes 5. The root is always black
  • 53. Insertion in RB Trees ● Insertion must preserve all red-black properties. ● Should an inserted node be colored Red? Black? ● Basic steps: ■ Use Tree-Insert from BST (slightly modified) to insert a node x into T. ○ Procedure RB-Insert(x). ■ Color the node x red. ■ Fix the modified tree by re-coloring nodes and performing rotation to preserve RB tree property. ○ Procedure RB-Insert-Fixup.
  • 54. Algorithm: Insertion We have detected a need for balance when z is red and his parent too. • If z has a red uncle: colour the parent and uncle black, and grandparent red. z Balanced trees: Red-black trees
  • 55. Algorithm: Insertion We have detected a need for balance when z is red and his parent too. • If z has a red uncle: colour the parent and uncle black, and grandparent red. • If z is a left child and has a black uncle: colour the parent black and the grandparent red, then rotateRight(z.parent.parent) Balanced trees: Red-black trees
  • 56. Algorithm: Insertion We have detected a need for balance when z is red and his parent too. • If z has a red uncle: colour the parent and uncle black, and grandparent red. right child and has black uncle: colour the parent black and • If z is a left child and has a a black uncle, then rotateLeft(z.parent) andgrandparent red, then rotateRight(z.parent.parent) the Balanced trees: Red-black trees
  • 57. Algorithm: Insertion Let’s insert 4, 7, 12, 15, 3 and 5. 4 7 1 2 Double red violation! It also shows it’s unbalanced… Balanced trees: Red-black trees
  • 58. Algorithm: Insertion Let’s insert 4, 7, 12, 15, 3 and 5. 7 4 What should we do? Nothing, no double red. 3 12 5 15 Double red violation. We can’t have a better balance, and there is a red uncle… Balanced trees: Red-black trees
  • 59. Insertion 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 RB-Insert(T, z) Contd. 14. left[z]  nil[T] 15. right[z]  nil[T] 16. color[z]  RED 17. RB-Insert-Fixup (T, z) How does it differ from the TreeInsert procedure of BSTs? Which of the RB properties might be violated? Fix the violations by calling RBInsert-Fixup.
  • 60. Insertion – Fixup ● Which property might be violated? 1. OK(Every node is either red or black) 2. If z is the root, then there’s a violation. Otherwise, OK(The root is black) 3. OK(Every leaf (NIL) is black) 4. If z.p is red, there’s a violation: both z and z.p are red(If a node is red, then both its children are black) ● OK(For each node, all simple paths from the node to descendant leaves contain the same number of black nodes) ● Remove the violation by calling RB-INSERT-FIXUP:
  • 61. Insertion – Fixup ● Problem: we may have one pair of consecutive reds where we did the insertion. ● Solution: rotate it up the tree and away… Three cases have to be handled…
  • 62. Insertion – Fixup 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
  • 63. Insertion – Fixup RB-Insert-Fixup(T, z) (Contd.) 9. else if z == right[p[z]] // color[y]  RED 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 (if p[z] = right[p[p[z]]])(same as 10-14 16. with “right” and “left” exchanged) 17. color[root[T ]]  BLACK
  • 64. Insertion – Fixup A node z ’after insertion. Because both z and its parent z.p are red, a violation of property 4 occurs. Since z’s uncle y is red, case 1 in the code applies. We recolor nodes and move the pointer z up the tree
  • 65. Insertion – Fixup Once again, z and its parent are both red, but z’s uncle y is black. Since z is the right child of z.p, case 2 applies. We perform a left rotation,
  • 66. Insertion – Fixup Now, z is the left child of its parent, and case 3 applies. Recoloring and right rotation yield the tree
  • 68. Correctness Loop invariant: ● At the start of each iteration of the while loop, ■ z is red. ■ If p[z] is the root, then p[z] is black. ■ There is at most one red-black violation: ○ Property 2: z is a red root, or ○ Property 4: z and p[z] are both red.
  • 69. Correctness – Contd. ● Initialization:  ● Termination: The loop terminates only if p[z] is black. Hence, property 4 is OK. The last line ensures property 2 always holds. ● Maintenance: We drop out when z is the root (since then p[z] is sentinel nil[T ], which is black). When we start the loop body, the only violation is of property 4. ■ There are 6 cases, 3 of which are symmetric to the other 3. We consider cases in which p[z] is a left child. ■ Let y be z’s uncle (p[z]’s sibling).
  • 70. Case 1 – uncle y is red p[p[z]] new z C C p[z] y A D z  A   B   D    B z is a right child here. Similar steps if z is a left child.   ● p[p[z]] (z’s grandparent) must be black, since z and p[z] are both red and there are no other violations of property 4. ● Make p[z] and y black  now z and p[z] are not both red. But property 5 might now be violated. ● Make p[p[z]] red  restores property 5. ● The next iteration has p[p[z]] as the new z (i.e., z moves up 2 levels).
  • 71. Case 1 – uncle y is red We take the same action whether z is a right child or z is a left child. Each subtree has a black root, and each has the same black-height. The code for case 1 changes the colors of some nodes, preserving property 5: all downward simple paths from a node to a leaf have the same number of blacks. The while loop continues with node z’s grandparent z.p.p as the new z. Any violation of property 4 can now occur only between the new z, which is red, and its parent, if it is red as well.
  • 72. Case 2 – y is black, z is a right child C C p[z] p[z]  y A  y B z  z B    A   ● Left rotate around p[z], p[z] and z switch roles  now z is a left child, and both z and p[z] are red. ● Takes us immediately to case 3.
  • 73. Case 3 – y is black, z is a left child B C p[z]  y B z  A  A  C    ● Make p[z] black and p[p[z]] red. ● Then right rotate on p[p[z]]. Ensures property 4 is maintained. ● No longer have 2 reds in a row. ● p[z] is now black  no more iterations. 
  • 74. Cases 2 and 3 of the procedure RB-INSERT-FIXUP As in case 1, property 4 is violated in either case 2 or case 3 because z and its parent z.p are both red. Each subtree has a black root , and each has the same black-height. We transform case 2 into case 3 by a left rotation, which preserves property 5: all downward simple paths from a node to a leaf have the same number of blacks. Case 3 causes some color changes and a right rotation, which also preserve property 5. The while loop then terminates, because property 4 is satisfied: there are no longer two red nodes in a row.
  • 75. Algorithm Analysis ● O(lg n) time to get through RB-Insert up to the call of RB-Insert-Fixup. ● Within RB-Insert-Fixup: ■ Each iteration takes O(1) time. ■ Each iteration but the last moves z up 2 levels. ■ O(lg n) levels  O(lg n) time. ■ Thus, insertion in a red-black tree takes O(lg n) time. ■ Note: there are at most 2 rotations overall.
  • 76. Deletion ● Deletion, like insertion, should preserve all the RB properties. ● The properties that may be violated depends on the color of the deleted node. ■ Red – OK. Why? ■ Black? ● Steps: ■ Do regular BST deletion. ■ Fix any violations of RB properties that may result.
  • 77. Deletion 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] // Do this, even if x is nil[T]
  • 78. Deletion RB-Delete (T, z) (Contd.) 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 The node passed to the fixup routine is the lone child of the spliced up node, or the sentinel.
  • 79. RB Properties Violation ● If the delete node is red? Not a problem – no RB properties violated ● If y is black, we could have violations of redblack properties: ■ Prop. 1. OK. ■ Prop. 2. If y is the root and x is red, then the root has become red. ■ Prop. 3. OK. ■ Prop. 4. Violation if p[y] and x are both red. ■ Prop. 5. Any path containing y now has 1 fewer black
  • 80. RB Properties Violation ● Prop. 5. Any path containing y now has 1 fewer black node. ■ Correct by giving x an “extra black.” ■ Add 1 to count of black nodes on paths containing x. ■ Now property 5 is OK, but property 1 is not. ■ x is either doubly black (if color[x] == BLACK) or red & black (if color[x] == RED). ■ The attribute color[x] is still either RED or BLACK. No new values for color attribute. ■ In other words, the extra blackness on a node is by virtue of x pointing to the node. ● Remove the violations by calling RB-Delete-Fixup.
  • 81. Deletion – Fixup 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
  • 82. RB-Delete-Fixup(T, x) (Contd.) /* x is still left[p[x]] */ 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
  • 83. Deletion – Fixup ● Idea: Move the extra black up the tree until x points to a red & black node  turn it into a black node, ● x points to the root  just remove the extra black, or ● We can do certain rotations and recolorings and finish. ● Within the while loop: ■ x always points to a nonroot doubly black node. ■ w is x’s sibling. ■ w cannot be nil[T ], since that would violate property 5 at p[x]. ● 8 cases in all, 4 of which are symmetric to the other.
  • 84. Case 1 – w is red p[x] B w x A  D B D  x C  E     A  E new w   C   ● w must have black children. ● Make w black and p[x] red (because w is red p[x] couldn’t have been red). ● Then left rotate on p[x]. ● New sibling of x was a child of w before rotation  must be black. ● Go immediately to case 2, 3, or 4.
  • 85. Case 2 – w is black, both w’s children p[x] are black new x c B w x A  B D A   C  E   c D  C   E   ● Take 1 black off x ( singly black) and off w ( red). ● Move that black to p[x]. ● Do the next iteration with p[x] as the new x. ● If entered this case from case 1, then p[x] was red  new x is red & black  color attribute of new x is RED  loop terminates. Then new x is made black in the last line. 
  • 86. Case 3 – w is black, w’s left child is c red, w’s right child is black c B w x A  B x D A   C  E   new w  C  D   E  ● Make w red and w’s left child black. ● Then right rotate on w. ● New sibling w of x is black with a red right child  case 4. 
  • 87. Case 4 – w is black, w’s right child is red c B w x A  D B D  C  c’   x E   E A  C     ● Make w be p[x]’s color (c). ● Make p[x] black and w’s right child black. ● Then left rotate on p[x]. ● Remove the extra black on x ( x is now singly black) without violating any red-black properties. ● All done. Setting x to root causes the loop to terminate.