Lec3 PDF
Lec3 PDF
39
20 41
11 35 40 56
2 23 61
21 30 112
28
Delete
39
20 41
Delete
11 35 40 56
2 23 61
21 30 112
28 33
Example
39
20 41
Delete
11 35 40 56
NULL NULL
2 23 61
21 30 112
28 33
Example
39
20 41
11 35
✘
40 56
2 23 61
21 30 112
28 33
Example
39
20 41
11 35 56
2 23 61
21 30 112
28 33
Example
39
20 41
11 35 40 56
Delete
2 23 61
21 30 112
28 33
Example
39
20 41
11 35 40 56
2 23
✘
61
21 30 112
28 33
Example
39
20 41
Delete
11 35 40 56
2 23 61
21 30 112
28 33
Example
39
20 41
11
✘
35 40 56
2 23 61
21 30 112
28 33
Example
39
20 41
11 35 40 56
2 23 61
Delete
21 30 112
28 33
Example
39
20 41
11 35 40 56
2 23 61
21 30 112
Successor
28 33
Example
39
20 41
11 35 40 56
2 23 61
21 33 112
Replace
28 33
Example
39
20 41
11 35 40 56
2 23 61
21 33 112
Delete
28
✘
33
Example
39
Delete
20 41
11 35 40 56
2 23 61
21 30 112
28 33
Example
39
20 41
11 35 40 56
2 23 61
21 30 112
Successor
28 33
Example
39
Replace
21 41
11 35 40 56
2 23 61
21 30 112
28 33
Example
39
21 41
11 35 40 56
2 23 61
✘
Delete
21 30 112
28 33
Example
Delete 39
20 51
11 35 41 56
2 23 44 61
21 30 43 49 112
28 33
Example
39
20 51
11 35 41 56
Successor
2 23 44 61
21 30 43 49 112
28 33
Example
Replace
41
20 51
11 35 41 56
2 23 44 61
21 30 43 49 112
28 33
Example
41
20 51
Delete
11 35
✘
41 56
2 23 44 61
21 30 43 49 112
28 33
Example
41
20 51
11 35 44 56
2 23 43 49 61
21 30 112
28 33
Running Time
We want to make sure this difference does not get too large.
Balancing a BST
X Y
Left Rotate
A X C
Y
B C A B
Example
41 41
20 51 20 51
ft
Le
11 35 41 56 11 35 44 56
2 23 44 61 2 23 41 49 61
21 30 43 49 112 21 30 43 112
28 33 28 33
Abstract Data Type
Set
maintains a set of elements from the universe
Red-Black Trees
Red-Black Trees (RBT) are Binary Search Trees that balance
themselves!
Data Structure
Red-Black Trees
Red-Black Trees (RBT) are Binary Search Trees that balance
themselves! RBTs have the following properties:
1. All nodes are colored either Red or Black.
2. The root node is black.
3. The leaf nodes (NIL) are black.
4. Both children of a red node are black.
5. For any node, all paths from the node to the descendant leaves
have the same number of black nodes.
Example
18
1. Every node is colored
either Red or Black.
2. The root node is black. 16 27
3. The leaf nodes (NIL)
are black.
13 17 23 34
4. Both children of a red
node are colored NIL NIL NIL NIL NIL NIL NIL
black. 1
5. For any node, all
NIL NIL
paths from the node
to the descendant
leaves have the same
number of black
nodes.
A Red-Black Tree supports all procedures of a BST:
I Insert(val) – Inserts val into the RBT rooted at node.
I Search(val) – Returns True of val exists in the BST rooted at
node. False otherwise.
I Succ(val) – Returns the smallest element greater than val in
the RBT.
I Pred(val) – Returns the largest element lesser than val in the
RBT.
I Delete(val) – Deletes val from the RBT.
A Red-Black Tree supports all procedures of a BST:
I Insert(val) – Inserts val into the RBT rooted at node.
I Search(val) – Returns True of val exists in the BST rooted at
node. False otherwise.
I Succ(val) – Returns the smallest element greater than val in
the RBT.
I Pred(val) – Returns the largest element lesser than val in the
RBT.
I Delete(val) – Deletes val from the RBT.
The procedures in green are implemented exactly like in a BST.
Black-Height
The black-height of a node X is the number of black colored nodes
encountered on a path starting from X to any leaf (excluding X
itself).
18
16 27
13 17 23 34
NIL NIL
Claim
A red-black tree with black-height β has height at most 2β.
Claim
A red-black tree with black-height β has height at most 2β.
Proof sketch:
I Try to construct the longest possible path with at most
β many black nodes.
I Property 4 will force you to color every alternate node
black.
Observations
Theorem
A red-black tree with n internal nodes has height at most
2 log(n + 1).
Observations
Theorem
A red-black tree with n internal nodes has height at most
2 log(n + 1).
Proof sketch
I Show that for any node X with black-height β, the
number of internal nodes in the subtree rooted at X is at
least 2β − 1.
I Conclude that with n internal nodes, the black-height
must be at most log(n + 1).
I Use previous claim that height is at most twice the
black-height to conclude the Theorem.