Docsity

robert sedgewick, Study notes of Construction

Partial preview of the text

Download robert sedgewick and more Study notes Construction in PDF only on Docsity!

ROBERT SEDGEWICK | KEVIN WAYNE

F O U R T H E D I T I O N

Algorithms

h t t p : / / a l g s 4. c s. p r i n c e t o n. e d u

Algorithms

ROBERT SEDGEWICK | KEVIN WAYNE

3.3 BALANCED SEARCH TREES

2-3 search trees

red-black BSTs

B-trees

Symbol table review

Challenge. Guarantee performance.

This lecture. 2-3 trees, left-leaning red-black BSTs, B-trees.

implementation

worst-case cost

(after N inserts)

worst-case cost

(after N inserts)

worst-case cost

(after N inserts)

average case

(after N random inserts)

average case

(after N random inserts)

average case

(after N random inserts)

ordered key

implementation

search insert delete search hit insert delete

iteration? interface

sequential search

(unordered list)

N N N N/2 N N/2 no equals()

binary search

(ordered array)

lg N N N lg N N/2 N/2 yes compareTo()

BST N N N 1.39 lg N 1.39 lg N? yes compareTo()

goal log N log N log N log N log N log N yes compareTo()

Allow 1 or 2 keys per node.

2-node: one key, two children.

3-node: two keys, three children.

Symmetric order. Inorder traversal yields keys in ascending order.

Perfect balance. Every path from root to null link has same length.

2-3 tree

between E and J

larger than J

smaller than E

A C H P S X

R

M

L

E

J

3-node

2-node

null link

Search.

Compare search key against keys in node.

Find interval containing search key.

Follow associated link (recursively).

2-3 tree demo

search for H

A C H P S X

R

M

L

E

J

A C S X

2-3 tree construction demo

2-3 tree

H P

E R

L

Insertion into a 2-node at bottom.

Add new key to 2-node to create a 3-node.

Insertion into a 2-3 tree

A C H P S X

E R

L

insert G

A C P S X

E R

L

G H

Splitting a 4-node is a local transformation: constant number of operations.

b c d

a e

between

a and b

less

than a

between

b and c

between

d and e

greater

than e

between

c and d

between

a and b

less

than a

between

b and c

between

d and e

greater

than e

between

c and d

b d

a c e

Splitting a 4-node is a local transformation that preserves balance

Local transformations in a 2-3 tree

Invariants. Maintains symmetric order and perfect balance.

Pf. Each transformation maintains symmetric order and perfect balance.

Global properties in a 2-3 tree

b

right

middle

left

right

left

b d

b c d

a c

a

a b c

d

a c

b d

a b c

a c

root

parent is a 2-node

parent is a 3-node

Splitting a temporary 4-node in a 2-3 tree (summary)

c e

b d

c d e

a b

b c d

a e

a b d

a c e

a b c

d e

a c

b d e

b

right

middle

left

right

left

b d

b c d

a c

a

a b c

d

a c

b d

a b c

a c

root

parent is a 2-node

parent is a 3-node

Splitting a temporary 4-node in a 2-3 tree (summary)

c e

b d

c d e

a b

b c d

a e

a b d

a c e

a b c

d e

a c

b d e

b

right

middle

left

right

left

b d

b c d

a c

a

a b c

d

a c

b d

a b c

a c

root

parent is a 2-node

parent is a 3-node

Splitting a temporary 4-node in a 2-3 tree (summary)

c e

b d

c d e

a b

b c d

a e

a b d

a c e

a b c

d e

a c

b d e

2-3 tree: performance

Perfect balance. Every path from root to null link has same length.

Tree height.

Worst case: lg N. [all 2-nodes]

Best case: log 3

N ≈ .631 lg N. [all 3-nodes]

Between 12 and 20 for a million nodes.

Between 18 and 30 for a billion nodes.

Guaranteed logarithmic performance for search and insert.

Typical 2-3 tree built from random keys

ST implementations: summary

constants depend upon implementation

implementation

worst-case cost

(after N inserts)

worst-case cost

(after N inserts)

worst-case cost

(after N inserts)

average case

(after N random inserts)

average case

(after N random inserts)

average case

(after N random inserts)

ordered

iteration?

key

interface

implementation

search insert delete search hit insert delete

iteration? interface

sequential search

(unordered list)

N N N N/2 N N/2 no equals()

binary search

(ordered array)

lg N N N lg N N/2 N/2 yes compareTo()

BST N N N 1.39 lg N 1.39 lg N? yes compareTo()

2-3 tree c lg N c lg N c lg N c lg N c lg N c lg N yes compareTo()

h t t p : / / a l g s 4. c s. p r i n c e t o n. e d u

ROBERT SEDGEWICK | KEVIN WAYNE

Algorithms

‣

2-3 search trees

‣

red-black BSTs

‣

B-trees

3.3 BALANCED SEARCH TREES

  1. Represent 2–3 tree as a BST.
  2. Use "internal" left-leaning links as "glue" for 3–nodes.

17 Left-leaning red-black BSTs (Guibas-Sedgewick 1979 and Sedgewick 2007) larger key is root

Encoding a 3-node with two 2-nodes

connected by a left-leaning red link a b

3-node

between

a and b less than a greater than b a b between a and b less than a greater than b

Encoding a 3-node with two 2-nodes connected by a left-leaning red link a b 3-node between a and b less than a greater than b a b between a and b less than a greater than b

X

H S P J R E A M C L J E R M

red−black tree horizontal red links

1 correspondence between red-black and 2-3 trees

X S H P J R E A M C L S X H P J E R A M C L

black tree ontal red links ree

E J

H L M R P S X A C

black links connect 2-nodes and 3-nodes red links "glue" nodes within a 3-node 2-3 tree corresponding red-black BST

Key property. 1–1 correspondence between 2–3 and LLRB.

Left-leaning red-black BSTs: 1-1 correspondence with 2-3 trees

1 −1 correspondence between red-black and 2-3 trees

X

H S

P

J

R

E

A

M

C

L

S X

H

P

J

E R

A

M

C

L

red−black tree

horizontal red links

2-3 tree

E J

H L

M

R

P S X

A C

Search implementation for red-black BSTs

Observation. Search is the same as for elementary BST (ignore color).

Remark. Most other ops (e.g., floor, iteration, selection) are also identical.

20

public Val get(Key key)

Node x = root;

while (x != null)

int cmp = key.compareTo(x.key);

if (cmp < 0) x = x.left;

else if (cmp > 0) x = x.right;

else if (cmp == 0) return x.val;

return null;

but runs faster

because of better balance

X

H S

P

J

R

E

A

M

C

L

S X

H

P

J

E R

A

M

C

L

red−black tree

horizontal red links

2-3 tree

E J

H L

M

R

P S X

A C