Beruflich Dokumente
Kultur Dokumente
2-3-4 trees
red-black trees
B-trees
References:
Algorithms in Java, Chapter 13
http://www.cs.princeton.edu/introalgsds/44balanced
1
Symbol Table Review
Randomized BST.
• Guarantee of ~c lg N time per operation (probabilistic).
• Need subtree count in each node.
• Need random numbers for each insert/delete op.
2
Summary of symbol-table implementations
probabilistic, with
exponentially small
chance of quadratic time
N = 250
lg N 8
1.39 lg N 11
4
2-3-4 trees
red-black trees
B-trees
5
2-3-4 Tree
2-3-4 tree. Generalize node to allow multiple keys; keep tree balanced.
Perfect balance. Every path from root to leaf has same length.
K R
smaller than K larger than R
between
K and R
C E M O W
A D F G J L N Q S V Y Z
6
Searching in a 2-3-4 Tree
Search.
• Compare search key against keys in node.
• Find interval containing search key.
• Follow associated link (recursively).
K R
between
K and R
C E M O W
smaller than M
A D F G J L N Q S V Y Z
found L
7
Insertion in a 2-3-4 Tree
Insert.
• Search to bottom for key.
Ex. Insert B
K R
smaller than K
smaller than C C E M O W
A D F G J L N Q S V Y Z
B not found
8
Insertion in a 2-3-4 Tree
Insert.
• Search to bottom for key.
• 2-node at bottom: convert to 3-node.
Ex. Insert B
K R
smaller than K
smaller than C C E M O W
A B D F G J L N Q S V Y Z
B fits here 9
Insertion in a 2-3-4 Tree
Insert.
• Search to bottom for key.
Ex. Insert X
K R
larger than R
C E M O W
larger than W
A D F G J L N Q S V Y Z
X not found
10
Insertion in a 2-3-4 Tree
Insert.
• Search to bottom for key.
• 2-node at bottom: convert to 3-node.
• 3-node at bottom: convert to 4-node.
Ex. Insert X
K R
C E M O W
A B D F G J L N Q S V X Y Z
X fits here 11
Insertion in a 2-3-4 Tree
Insert.
• Search to bottom for key.
Ex. Insert H
K R
smaller than K
C E M O W
larger than E
A D F G J L N Q S V Y Z
H not found
12
Insertion in a 2-3-4 Tree
Insert.
• Search to bottom for key.
• 2-node at bottom: convert to 3-node.
• 3-node at bottom: convert to 4-node.
• 4-node at bottom: ??
Ex. Insert H
K R
C E M O W
A B D F G J L N Q S V X Y Z
A B D F J
A B D F G J
C E G
A B D F H J
local transformations
that work anywhere
in the tree
Consequences
•4-node below a 4-node case never happens
•insertion at bottom node is easy since it's not a 4-node.
15
Splitting a 4-node below a 2-node in a 2-3-4 tree
D DQ
KQW K W
A-C A-C
16
Splitting a 4-node below a 3-node in a 2-3-4 tree
DH DHQ
KQW K W
A-C E-G A-C E-G
17
Growth of a 2-3-4 tree
A A C R S
insert S
insert H
A S E
insert E A C H R S
A E S
insert I
insert R split 4-node to
E
E
E
A S
A C H I R S
tree grows A R S and then insert
up one level
18
Growth of a 2-3-4 tree (continued)
split 4-node to
E E R
I S
A C H I R S and then insert
E R
A C H I N S
split 4-node to
E C R C
E R
A G H I N S
split 4-node to and then insert
E C R
A H
C
and then insert E R
tree grows
up one level
A G H I N S X
19
Balance in 2-3-4 trees
Key property: All paths from root to leaf have same length.
Tree height.
• Worst case: lg N [all 2-nodes]
• Best case: log4 N = 1/2 lg N [all 4-nodes]
• Between 10 and 20 for a million nodes.
• Between 15 and 30 for a billion nodes.
20
2-3-4 Tree: Implementation?
fantasy code
Bottom line: could do it, but stay tuned for an easier way. 21
Summary of symbol-table implementations
22
2-3-4 trees
red-black trees
B-trees
23
Left-leaning red-black trees (Guibas-Sedgewick, 1979 and Sedgewick, 2007)
internal “glue”
Key Properties
• elementary BST search works
• 1-1 correspondence between 2-3-4 and left-leaning red-black trees
C E E
C J
A B D F G J B D G
A F
24
Left-leaning red-black trees
Disallowed:
25
Search implementation for red-black trees
27
Insert implementation for left-leaning red-black trees (strategy)
[stay tuned]
[stay tuned]
[stay tuned]
[stay tuned]
28
Inserting a new node at the bottom in a LLRB tree
1. Add new node as usual, with red link to glue it to node above
rotate
left
or
OK
rotate
left
or or
OK rotate
left
29
Splitting a 4-node below a 2-node in a left-leaning red-black tree
d d b e
c c e c
also make
this black
Q
D
W D
W
Q
K
K
A-C A-C
also make
H this black
Q
D W H
D
Q
K W
K
A-C E-G A-C E-G
left
b
right rotate
e rotate b (if necessary) d
d d b e
c also make c e c
this black
right left
b
rotate b rotate
a e (if necessary) d
a d
d b e
c e
c also make a c
this black
32
Insert implementation for left-leaning red-black trees (strategy revisited)
Search as usual
•
or
if key found reset value, as usual
• if key not found insert a new red node at the bottom
[might be right-leaning red link]
Split a 4-node
right fix
rotate color
34
Insert implementation for left-leaning red-black trees (code for basic operations)
Split a 4-node
could be
private Node splitFourNode(Node h) h
left or right x
{ right fix color of
x = rotR(h); rotate left node
x.left.color = BLACK;
return x;
}
middle node
is red
35
Insert implementation for left-leaning red-black trees (code)
if (isRed(h.left))
if (isRed(h.left.left)) split 4-nodes on the way down
h = splitFourNode(h);
36
Balance in left-leaning red-black trees
Proposition A. Every path from root to leaf has same number of black links.
Property E. Nearly all 4-nodes are on the bottom in the typical applications.
37
Why left-leaning trees?
}
return x;
extremely tricky
38
Why left-leaning trees?
Simplified code
• left-leaning restriction reduces number of cases
• recursion gives two (easy) chances to fix each node
•
new
short inner loop
old
N = 500
lg N 9
41
2-3-4 trees
red-black trees
B-trees
42
B-trees (Bayer-McCreight, 1972)
Space-time tradeoff.
• M large only a few levels in tree.
• M small less wasted space.
• Typical M = 1000, N < 1 trillion.
in practice: 3 or 4 (!)
43
B-Tree Example
no room
for 275
no room
for 737
M=5
44
B-Tree Example (cont)
no room
for 526
45
Summary of symbol-table implementations
B-tree 1 1 1 1 1 1 yes
48
Red-black trees in the wild
!!
red-black tree
49
Red-black trees in the wild
!!
red-black tree
50