













































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Guarantee performance. This lecture. 2-3 trees, left-leaning red-black BSTs, B-trees. implementation worst ...
Typology: Study notes
1 / 53
This page cannot be seen from the preview
Don't miss anything!














































ROBERT SEDGEWICK | KEVIN WAYNE
F O U R T H E D I T I O N
h t t p : / / a l g s 4. c s. p r i n c e t o n. e d u
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.
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
17 Left-leaning red-black BSTs (Guibas-Sedgewick 1979 and Sedgewick 2007) larger key is root
connected by a left-leaning red link a b
3-node
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
H S P J R E A M C L J E R M
red−black tree horizontal red links
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
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
red−black tree
horizontal red links
2-3 tree
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
red−black tree
horizontal red links
2-3 tree