Red-Black Trees Binary Search Trees with O(log n) Worst-Case Time - - PowerPoint PPT Presentation

red black trees
SMART_READER_LITE
LIVE PREVIEW

Red-Black Trees Binary Search Trees with O(log n) Worst-Case Time - - PowerPoint PPT Presentation

Red-Black Trees Binary Search Trees with O(log n) Worst-Case Time per Operation The Red-Black Tree Properties Red-black tree properties Every node has a color, red or black. The root is black. The leaves are black. nil nil nil nil nil


slide-1
SLIDE 1

Red-Black Trees

Binary Search Trees with O(log n) Worst-Case Time per Operation

slide-2
SLIDE 2

The Red-Black Tree Properties

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. 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.

nil nil nil nil nil nil nil nil nil nil nil nil

slide-3
SLIDE 3

Corollary: Operations Tree-Minimum, Tree-Maximum, Tree-Predecessor, Tree- Successor, and Find take O(log n) time on a red-black tree of size n. Corollary: A red-black tree with n nodes has height at most 2 log (n + 1).

The Height of a Red-Black Tree with n Nodes

nil nil nil nil nil nil nil nil nil nil nil nil

Lemma: A red-black tree of black-height h contains at least 2h – 1 nodes. Lemma: A red-black tree of black-height h has height at most 2h.

slide-4
SLIDE 4

Rotations—The Key to Height Reduction

b d A B C a c e b d A B C a c e Right rotation Left rotation

slide-5
SLIDE 5

A Rotation Takes Constant Time

b d A B C a c e b d A B C a c e

Right rotation

d b c a e

slide-6
SLIDE 6

A Rotation Takes Constant Time

b d A B C a c e b d A B C a c e

Right rotation

d b c a e

slide-7
SLIDE 7

A Rotation Takes Constant Time

b d A B C a c e b d A B C a c e

Right rotation

d b c a e

slide-8
SLIDE 8

A Rotation Takes Constant Time

b d A B C a c e b d A B C a c e

Right rotation

d b c a e

slide-9
SLIDE 9

A Rotation Takes Constant Time

b d A B C a c e b d A B C a c e

Right rotation

c a e d b

slide-10
SLIDE 10

nil nil nil nil nil nil nil nil nil nil nil nil

Insertion into a Red-Black Tree

1 5 13 7 17 18 19 37 25 33 49 Element to insert: 21

slide-11
SLIDE 11

nil nil nil nil nil nil nil nil nil nil nil nil nil

Insertion into a Red-Black Tree

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. Every root-to-leaf path contains the same number of black nodes.

4

1 5 13 7 17 18 19 37 25 33 49 Element to insert: 21 21

4 4

slide-12
SLIDE 12

Insertion into a Red-Black Tree

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. Every root-to-leaf path contains the same number of black nodes.

4

Element to insert: 3

4 4

nil nil nil nil nil nil nil nil nil nil nil nil

1 5 13 7 17 18 19 37 25 33 49

slide-13
SLIDE 13

nil nil nil nil nil nil nil nil nil nil nil nil nil

Insertion into a Red-Black Tree

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. Every root-to-leaf path contains the same number of black nodes.

4

Element to insert: 3

4 4

1 5 13 7 17 18 19 37 25 33 49 3

4 8

No red-red property Goal: Restore no-red-red condition while maintaining the other four. Tools: Recoloring of nodes; rotations

slide-14
SLIDE 14

nil nil nil nil nil nil nil nil nil nil nil nil nil

Insertion into a Red-Black Tree

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. Every root-to-leaf path contains the same number of black nodes.

4

Element to insert: 3

4 4

1 5 13 7 17 18 19 37 25 33 49 3

4 8

No red-red property Goal: Restore no-red-red condition while maintaining the other four. Tools: Recoloring of nodes; rotations

slide-15
SLIDE 15

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.

Restoring the No-Red-Red Property

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

slide-16
SLIDE 16

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.

Assumptions:

■ Neither x nor parent[x] is the root ■ parent[x] is the left child of parent[parent[x]]

Restoring the No-Red-Red Property

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

slide-17
SLIDE 17

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.

Assumptions:

■ Neither x nor parent[x] is the root ■ parent[x] is the left child of parent[parent[x]]

Restoring the No-Red-Red Property

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

slide-18
SLIDE 18

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.

Assumptions:

■ Neither x nor parent[x] is the root ■ parent[x] is the left child of parent[parent[x]]

Restoring the No-Red-Red Property

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

slide-19
SLIDE 19

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

Case 1

Restoring the No-Red-Red Property

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 x

Recoloring

slide-20
SLIDE 20

Restoring the No-Red-Red Property

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 x parent[x] parent[parent[x]] x

Recoloring

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

Recoloring Right-rotation Case 1 Case 2

slide-21
SLIDE 21

Restoring the No-Red-Red Property

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 x parent[x] parent[parent[x]] x

Recoloring

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

Recoloring + Right-rotation Case 2 Case 1

slide-22
SLIDE 22

Restoring the No-Red-Red Property

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 x parent[x] parent[parent[x]] x

Recoloring

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

Case 2 Recoloring + Right-rotation Left-rotation Case 1 Case 3

slide-23
SLIDE 23

Lemma: An insertion into a red-black tree with n nodes takes O(log n) time and performs at most two rotations. 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

Insertion into a Red-Black Tree—Summary

slide-24
SLIDE 24

Deletion from a Red-Black Tree

nil nil nil nil nil nil nil nil nil nil nil nil

1 5 13 7 17 18 19 37 25 33 49 Element to delete: 25

slide-25
SLIDE 25

Deletion from a Red-Black Tree

1 5 13 7 17 18 19 37 33 49 Element to delete: 25

nil nil nil nil nil nil nil nil nil nil nil

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. Every root-to-leaf path contains the same number of black nodes.

4 4 4 4

slide-26
SLIDE 26

Deletion from a Red-Black Tree

Element to delete: 18 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. Every root-to-leaf path contains the same number of black nodes.

4 4 4 4

nil nil nil nil nil nil nil nil nil nil nil nil

1 5 13 7 17 18 19 37 25 33 49

slide-27
SLIDE 27

Deletion from a Red-Black Tree

1 5 13 7 17 19 37 25 33 49 Element to delete: 18

nil nil nil nil nil nil nil nil nil nil nil

4 4 4 4

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. Every root-to-leaf path contains the same number of black nodes.

8

slide-28
SLIDE 28

nil nil nil nil nil nil nil nil nil nil nil

Deletion from a Red-Black Tree

1 5 13 7 17 19 37 25 33 49 Element to delete: 18

4 4 4

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. Every root-to-leaf path contains the same number of black nodes. First step: Declare the child of the removed node “doubly black.”

4 8

slide-29
SLIDE 29

nil nil nil nil nil nil nil nil nil nil nil

Deletion from a Red-Black Tree

1 5 13 7 17 19 37 25 33 49 Element to delete: 18

4 4 4

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. Every root-to-leaf path contains the same number of black nodes. First step: Declare the child of the removed node “doubly black.”

4 4

slide-30
SLIDE 30

nil nil nil nil nil nil nil nil nil nil nil

Deletion from a Red-Black Tree

1 5 13 7 17 19 37 25 33 49 Element to delete: 18

4 4 4 4

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. Every root-to-leaf path contains the same number of black nodes.

8

First step: Declare the child of the removed node “doubly black.” Color property

slide-31
SLIDE 31

nil nil nil nil nil nil nil nil nil nil nil

Deletion from a Red-Black Tree

1 5 13 7 17 19 37 25 33 49 Element to delete: 18

4 4 4 4

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. Every root-to-leaf path contains the same number of black nodes.

8

First step: Declare the child of the removed node “doubly black.” Color property

slide-32
SLIDE 32

Restoring the Color Property

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

slide-33
SLIDE 33

x parent[x] w c c'

Case 2

x parent[x] w c

Case 1

Restoring the Color Property

Assume that x is the left child of its parent and that its right sibling w is black. 3 cases, depending on colors

  • f children of w:

■ Both are black ■ Right child is red ■ Left child is red, and right

child is black

Recoloring

x c c c'

Recoloring

c c'

Left-rotation

slide-34
SLIDE 34

Case 3

x parent[x] w c

Recoloring

Restoring the Color Property

Assume that x is the left child of its parent and that its right sibling w is black. 3 cases, depending on colors

  • f children of w:

■ Both are black ■ Right child is red ■ Left child is red, and right

child is black x parent[x] w c x c

Case 1 Recoloring

c c

Right-rotation Case 2

slide-35
SLIDE 35

Case 4

x w parent[x]

Restoring the Color Property

The missing case: The right sibling w of x is red. Observations:

■ Cases 2 and 3 perform at most two rotations; then we’re 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. x w (new) w (old)

Left-rotation

x w parent[x]

Recoloring

slide-36
SLIDE 36

Red-black trees support all dictionary operations in O(log n) time:

■ Insert ■ Delete ■ Find ■ Predecessor ■ Successor ■ Minimum ■ Maximum

To re-balance the tree after an update, only O(1) rotations are necessary. The latter will be handy for data structures that store more complicated information at every node, which is more expensive to restore after a rotation.

Red-Black Trees—Summary