Sei sulla pagina 1di 6

The Red-Black Tree Properties

0. Every node has a color, red or black.

Data Structures
Dana Shapira Lecture #11 Red-Black Trees

1. The root is black. 2. The leaves are black. 3. Both children of a red node are black. 4. Every root-to-leaf path contains the same number of black nodes.

The black-height of a node x is the number of black nodes on a path from, but not including, x to a leaf. The black-height of T is the black-height of its root.

The Height of a Red-Black Tree with n Nodes


Lemma: A red-black tree rooted by x contains at least 2bh(x) 1 nodes. Proof: By Induction on the height of x. Corollary 1: A red-black tree with n nodes has height at most 2 log (n + 1). Proof: A red-black tree has black height at least h/2. Corollary 2: Operations Tree-Minimum, Tree-Maximum, Tree-Predecessor, Tree-Successor, and Find take O(log n) time on a red-black tree of size n.

Insertion into a Red-Black Tree


17 7 13 18 5 1 25 33 37 49 19

Element to insert: 21

Insertion into a Red-Black Tree


17 7 13 18 5 1 21 25 33 37 49 19 Red-black tree properties Every node has a color, red or black. The root is black. The leaves are black. Both children of a red node are black. Element to insert: 21 Every root-to-leaf path contains the same number of black nodes.

Insertion into a Red-Black Tree


17 7 13 18 5 1 25 33 37 49 19 Red-black tree properties Every node has a color, red or black. The root is black. The leaves are black. Both children of a red node are black. Element to insert: 3 Every root-to-leaf path contains the same number of black nodes.

Insertion into a Red-Black Tree


17 7 13 18 5 1 3 25 33 37 49 19 Red-black tree properties Every node has a color, red or black. The root is black. The leaves are black. Both children of a red node are black. Element to insert: 3 Every root-to-leaf path contains the same number of black nodes.

Restoring the No-Red-Red Property


Invariant: There is exactly one red node x in the tree whose parent may be red. Strategy: Fix the violation of no-red-red property at x This may violate the condition at some ancestor Continue fixing the property at the ancestor (the ancestor becomes x) Observations: Initially, x has a red parent; hence, x is not the root. If parent[x] is red, it is not the root either; that is, parent[parent[x]] exists. If parent[x] is black, we are done.

parent[parent[x]] ? parent[x] x

Restoring the No-Red-Red parent[parent[x]] Property


1. 2.

Recoloring
1.

Restoring the No-Red-Red parent[parent[x]] Property


2.

Recoloring

3.

3 cases: Both parent[x] and its sibling are red parent[x] is red, its sibling is black, and x is the left child of its parent As Case 2; but x is the right child of its parent

parent[x] x
Case 1
3.

3 cases: Both parent[x] and its sibling are red parent[x] is red, its sibling is black, and x is the left child of its parent As Case 2; but x is the right child of its parent

parent[x] x
Case 1

parent[parent[x]] parent[parent[x]]
Recoloring Right-rotation

Recoloring + Right-rotation

parent[x]
x Case 2

parent[x] x
Case 2

Restoring the No-Red-Red parent[parent[x]] Property


parent[x] x
Case 1

Recoloring

Insertion into a Red-Black TreeSummary


Observations: Every case takes constant time Case 1 brings x two steps closer to the root and only recolors nodes (no rotations) In Cases 2 and 3, we perform 1 or 2 rotations; then we are done

parent[parent[x]]
Left-rotation

Recoloring + Right-rotation

parent[x] x
Case 3 Case 2

Lemma: An insertion into a red-black tree with n nodes takes O(log n) time and performs at most two rotations.

Deletion from a Red-Black Tree


17 7 13 18 5 1 25 33 37 49 19

Deletion from a Red-Black Tree


17 7 13 18 5 1 33 37 49 19 Red-black tree properties Every node has a color, red or black. The root is black. The leaves are black. Both children of a red node are black.

Element to delete: 25

Element to delete: 25

Every root-to-leaf path contains the same number of black nodes.

Deletion from a Red-Black Tree


17 7 13 18 5 1 25 33 37 49 19 Red-black tree properties Every node has a color, red or black. The root is black. The leaves are black. Both children of a red node are black. Element to delete: 18 Every root-to-leaf path contains the same number of black nodes.

Deletion from a Red-Black Tree


17 7 13 5 1 25 33 37 49 19 Red-black tree properties Every node has a color, red or black. The root is black. The leaves are black. Both children of a red node are black. Element to delete: 18 Every root-to-leaf path contains the same number of black nodes.

Deletion from a Red-Black Tree


17 7 13 5 1 25 33 37 49 19

Deletion from a Red-Black Tree


17 7 13 5 1 25 33 37 49 19 Red-black tree properties Every node has a color, red or black.

Element to delete: 18

First step: Declare the child of the removed node doubly black.

Element to delete: 18

First step: Declare the child of the removed node doubly black.

Restoring the Color Property


The easy cases: Node x is red, root or not

Restoring the Color Property


Assume that x is the left child of its parent and that its right sibling w is black. parent[x] c
3 cases, depending on colors of children of w: Both are black Right child is red Left child is red, and right child is black Recoloring

x c w

1. 2. 3.

Case 1

parent[x] c Node x is the root and black x c' w


Recoloring

c c c'
Left-rotation

c'

Case 2

Assume that x is the left child of its parent and that its right sibling w is black. parent[x] c
3 cases, depending on colors of children of w: Both are black Right child is red Left child is red, and right child is black Recoloring

Restoring the Color Property


x w

Restoring the Color Property


The missing case: The right sibling w of x is red. parent[x] x w parent[x]
Recoloring Left-rotation

x c

w (old) w x w (new)

1. 2. 3.

Case 1

Case 4

parent[x] c x w
Recoloring

c
Right-rotation

Observations: Cases 2 and 3 perform at most two rotations; then were done Case 1 only recolors and brings us one step closer to the root Case 4 performs one rotation and bring us one step away from the root! But then Cases 2 and 3 finish as before If Case 1 applies after the rotation, the next iteration exits because parent[x] is red Lemma: A deletion from a red-black tree with n nodes takes O(log n) time and performs at most three rotations.

Case 3

Case 2

Red-Black TreesSummary
Red-black trees support all dictionary operations in O(log n) time: Insert Delete Find Predecessor Successor Minimum Maximum

Potrebbero piacerti anche