Docsity

Binary Search Trees - Analysis of Algorithms | CS 3343, Study notes of Algorithms and Programming

Partial preview of the text

Download Binary Search Trees - Analysis of Algorithms | CS 3343 and more Study notes Algorithms and Programming in PDF only on Docsity!

CS 334 Analysis of Algorithms

10/11/

CS 3343 – Fall 2007

Red-black trees

Carola Wenk

Slides courtesy of Charles Leiserson with small

changes by Carola Wenk

CS 334 Analysis of Algorithms

10/11/

Search Trees

A binary search tree is a binary tree. Each node stores a key. The tree fulfills the

binary search tree property

:

For every node

x

holds:

left(x)

≤

x

, if

x

’s left child

left(x)

exists

x

≤

right(x)

, if

x

’s right child

right(x)

exists

8 8

1515

1010

1818

2222

33

77

1212

1717

CS 334 Analysis of Algorithms

10/11/

ADT Dictionary / Dynamic Set

Abstract data type (ADT) Dictionary(also called Dynamic Set): A data structure which supports operations• Insert• Delete• FindUsing

balanced binary search trees

we can

implement a dictionary data structure such thateach operation takes

O(

log

n)

time. 8 8

1515

1010

1818

2222

33

77

1212

1717

CS 334 Analysis of Algorithms

10/11/

Balanced search trees

Balanced search tree:

A search-tree data

structure for which a height of

O

(log

n

) is

guaranteed when implementing a dynamicset of

n

items.

Examples:

  • AVL trees• 2-3 trees• 2-3-4 trees• B-trees• Red-black trees

CS 334 Analysis of Algorithms

10/11/

Example of a red-black tree

h

8 8

1111

1010

1818

2626

2222

33

77

NIL

NIL

NIL

NIL

NIL

NIL

NIL

NIL

NIL

CS 334 Analysis of Algorithms

10/11/

Example of a red-black tree

8 8

1111

1010

1818

2626

2222

33

77

NIL

NIL

NIL

NIL

NIL

NIL

NIL

NIL

NIL

  1. Every node is either red or black.

CS 334 Analysis of Algorithms

10/11/

Example of a red-black tree

8 8

1111

1010

1818

2626

2222

33

77

NIL

NIL

NIL

NIL

NIL

NIL

NIL

NIL

NIL

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

black.

CS 334 Analysis of Algorithms

10/11/

Example of a red-black tree

  1. All simple paths from any node

x

, excluding

x

, to a descendant leaf have the same number of black nodes =

black-height

x

8 8

1111

1010

1818

2626

2222

33

77

NIL

NIL

NIL

NIL

NIL

NIL

NIL

NIL

NIL

bh

= 2

bh

= 1

bh

= 1

bh

= 2

bh

= 0

CS 334 Analysis of Algorithms

10/11/

Height of a red-black tree

Theorem.

A red-black tree with n keys has height

h

2 log(

n

Proof.

(The book uses induction. Read carefully.)

I

NTUITION

  • Merge red nodes

into their blackparents.

CS 334 Analysis of Algorithms

10/11/

Height of a red-black tree

Theorem.

A red-black tree with n keys has height

h

2 log(

n

Proof.

(The book uses induction. Read carefully.)

I

NTUITION

  • Merge red nodes

into their blackparents.

CS 334 Analysis of Algorithms

10/11/

Height of a red-black tree

Theorem.

A red-black tree with n keys has height

h

2 log(

n

Proof.

(The book uses induction. Read carefully.)

I

NTUITION

  • Merge red nodes

into their blackparents.

CS 334 Analysis of Algorithms

10/11/

Height of a red-black tree

Theorem.

A red-black tree with

n

keys has height

h

2 log(

n

Proof.

(The book uses induction. Read carefully.)

  • This process produces a tree in which each node

has 2, 3, or 4 children.

  • The 2-3-4 tree has uniform depth

h

′^

of leaves.

I

NTUITION

  • Merge red nodes

into their blackparents.

h

CS 334 Analysis of Algorithms

10/11/

Query operations

Corollary.

The queries S

EARCH

, M

IN

M

AX

, S

UCCESSOR

, and P

REDECESSOR

all run in

O

(log

n

) time on a red-black

tree with

n

nodes.

8 8

1111

1010

1818

2626

2222

33

77

NIL

NIL

NIL NIL NIL NIL

NIL

NIL NIL

CS 334 Analysis of Algorithms

10/11/

Modifying operations

The operations I

NSERT

and D

ELETE

cause

modifications to the red-black tree:1. the operation itself,2. color changes,3. restructuring the links of the tree

via

“rotations”