22
Struktur Data & Algoritme (Data Structures & Algorithms) Fakultas Ilmu Komputer Universitas Indonesia Semester Ganjil - 2006/2007 Version 2.0 - Internal Use Only Denny [email protected] Suryana Setiawan [email protected] Red Black Tree

Struktur Data & Algoritme - aren.cs.ui.ac.idaren.cs.ui.ac.id/sda/resources/sda2010/redblacktree.pdf · Struktur Data & Algoritme (Data Structures & Algorithms) Fakultas Ilmu Komputer

  • Upload
    others

  • View
    19

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Struktur Data & Algoritme - aren.cs.ui.ac.idaren.cs.ui.ac.id/sda/resources/sda2010/redblacktree.pdf · Struktur Data & Algoritme (Data Structures & Algorithms) Fakultas Ilmu Komputer

Struktur Data & Algoritme(Data Structures & Algorithms)

Fakultas Ilmu Komputer Universitas Indonesia

Semester Ganjil - 2006/2007Version 2.0 - Internal Use Only

Denny [email protected]

Suryana Setiawan [email protected]

Red Black Tree

Page 2: Struktur Data & Algoritme - aren.cs.ui.ac.idaren.cs.ui.ac.id/sda/resources/sda2010/redblacktree.pdf · Struktur Data & Algoritme (Data Structures & Algorithms) Fakultas Ilmu Komputer

SDA/RED-BLACK/DN-SUR/V2.0/2

Motivation History: invented by Rudolf Bayer (1972) who called them

"symmetric binary B-trees", its modern name by Leo J. Guibas and Robert Sedgewick ( 1978).

Red-black trees, along with AVL trees, offer the best possible worst-case guarantees for insertion time, deletion time, and search time -- O(log n).

many data structures used in computational geometry can be based on red-black trees.

in functional programming, used to construct associative arrays and sets which can retain previous versions after mutations.

Page 3: Struktur Data & Algoritme - aren.cs.ui.ac.idaren.cs.ui.ac.id/sda/resources/sda2010/redblacktree.pdf · Struktur Data & Algoritme (Data Structures & Algorithms) Fakultas Ilmu Komputer

SDA/RED-BLACK/DN-SUR/V2.0/3

Objectives Understand the definition, properties and operations of

Red-Black Trees.

Page 4: Struktur Data & Algoritme - aren.cs.ui.ac.idaren.cs.ui.ac.id/sda/resources/sda2010/redblacktree.pdf · Struktur Data & Algoritme (Data Structures & Algorithms) Fakultas Ilmu Komputer

SDA/RED-BLACK/DN-SUR/V2.0/4

Outline Red-Black Trees

Definition

Operation

Page 5: Struktur Data & Algoritme - aren.cs.ui.ac.idaren.cs.ui.ac.id/sda/resources/sda2010/redblacktree.pdf · Struktur Data & Algoritme (Data Structures & Algorithms) Fakultas Ilmu Komputer

SDA/RED-BLACK/DN-SUR/V2.0/5

Red-Black Trees: Definition

The red-black tree is a binary search tree whose nodes

colored either black or red, under properties:

Property#1. Every node is colored either red or black

Property#2. The root is black

Property#3. If a node is red, its children must be black

Property#4. Every path from a node to a null reference

must contain the same number of black nodes

Page 6: Struktur Data & Algoritme - aren.cs.ui.ac.idaren.cs.ui.ac.id/sda/resources/sda2010/redblacktree.pdf · Struktur Data & Algoritme (Data Structures & Algorithms) Fakultas Ilmu Komputer

SDA/RED-BLACK/DN-SUR/V2.0/6

30

15

5 50

10

70

8560

40 55

80 9065

20

Red-Black Trees

Example: (insertion sequence: 10, 85, 15, 70, 20, 60, 30, 50, 65, 80, 90, 40, 5, 55)

Page 7: Struktur Data & Algoritme - aren.cs.ui.ac.idaren.cs.ui.ac.id/sda/resources/sda2010/redblacktree.pdf · Struktur Data & Algoritme (Data Structures & Algorithms) Fakultas Ilmu Komputer

SDA/RED-BLACK/DN-SUR/V2.0/7

Implications

Consecutive red nodes are disallowed (Pr#3)

Every red node must have a black parent (Pr#3)

The longest possible path from the root to a leaf is no more than twice as long as the shortest possible path. (Pr#3 & Pr#4)

Page 8: Struktur Data & Algoritme - aren.cs.ui.ac.idaren.cs.ui.ac.id/sda/resources/sda2010/redblacktree.pdf · Struktur Data & Algoritme (Data Structures & Algorithms) Fakultas Ilmu Komputer

SDA/RED-BLACK/DN-SUR/V2.0/8

)1log(2)1log(

)1log()1log(2

1

2)1log()1log(

22log2log

212

1212

2

2

2

NHN

NBN

BNNB

BB

N

N

BB

BB

BB

B = total black nodes from root to leaf

N = total all nodes

H = height

All operation guaranteed logarithmic.

The least The most

Page 9: Struktur Data & Algoritme - aren.cs.ui.ac.idaren.cs.ui.ac.id/sda/resources/sda2010/redblacktree.pdf · Struktur Data & Algoritme (Data Structures & Algorithms) Fakultas Ilmu Komputer

SDA/RED-BLACK/DN-SUR/V2.0/9

Variants of the description

A red-black tree as a binary search tree whose edges(instead of nodes) are colored in red or black.

The color of a node in our terminology corresponds to the color of the edge connecting the node to its parent, except that the root node is always black.

30

15

5 50

10

70

8560

40 55

80 9065

20

30

15

5 50

10

70

8560

40 55

80 9065

20

Page 10: Struktur Data & Algoritme - aren.cs.ui.ac.idaren.cs.ui.ac.id/sda/resources/sda2010/redblacktree.pdf · Struktur Data & Algoritme (Data Structures & Algorithms) Fakultas Ilmu Komputer

SDA/RED-BLACK/DN-SUR/V2.0/10

Algorithm: Finding A node

As in the binary search node…

Page 11: Struktur Data & Algoritme - aren.cs.ui.ac.idaren.cs.ui.ac.id/sda/resources/sda2010/redblacktree.pdf · Struktur Data & Algoritme (Data Structures & Algorithms) Fakultas Ilmu Komputer

SDA/RED-BLACK/DN-SUR/V2.0/11

Algorithm: Inserting a Node (1)

Inserted as in binary search tree as a new leaf node, but the node must be colored red

why?

• new item is always inserted as leaf in the tree

• if we color a new item black, then we will have a longer path of black nodes (violate property #4)

Is the resulting tree still a red-black tree?

if the parent is black, no problemo

If it is the only node in the tree (i.e., it is a root), repaint it to be a black node.

if the parent is red(two consecutive red nodes violate Pr #3), then should be restructured…..

Page 12: Struktur Data & Algoritme - aren.cs.ui.ac.idaren.cs.ui.ac.id/sda/resources/sda2010/redblacktree.pdf · Struktur Data & Algoritme (Data Structures & Algorithms) Fakultas Ilmu Komputer

SDA/RED-BLACK/DN-SUR/V2.0/12

Algorithm: Inserting a Node (2)

Let, the new node is N, the Parent node is P, and the sibling of the parent node is U (from ‘uncle’), and the parent of P is G (from ‘grandparent’). If P and U are redand G is black, repaint P and U to be black, and G to be red (color flipping)

G, then,

• violate Pr#2 (when it is the root) G is changed just to be black (as in the first page).

• Violate Pr#3 (when G’s parent is red) perform the algo. with G as N.

G

N

UP

G

N

UP

Page 13: Struktur Data & Algoritme - aren.cs.ui.ac.idaren.cs.ui.ac.id/sda/resources/sda2010/redblacktree.pdf · Struktur Data & Algoritme (Data Structures & Algorithms) Fakultas Ilmu Komputer

SDA/RED-BLACK/DN-SUR/V2.0/13

Algorithm: Inserting a Node (3)

If P is red but U is black (or empty substree), then there are four subcases:

N is left child of P and U is right-sibling of P SRR

N is right child of P and U is left-sibling of P SLR

N is right child if P and U is right-sibling of P DRR

N is left child if P and U is left-sibling of P DLR

G

N

UP

G

N

U P

G

N

UP

G

N

U P

Page 14: Struktur Data & Algoritme - aren.cs.ui.ac.idaren.cs.ui.ac.id/sda/resources/sda2010/redblacktree.pdf · Struktur Data & Algoritme (Data Structures & Algorithms) Fakultas Ilmu Komputer

SDA/RED-BLACK/DN-SUR/V2.0/14

A B

G

N

UP

C D E A B

GN

U

P

C

D E

Single Rotation

N: new node

P: parent

U: uncle

G: grandparent

Color Changes:

P: red black

G: black red

Page 15: Struktur Data & Algoritme - aren.cs.ui.ac.idaren.cs.ui.ac.id/sda/resources/sda2010/redblacktree.pdf · Struktur Data & Algoritme (Data Structures & Algorithms) Fakultas Ilmu Komputer

SDA/RED-BLACK/DN-SUR/V2.0/15

B C

G

N

UP

A D E A B

GP

U

N

C

D E

Double Rotation

N: new node

P: parent

U: uncle

G: grandparent

Color Changes:

N: red black

G: black red

Page 16: Struktur Data & Algoritme - aren.cs.ui.ac.idaren.cs.ui.ac.id/sda/resources/sda2010/redblacktree.pdf · Struktur Data & Algoritme (Data Structures & Algorithms) Fakultas Ilmu Komputer

SDA/RED-BLACK/DN-SUR/V2.0/16

30

15

5

10

70

8560

50

55

80 9065

20

40

Insert 45 (original)

Page 17: Struktur Data & Algoritme - aren.cs.ui.ac.idaren.cs.ui.ac.id/sda/resources/sda2010/redblacktree.pdf · Struktur Data & Algoritme (Data Structures & Algorithms) Fakultas Ilmu Komputer

SDA/RED-BLACK/DN-SUR/V2.0/17

45

30

15

5

10

70

8560

50

55

80 9065

20

40

Insert 45

Violate Pr#3

Page 18: Struktur Data & Algoritme - aren.cs.ui.ac.idaren.cs.ui.ac.id/sda/resources/sda2010/redblacktree.pdf · Struktur Data & Algoritme (Data Structures & Algorithms) Fakultas Ilmu Komputer

SDA/RED-BLACK/DN-SUR/V2.0/18

45

5540

30

15

5

10

70

8560

50

55

80 9065

20

40

Insert 45 (color flip)

Color Flip

Violate Pr#3 50

Then, single rotation

Page 19: Struktur Data & Algoritme - aren.cs.ui.ac.idaren.cs.ui.ac.id/sda/resources/sda2010/redblacktree.pdf · Struktur Data & Algoritme (Data Structures & Algorithms) Fakultas Ilmu Komputer

SDA/RED-BLACK/DN-SUR/V2.0/19

45

30

15

5

10 70

85

60

50

55

80 90

65

20

40

Insert 45 (single rotation)

Page 20: Struktur Data & Algoritme - aren.cs.ui.ac.idaren.cs.ui.ac.id/sda/resources/sda2010/redblacktree.pdf · Struktur Data & Algoritme (Data Structures & Algorithms) Fakultas Ilmu Komputer

SDA/RED-BLACK/DN-SUR/V2.0/20

Top-down algorithm

Color-flipping described previously performed buttom-up wise (from the insertion position up to above).

Weiss’book recommends to do color-flipping top-down wise:

On the way going down from the root during in finding the insertion position, if the current node has two red children, color flip them (the current and the children).

The flipping will be possibly followed by other flipping as well as the rotation

When the insertion position reached and the new red node inserted, the next possibly operation is just the rotation

Page 21: Struktur Data & Algoritme - aren.cs.ui.ac.idaren.cs.ui.ac.id/sda/resources/sda2010/redblacktree.pdf · Struktur Data & Algoritme (Data Structures & Algorithms) Fakultas Ilmu Komputer

SDA/RED-BLACK/DN-SUR/V2.0/21

Algorithm: deletion

Firstly, the deletion following the binary seach tree’s deletion algorithms

Every deletion causes in deleting a leaf node or deleting an internal node with one child

Deleting red node no problemo

Deleting black node whose a red child will also be OK by switching its color with its child’s color

The problem, when both the deleted node and its child are black

Page 22: Struktur Data & Algoritme - aren.cs.ui.ac.idaren.cs.ui.ac.id/sda/resources/sda2010/redblacktree.pdf · Struktur Data & Algoritme (Data Structures & Algorithms) Fakultas Ilmu Komputer

SDA/RED-BLACK/DN-SUR/V2.0/22

Summary Red-Black trees use color as balancing information

instead of height in AVL trees.

An insertion may cause a local perturbation (two consecutive red nodes)

The pertubation is either

resolved locally (rotations), or

propagated to a higher level in the tree by recoloring (color flip)

O(1) for a rotation or color flip

At most one restructuring per insertion.

O(log n) color flips

Total time: O(log n)