6331 - Algorithms, Spring 2014, CSE, OSU Lecture 5: Red ... · The height of a red-black tree Lemma...

30
6331 - Algorithms, Spring 2014, CSE, OSU Lecture 5: Red-black trees Instructor: Anastasios Sidiropoulos January 17, 2014

Transcript of 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 5: Red ... · The height of a red-black tree Lemma...

Page 1: 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 5: Red ... · The height of a red-black tree Lemma A red-black tree with n nodes has height O(logn). Proof. Any subtree rooted at

6331 - Algorithms, Spring 2014, CSE, OSULecture 5: Red-black trees

Instructor: Anastasios Sidiropoulos

January 17, 2014

Page 2: 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 5: Red ... · The height of a red-black tree Lemma A red-black tree with n nodes has height O(logn). Proof. Any subtree rooted at

Red-black trees

For every node x :

I x .color : red or black

I x .k : key

I x .p : pointer to the parent of x

I x .left : pointer to the left child of x

I x .right : pointer to the right child of x

Page 3: 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 5: Red ... · The height of a red-black tree Lemma A red-black tree with n nodes has height O(logn). Proof. Any subtree rooted at

Properties of binary search trees

Let x be a node.

I For any node y in the left subtree of x , we havey .key ≤ x .key .

I For any node y in the right subtree of x , we havey .key ≥ x .key .

Page 4: 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 5: Red ... · The height of a red-black tree Lemma A red-black tree with n nodes has height O(logn). Proof. Any subtree rooted at

Properties of red-black trees

Properties:

1. Every node is either red or black.

2. The root is black.

3. Every leaf is black, and is represented by NIL.

4. If a node is red, then both its children are black.

5. For each node x , all paths from x to a descendant leaf of xcontain the same number of black nodes.

Page 5: 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 5: Red ... · The height of a red-black tree Lemma A red-black tree with n nodes has height O(logn). Proof. Any subtree rooted at

Black-height

For a node x , the black-height of x , denoted bh(x) is the numberof black nodes on any path from, but not including, x , to adescendant leaf of x .

Page 6: 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 5: Red ... · The height of a red-black tree Lemma A red-black tree with n nodes has height O(logn). Proof. Any subtree rooted at

The height of a red-black tree

LemmaA red-black tree with n nodes has height O(log n).

Proof.Any subtree rooted at a node x contains at least 2bh(x) − 1 internalnodes.Proof by induction on height(x).Each child of a node x has black-height at least bh(x)− 1.. . .Let h be the height of the tree. Then, h is at most twice theblack-height of the root.n ≥ 2h/2 − 1.h = O(log n).

Page 7: 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 5: Red ... · The height of a red-black tree Lemma A red-black tree with n nodes has height O(logn). Proof. Any subtree rooted at

The height of a red-black tree

LemmaA red-black tree with n nodes has height O(log n).

Proof.Any subtree rooted at a node x contains at least 2bh(x) − 1 internalnodes.

Proof by induction on height(x).Each child of a node x has black-height at least bh(x)− 1.. . .Let h be the height of the tree. Then, h is at most twice theblack-height of the root.n ≥ 2h/2 − 1.h = O(log n).

Page 8: 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 5: Red ... · The height of a red-black tree Lemma A red-black tree with n nodes has height O(logn). Proof. Any subtree rooted at

The height of a red-black tree

LemmaA red-black tree with n nodes has height O(log n).

Proof.Any subtree rooted at a node x contains at least 2bh(x) − 1 internalnodes.Proof by induction on height(x).

Each child of a node x has black-height at least bh(x)− 1.. . .Let h be the height of the tree. Then, h is at most twice theblack-height of the root.n ≥ 2h/2 − 1.h = O(log n).

Page 9: 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 5: Red ... · The height of a red-black tree Lemma A red-black tree with n nodes has height O(logn). Proof. Any subtree rooted at

The height of a red-black tree

LemmaA red-black tree with n nodes has height O(log n).

Proof.Any subtree rooted at a node x contains at least 2bh(x) − 1 internalnodes.Proof by induction on height(x).Each child of a node x has black-height at least bh(x)− 1.. . .

Let h be the height of the tree. Then, h is at most twice theblack-height of the root.n ≥ 2h/2 − 1.h = O(log n).

Page 10: 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 5: Red ... · The height of a red-black tree Lemma A red-black tree with n nodes has height O(logn). Proof. Any subtree rooted at

The height of a red-black tree

LemmaA red-black tree with n nodes has height O(log n).

Proof.Any subtree rooted at a node x contains at least 2bh(x) − 1 internalnodes.Proof by induction on height(x).Each child of a node x has black-height at least bh(x)− 1.. . .Let h be the height of the tree. Then, h is at most twice theblack-height of the root.

n ≥ 2h/2 − 1.h = O(log n).

Page 11: 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 5: Red ... · The height of a red-black tree Lemma A red-black tree with n nodes has height O(logn). Proof. Any subtree rooted at

The height of a red-black tree

LemmaA red-black tree with n nodes has height O(log n).

Proof.Any subtree rooted at a node x contains at least 2bh(x) − 1 internalnodes.Proof by induction on height(x).Each child of a node x has black-height at least bh(x)− 1.. . .Let h be the height of the tree. Then, h is at most twice theblack-height of the root.n ≥ 2h/2 − 1.

h = O(log n).

Page 12: 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 5: Red ... · The height of a red-black tree Lemma A red-black tree with n nodes has height O(logn). Proof. Any subtree rooted at

The height of a red-black tree

LemmaA red-black tree with n nodes has height O(log n).

Proof.Any subtree rooted at a node x contains at least 2bh(x) − 1 internalnodes.Proof by induction on height(x).Each child of a node x has black-height at least bh(x)− 1.. . .Let h be the height of the tree. Then, h is at most twice theblack-height of the root.n ≥ 2h/2 − 1.h = O(log n).

Page 13: 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 5: Red ... · The height of a red-black tree Lemma A red-black tree with n nodes has height O(logn). Proof. Any subtree rooted at

Implications

The operations Search, Minimum, Maximum, Successor, andPredecessor take time O(log n).

What about Insertion and Deletion?

Page 14: 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 5: Red ... · The height of a red-black tree Lemma A red-black tree with n nodes has height O(logn). Proof. Any subtree rooted at

Implications

The operations Search, Minimum, Maximum, Successor, andPredecessor take time O(log n).

What about Insertion and Deletion?

Page 15: 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 5: Red ... · The height of a red-black tree Lemma A red-black tree with n nodes has height O(logn). Proof. Any subtree rooted at

Rotations

Left-Rotate(T , x)y = x .rightx .right = y .leftif y .left 6= NIL

y .left.p = xy .p = x .pif x .p = NIL

T .root = yelseif x = x .p.left

x .p.left = yelse x .p.right = yy .left = xx .p = y

Page 16: 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 5: Red ... · The height of a red-black tree Lemma A red-black tree with n nodes has height O(logn). Proof. Any subtree rooted at

Rotations

Page 17: 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 5: Red ... · The height of a red-black tree Lemma A red-black tree with n nodes has height O(logn). Proof. Any subtree rooted at

InsertionRB-Insert(T , z)

y = NILx = T .rootwhile x 6= NIL

y = xif z .key < x .key

x = x .leftelse x = x .right

z .p = yif y = NIL

T .root = zelseif z .key < y .key

y .left = zelse y .right = zz .left = NILz .right = NILz .color = REDRB-Insert-Fixup(T , z)

Page 18: 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 5: Red ... · The height of a red-black tree Lemma A red-black tree with n nodes has height O(logn). Proof. Any subtree rooted at

Insertion

Does RB-Insert create a valid Red-black tree?

What can go wrong?

I If the parent of z is RED, then we have two consecutive REDnodes.

I If T is empty, then after the insertion the root is RED.

Page 19: 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 5: Red ... · The height of a red-black tree Lemma A red-black tree with n nodes has height O(logn). Proof. Any subtree rooted at

Insertion

Does RB-Insert create a valid Red-black tree?

What can go wrong?

I If the parent of z is RED, then we have two consecutive REDnodes.

I If T is empty, then after the insertion the root is RED.

Page 20: 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 5: Red ... · The height of a red-black tree Lemma A red-black tree with n nodes has height O(logn). Proof. Any subtree rooted at

Insertion

Does RB-Insert create a valid Red-black tree?

What can go wrong?

I If the parent of z is RED, then we have two consecutive REDnodes.

I If T is empty, then after the insertion the root is RED.

Page 21: 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 5: Red ... · The height of a red-black tree Lemma A red-black tree with n nodes has height O(logn). Proof. Any subtree rooted at

FixupRB-Insert-Fixup(T , z)

while z .p.color = REDif z .p = z .p.p.left

y = z .p.p.rightif y .color = RED

z .p.color = BLACKy .color = BLACKz .p.p.color = REDz = z .p.p

elseif z = z .p.right

z = z .pLeft-Rotate(T .z)

z .p.color = BLACKz .p.p.color = REDRight-Rotate(T , z .p.p)

else (same with “left” and “right” exchanged)T.root.color = BLACK

Page 22: 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 5: Red ... · The height of a red-black tree Lemma A red-black tree with n nodes has height O(logn). Proof. Any subtree rooted at

Invariants of Fixup

(a) Node z is red.

(b) If z .p is the root, then z .p is black.

(c) If the tree violates any of the properties, then itviolates at most one of them, and the violation iseither 2, or 4.

I If it violates property 2, then z is the root and isred.

I If it violates property 4, then z and z .p are red,and no other node violates property 4.

Page 23: 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 5: Red ... · The height of a red-black tree Lemma A red-black tree with n nodes has height O(logn). Proof. Any subtree rooted at

Invariants of Fixup

Initialization?

(a) Node z is red.

(b) If z .p is the root, then z .p is black.

(c) If the tree violates any of the properties, then itviolates at most one of them, and the violation iseither 2, or 4.

I If it violates property 2, then z is the root and isred.

I If it violates property 4, then z and z .p are red,and no other node violates property 4.

Page 24: 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 5: Red ... · The height of a red-black tree Lemma A red-black tree with n nodes has height O(logn). Proof. Any subtree rooted at

Invariants of Fixup

Termination?

(a) Node z is red.

(b) If z .p is the root, then z .p is black.

(c) If the tree violates any of the properties, then itviolates at most one of them, and the violation iseither 2, or 4.

I If it violates property 2, then z is the root and isred.

I If it violates property 4, then z and z .p are red,and no other node violates property 4.

Page 25: 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 5: Red ... · The height of a red-black tree Lemma A red-black tree with n nodes has height O(logn). Proof. Any subtree rooted at

Maintenance of invariants of Fixup

Assume w.l.o.g. that z .p is a left child.The other case is symmetric.

Page 26: 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 5: Red ... · The height of a red-black tree Lemma A red-black tree with n nodes has height O(logn). Proof. Any subtree rooted at

Maintenance of invariants of Fixup

Case 1: z ’s uncle y is red.

Page 27: 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 5: Red ... · The height of a red-black tree Lemma A red-black tree with n nodes has height O(logn). Proof. Any subtree rooted at

Maintenance of invariants of Fixup

Case 2: z ’s uncle y is black and z is a right child.Case 3: z ’s uncle y is black and z is a left child.

Page 28: 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 5: Red ... · The height of a red-black tree Lemma A red-black tree with n nodes has height O(logn). Proof. Any subtree rooted at

Running time

The running time of RB-Insert-Fixup is O(log n).

Therefore, the running time of RB-Insert is O(log n).

Page 29: 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 5: Red ... · The height of a red-black tree Lemma A red-black tree with n nodes has height O(logn). Proof. Any subtree rooted at

Running time

The running time of RB-Insert-Fixup is O(log n).

Therefore, the running time of RB-Insert is O(log n).

Page 30: 6331 - Algorithms, Spring 2014, CSE, OSU Lecture 5: Red ... · The height of a red-black tree Lemma A red-black tree with n nodes has height O(logn). Proof. Any subtree rooted at

Running time

The running time of RB-Insert-Fixup is O(log n).

Therefore, the running time of RB-Insert is O(log n).