0% found this document useful (0 votes)
46 views27 pages

11 redBlackTrees

The document discusses Red-Black Trees, a type of balanced binary search tree that maintains a height of O(log n) for efficient search and update operations. It outlines the properties of Red-Black Trees, the process of insertion, and the necessary rotations to maintain balance. Additionally, it provides insights into the height of Red-Black Trees and offers examples to illustrate the insertion process.

Uploaded by

attendancedckyb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views27 pages

11 redBlackTrees

The document discusses Red-Black Trees, a type of balanced binary search tree that maintains a height of O(log n) for efficient search and update operations. It outlines the properties of Red-Black Trees, the process of insertion, and the necessary rotations to maintain balance. Additionally, it provides insights into the height of Red-Black Trees and offers examples to illustrate the insertion process.

Uploaded by

attendancedckyb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

CS 260 - Data Structures

Balanced Binary
Search Trees
- Red-Black Trees -
Yusuf Osmanlioglu
Department of Computer Science
Drexel University
Tree height in Binary Search Trees
● A tree with n nodes has height h.
1
● Best Case Tree Height: O(log2n)
○ Insert in order of 6, 4, 1, 10, 8, 5, 12
null 4
○ A perfectly balanced tree

● Worst Case Tree Height: O(n) null 5


○ Insert in order of 1, 4, 5, 6, 8, 10, 12
○ An unbalanced tree!!! null 6

6
null 8

4 10 null 10

null 12
1 5 8 12
Balanced Binary Search trees?
● Can we make sure the height is close to O(log2n)?
○ AVL, Red-Black Trees, or 2-3 Trees!
● Today we will cover Red-Black Trees!
Red-Black Trees

● They are balanced search trees, which means their height is O(log n).

● Most of the search and update operations on these trees take O(log n) time.

● The structure is well balanced, i.e. each subtree itself is a balanced search tree.
Red-Black Trees: Properties
1. Every node is either red or black.
2. The root is black.
x
3. Every leaf is NULL and black.
4. If a node is red, then both its children are
black.
5. All paths from a node x to any leaf have a b
same number of black nodes in between
(i.e., their Black-Height(x) is the same) NULL NULL NULL NULL
Example

1. Every node is either red 7


or black.
2. The root is black.
18
3. Every leaf is NULL and 3
black.
4. If a node is red, then
10 22
both its children are NULL NULL
black.
5. All paths from a node x
8 11 NULL 26
to any leaf have same
number of black nodes
in between (i.e., their
Black-Height(x) is the NULL NULL NULL NULL NULL NULL

same)
Height of a Red-Black tree
● We said, height of a Red-Black tree with n nodes is O(logn) in any case
(not only in the average or best case)
● What is the proof of that?
○ We will talk about that soon!!!
○ First, let’s get to see operations on Red-Black trees to understand how they
work to get acquainted with the data structure…
Rotation

B Right-Rotate(B) A

A B

S
L
L R R S
Left-Rotate(A)
Rotations
● Rotation is the basic operation for
maintaining balanced trees. A

● Maintains inorder key ordering:


○ (Left-Rotate) For all a in L, b in R, B
c in S we have a<=b<=c.
● Depth(L) increases by 1.
● Depth(R) stays the same.
L
● Depth(S) decreases by 1.
● Takes O(1) R S
x.p
x
Rotations A

Left-Rotate(T, x) y
y = x.right B
x.right = y.left
if y.left != NULL then
y.left.p = x
y.p = x.p
L
if x.p == NULL then
R S
T.root = y
else if x == x.p.left then
x.p.left = y
else B
x.p.right = y
A
y.left = x
x.p = y S
L R
Red-Black Tree: Insertion
1. Every node is either red or black.
2. The root is black.
● Insert x into tree.
3. Every leaf is NULL and black.
● Color x red.
4. If a node is red, then both its children are
● Red-black property 1 still holds: black.
○ since color of x is red 5. All paths from a node x to any leaf have
● Red-Black property 3 still holds: same number of black nodes in between
(i.e., their Black-Height(x) is the same)
○ since inserted node has NULL’s for
children, which are black.
● Red-black property 5 still holds: x
○ since x replaces a black NULL and
has NULL children, that are black.

a b

NULL NULL NULL NULL


Red-Black Tree: Insertion
1. Every node is either red or black.
2. The root is black.
● Two violations possible: 3. Every leaf is NULL and black.
○ If x.p is red, then property 4 is violated. 4. If a node is red, then both its children are
black.
○ If color of root is red, then property 2 is violated.
5. All paths from a node x to any leaf have
same number of black nodes in between
● To correct violation of rule 4, we move violation (i.e., their Black-Height(x) is the same)
up in tree until it can be fixed.
● No additional violations will be introduced during x
this process.
○ root can become red at some point, which will be fixed
using the same procedure.
a b
● For each iteration, there are six possible cases.
○ 3 of these cases are symmetric of the other. NULL NULL NULL NULL
Insertion: Case - 1 C
A x D
● x’s parent is the left child of x’s B
grandparent. 1 4 5
● x’s uncle is Red.
2 3
● Then
○ x.p.color= Black x
○ (x.p.p.right).color= Black
C
○ (x.p.p).color= Red
○ x=x.p.p A D
B
1 4 5
2 3
Insertion: Case - 1 (flip case) C
x A D
● x’s parent is the left child of x’s B
grandparent.
3 4 5
● x’s uncle is Red.
1 2
● Then
○ x.p.color= Black
x
○ (x.p.p.right).color= Black
C
○ (x.p.p).color= Red
○ x=x.p.p A D
B

3 4 5
1 2
Insertion: Case - 2 C
A x D
B
● x’s parent is the left child of x’s
grandparent. 1 4 5
● x’s uncle is Black. 2 3
● x is right child of x.p.
● Then
B
C
○ x = x.p x
○ Left-Rotate(T, x) x B D A C
○ (x.p).color = Black A D
○ (x.p.p).color = Red
○ Right-Rotate(T, x.p.p) 3 4 5 1 2 3
1 2 4 5
Insertion: Case - 3 C
x A D
● x’s parent is the left child of x’s
B
grandparent.
3 4 5
● x’s uncle is Black. 1 2
● x is the left child of x.p
C A
● Then x
x A D B C
○ (x.p).color = Black
B D
○ (x.p.p).color = Red
○ Right-Rotate(T, x.p.p) 3 4 5 1 2 3
1 2 4 5
Insertion: Case 2 &3
● Case 2 will always include Case 3
○ so they are not exclusive
Red-Black Trees: Insert
RB-Insert(T, x)
1: Tree-Insert(T,x)
2: x.color = Red
● Cases 4,5,6 are symmetric to 1,2,3 3: while x != T.root and x.p.color = Red
(x’s parent is the right child of x’s 4: if x.p = x.p.p.left then
5: y = x.p.p.right
grandparent). 6: if y.color = Red then
7: x.p.color = Black
● After case 2 or 3, no further 8: y.color = Black
correction is needed. 9: x.p.p.color = Red
10: x = x.p.p
11: else
if x = x.p.right then
12: x = x.p
13: Left-Rotate(T, x)
14: x.p.color = Black
15: x.p.p.color = Red
16: Right-Rotate(T, x.p.p)
17: else{same as the clause [line 5] with “Right” and “Left”
exchanged.}
18: T.root.color = Black
Example
● Use R-B Insert to insert element with key 4.

11

2 14

1 7 15

5 8
Example
● Use R-B Insert to insert element with key 4.

11 11

2 14 2 14

x
15 1 7 15
1 7
Case 2
x’s parent is the left
5 8 5 8 child of x’s
Case 1 grandparent.

x x’s parent is the left child of x’s


grandparent.
x’s uncle is Black.

4 x’s uncle is Red.


4 x is right child of x.p.
x = x.p
x.p.color= Black Left-Rotate(T, x)
(x.p.p.right).color= Black (x.p).color = Black
(x.p.p).color= Red (x.p.p).color = Red
x=x.p.p Right-Rotate(T, x.p.p)
Example
● Use R-B Insert to insert element with key 4.
11
11

2 14 7 14
x x
15 2 8
1 7 15
Case 2
5 8 x’s parent is the left Case 3
child of x’s 1 5 x’s parent is the left
grandparent. child of x’s
x’s uncle is Black. grandparent.

4 x is right child of x.p. x’s uncle is Black.


4
x = x.p x is the left child of x.p
Left-Rotate(T, x) (x.p).color = Black
(x.p).color = Black (x.p.p).color = Red
(x.p.p).color = Red Right-Rotate(T, x.p.p)
Right-Rotate(T, x.p.p)
Example
● Use R-B Insert to insert element with key 4.

11 7

x
7 14 2 11
x
2 8
15 1 8 14
5
Case 3
1 5 x’s parent is the left 15
child of x’s 4
grandparent.

x’s uncle is Black. Done


4
x is the left child of x.p
(x.p).color = Black
(x.p.p).color = Red
Right-Rotate(T, x.p.p)
Deletion

● Similar idea with insertion

● A bit more complicated


Height of a Red-Black Tree
⚫ A red-black tree with n keys has 7
height at most 2lg(n+1).
⚫ Proof (Intuition): Merge the red 18
nodes into their parents 3

10 22
NULL NULL

8 11 NULL 26

NULL NULL NULL NULL NULL NULL


Proof
● Produces a tree with 7
nodes having 2,3, or 18
4 children
10 22
● Height h’ of new tree 3
is black height of 8 11 26
the original tree

NULL NULL NULL NULL NULL NULL NULL NULL NULL


Proof
7

3
18 ●
NULL NULL 10 22 h ● A complete binary tree with height h’ has
8 11 NULL 26
internal nodes
NULL NULL NULL NULL NULL NULL
○ New tree has internal nodes branching 2,3,
or 4.
○ Number of internal nodes of new tree is
7
more than
18
○ Original tree has even more internal nodes
10 22
3 h’ than the new tree, thus
8 11 26

NULL NULL NULL NULL NULL NULL NULL NULL NULL


Proof
7

18
3

NULL NULL 10 22 h
8 11 NULL 26

NULL NULL NULL NULL NULL NULL

7
18

10 22
3 h’
8 11 26

NULL NULL NULL NULL NULL NULL NULL NULL NULL

You might also like