Professional Documents
Culture Documents
‣ 2-3-4 trees
‣ red-black trees
‣ B-trees
References:
Algorithms in Java, Chapter 13 Except as otherwise noted, the content of this presentation
http://www.cs.princeton.edu/algs4 is licensed under the Creative Commons Attribution 2.5 License.
Algorithms in Java, 4th Edition · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · May 2, 2008 10:49:44 AM
Symbol table review
Randomized BST.
• Probabilistic guarantee of ~ c lg N time per operation.
• Need subtree count in each node.
• Need random numbers for each insertion and deletion.
2
‣ 2-3-4 trees
‣ red-black trees
‣ B-trees
3
2-3-4 tree
Maintain 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
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 5
Insertion 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
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 6
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 7
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 8
Insertion in a 2-3-4 tree
Insert.
• Search to bottom for key.
• 2-node at bottom: convert to 3-node.
Ex. Insert X.
K R
larger than R
larger
than W
C E M O W
A D F G J L N Q S V Y Z
X not found 9
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
larger than R
larger
than W
C E M O W
A D F G J L N Q S V X Y Z
X fits here 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 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 11
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: no room for new 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
no room for H 12
Splitting a 4-node in a 2-3-4 tree
A D F J
A D F G J
C E G
A D F H J
a ac
bcd b d
ab abd
cde
c e
Consequences.
• 4-node below a 4-node case never happens.
• Insertion at bottom node is easy since it's not a 4-node.
14
Splitting a 4-node below a 2-node in a 2-3-4 tree
D D Q
K Q W K W
A-C A-C
15
Splitting a 4-node below a 3-node in a 2-3-4 tree
DH D H Q
K Q W K W
A-C E-G A-C E-G
16
Growth of a 2-3-4 tree
insert A insert C
A E
A C R S
insert S
A S
insert D
insert E
E
A E S
A C D R S
insert R
split 4-node to
E insert I
E
A S
E
tree grows A R S and then insert
up one level
A C D I R S
17
Growth of a 2-3-4 tree (cont)
split 4-node to
E R
E insert N
I S
A C D I R S and then insert
E R
A C D I N S
insert X split 4-node to
insert B C E R E
C R
A B D I N S and then insert
split 4-node to
C E R
A D E
and then insert C R
tree grows
up one level
A B D I N S X
18
Balance in a 2-3-4 tree
Key property. All paths from root to leaf have same length.
Tree height.
• Worst case:
• Best case:
19
Balance in a 2-3-4 tree
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
22
‣ 2-3-4 trees
‣ red-black trees
‣ B-trees
23
Left-leaning red-black trees (Guibas-Sedgewick, 1979 and Sedgewick, 2007)
CE E
C J
AB D FGJ B D G
4-node
24
Left-leaning red-black trees (Guibas-Sedgewick, 1979 and Sedgewick, 2007)
25
Search implementation for red-black trees
Remark. Many other ops (e.g., iteration, kth largest, ceiling) are also the same.
26
Insertion in a LLRB tree: overview
c
b
ab [stay tuned] b abc
a
a
• Splitting a 4-node.
a c
a ac
d a d
[stay tuned]
bcd c b d
b
b
h
u h
v
h = rotL(u)
v
u
A h = rotR(v) C
B C A B
28
Insertion in a LLRB tree: adding a node to the bottom
b
b b ab
a
OK
rotate
a a a left b
ab
b a
Insertion in a LLRB tree: adding a node to the bottom
c
c
bc b abc
b
a
OK
c c
c
ac a b abc
a
b a
rotate
left
rotate c
b b left
ab b abc
a a c
a
Insertion in a LLRB tree: adding a node to the bottom
rotate
left
or
OK
rotate
or or left
OK rotate
left
Insertion in a LLRB tree: splitting 4-nodes
d
d d d
c bd
abc b b
b a
a a c
c c
rotate flip to
a
right black
rotate
a
a a left c
d ac
c a d
bcd
c b d
b d b
flip to rotate
black b
right
32
Insertion in a LLRB tree: splitting 4-nodes
e e
e
a rotate
ae a left c ace
d
c a d
bcd b d
c
b d b
b rotate
flip to
black right
rotate
b left d
b
ab abd
a e e
a d b
cde
d a c c e
c e
c rotate
flip to
black right
33
Insertion in a LLRB tree: splitting 4-nodes
if necessary,
rotate left d
red or b b
black e d b e
d c e c
flip to rotate
black c right
34
Insertion in a LLRB: strategy revisited
Search as usual.
•
a
If key not found, insert a new node at the bottom. a
•
b
Might leave right-leaning link.
35
Red-black tree implementation: Java skeleton
36
Red-black tree implementation: basic operations
1. Split a 4-node.
could be
right or left
private Node splitFourNode(Node h)
{
x = rotR(h); c right fix color of
x.left.color = BLACK; rotate
b left node
b
return x; b
} a c c a
a
could be
private Node leanLeft(Node h) red or black
{
x = rotL(h); left flip
x.color = x.left.color; rotate b colors
a b
x.left.color = RED;
return x; b a a
}
37
Insertion in a LLRB tree: Java implementation
return h;
}
38
Insertion in a LLRB tree: visualization
39
Insertion in a LLRB tree: visualization
40
Insertion in a LLRB tree: visualization
50 random insertions
41
Insertion in a LLRB tree: visualization
42
Typical random LLRB trees
43
Balance in left-leaning red-black trees
Proposition A. Every path from root to leaf has same number of black links.
Proposition B. Never three red links in-a-row.
Proposition C. Height of tree is less than 3 lg N + 2 in the worst case.
44
Why left-leaning trees?
old code (that students had to learn in the past) new code (that you have to learn)
private Node insert(Node x, Key key, Value val, boolean sw) private Node insert(Node h, Key key, Value val)
{ {
if (x == null) int cmp = key.compareTo(h.key);
return new Node(key, value, RED); if (h == null)
int cmp = key.compareTo(x.key); return new Node(key, val, RED);
if (isRed(h.left) && isRed(h.left.left))
if (isRed(x.left) && isRed(x.right)) {
{ h = rotR(h);
x.color = RED; h.left.color = BLACK;
x.left.color = BLACK; }
x.right.color = BLACK; if (cmp < 0)
} h.left = insert(h.left, key, val);
if (cmp < 0) else if (cmp > 0)
{ h.right = insert(h.right, key, val);
x.left = insert(x.left, key, val, false); else
if (isRed(x) && isRed(x.left) && sw) x.val = val;
x = rotR(x); if (isRed(h.right))
if (isRed(x.left) && isRed(x.left.left)) {
{ h = rotL(h);
x = rotR(x); h.color = h.left.color;
x.color = BLACK; x.right.color = RED; h.left.color = RED;
} }
} return h;
else if (cmp > 0) }
{
x.right = insert(x.right, key, val, true);
if (isRed(h) && isRed(x.right) && !sw)
x = rotL(x); straightforward
if (isRed(h.right) && isRed(h.right.right)) (if you’ve paid attention)
{
x = rotL(x);
x.color = BLACK; x.left.color = RED;
}
}
else x.val = val;
extremely tricky
return x;
}
45
Why left-leaning trees?
Simplified code.
• Left-leaning restriction reduces number of cases.
• Recursion gives two (easy) chances to fix each node. new
• Arbitrary delete.
• Red-black trees.
48
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.
3 or 4 in practice (!)
49
B-Tree Example
no room
for 773
no room
for 275
no room
for 737
M=5
50
B-Tree Example (cont)
no room
for 526
M=5
51
Balanced trees in the wild
B-trees (and variants) are widely used for file systems and databases.
• Windows: HPFS.
• Mac: HFS, HFS+.
• Linux: ReiserFS, XFS, Ext3FS, JFS.
• Databases: ORACLE, DB2, INGRES, SQL, PostgreSQL.
52
Red-black trees in the wild
53
Red-black trees in the wild
54