Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into...
Transcript of Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into...
![Page 1: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/1.jpg)
ROBERT SEDGEWICK | KEVIN WAYNE
F O U R T H E D I T I O N
Algorithms
Algorithms ROBERT SEDGEWICK | KEVIN WAYNE
Last updated on 10/6/20 9:52 AM
3.3 BALANCED SEARCH TREES
‣ 2–3 search trees
‣ red–black BSTs (representation)
‣ red–black BSTs (insertion)
‣ context
https://algs4.cs.princeton.edu
![Page 2: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/2.jpg)
Symbol table review
Challenge. Θ(log n) time in worst case.
This lecture. 2–3 trees and left-leaning red–black BSTs.
2
implementation
guarantee average caseordered
ops?key
interfacesearch insert delete search insert delete
sequential search(unordered list)
n n n n n n equals()
binary search(sorted array)
log n n n log n n n ✔ compareTo()
BST n n n log n log n √ n ✔ compareTo()
goal log n log n log n log n log n log n ✔ compareTo()
optimized for teaching and coding; introduced to the world in COS 226!
co-invented by Bob Sedgewick
![Page 3: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/3.jpg)
3.3 BALANCED SEARCH TREES
‣ 2–3 search trees
‣ red–black BSTs (representation)
‣ red–black BSTs (insertion)
‣ contextROBERT SEDGEWICK | KEVIN WAYNE
Algorithms
https://algs4.cs.princeton.edu
![Page 4: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/4.jpg)
2–3 tree
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.
4
between E and J
larger than Jsmaller than E
S XA C PH
R
M
L
E J
3-node 2-node
null link
how to maintain?
![Page 5: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/5.jpg)
2–3 tree demo
Search.
独Compare search key against key(s) in node.
独Find interval containing search key.
独Follow associated link (recursively).
5
search for H
S XA C PH
R
M
L
E J
![Page 6: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/6.jpg)
2–3 tree: insertion
Insertion into a 2-node at bottom.
独Add new key to 2-node to create a 3-node.
6
S XA C PH
E R
L
insert G
S XA C P
E R
L
G H
![Page 7: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/7.jpg)
2–3 tree: insertion
Insertion into a 3-node at bottom.
独Add new key to 3-node to create temporary 4-node.
独Move middle key in 4-node into parent.
独Repeat up the tree, as necessary.
独If you reach the root and it’s a 4-node, split it into three 2-nodes.
7
S XA C PH
E R
L
insert Z
R X
A C P
E
L
H S Z
![Page 8: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/8.jpg)
2–3 tree construction demo
8
![Page 9: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/9.jpg)
What is the maximum height of a 2–3 tree with n keys?
A. ~ log3 n
B. ~ log2 n
C. ~ 2 log2 n
D. ~ n
height ~ log3 n
Balanced search trees: quiz 2
9
height = log2 (n + 1)
all 3-nodes (min height)
all 2-nodes (max height)
![Page 10: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/10.jpg)
2–3 tree: performance
Perfect balance. Every path from root to null link has same length.
Tree height.
独Min: log3 n ≈ 0.631 log2 n. [all 3-nodes]
独Max: log2 n. [all 2-nodes]
独Between 12 and 20 for a million nodes.
独Between 18 and 30 for a billion nodes.
Bottom line. Search and insert take Θ(log n) time in worst case
10
Typical 2-3 tree built from random keys
![Page 11: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/11.jpg)
ST implementations: summary
11
implementation
guarantee average caseordered
ops?key
interfacesearch insert delete search insert delete
sequential search(unordered list)
n n n n n n equals()
binary search(sorted array)
log n n n log n n n ✔ compareTo()
BST n n n log n log n √ n ✔ compareTo()
2–3 tree log n log n log n log n log n log n ✔ compareTo()
but hidden constant c is large (depends upon implementation)
![Page 12: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/12.jpg)
“ Beautiful algorithms are not always the most useful. ”
— Donald Knuth
2–3 tree: implementation?
Direct implementation is complicated, because:
独Maintaining multiple node types is cumbersome.
独Need multiple compares to move down tree.
独Need to move back up the tree to split 4-nodes.
独Large number of cases for splitting.
Bottom line. Could do it, but there’s a better way.
public void put(Key key, Value val) { Node x = root; while (x.getTheCorrectChild(key) != null) { x = x.getTheCorrectChildKey(); if (x.is4Node()) x.split(); } if (x.is2Node()) x.make3Node(key, val); else if (x.is3Node()) x.make4Node(key, val); }
fantasy code
12
![Page 13: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/13.jpg)
3.3 BALANCED SEARCH TREES
‣ 2–3 search trees
‣ red–black BSTs (representation)
‣ red–black BSTs (insertion)
‣ contextROBERT SEDGEWICK | KEVIN WAYNE
Algorithms
https://algs4.cs.princeton.edu
![Page 14: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/14.jpg)
How to implement 2–3 trees with binary trees?
Challenge. How to represent a 3 node?
Approach 1. Regular BST.
独No way to tell a 3-node from two 2-nodes.
独Can’t (uniquely) map from BST back to 2–3 tree.
Approach 2. Regular BST with red “glue” nodes.
独Wastes space for extra node.
独Messy code.
Approach 3. Regular BST with red “glue” links.
独Widely used in practice.
独Arbitrary restriction: red links lean left.
14
E R
E
R
E
R
E R
![Page 15: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/15.jpg)
Left-leaning red–black BSTs (Guibas–Sedgewick 1979 and Sedgewick 2007)
1. Represent 2–3 tree as a BST.
2. Use “internal” left-leaning links as “glue” for 3–nodes.
15
larger key is root
Encoding a 3-node with two 2-nodes connected by a left-leaning red link
a b3-node
betweena and b
lessthan a
greaterthan b
a
b
betweena and b
lessthan a
greaterthan b
Encoding a 3-node with two 2-nodes connected by a left-leaning red link
a b3-node
betweena and b
lessthan a
greaterthan b
a
b
betweena and b
lessthan a
greaterthan b
1!1 correspondence between red-black and 2-3 trees
XSH
P
J R
E
A
M
CL
XSH P
J REA
M
C L
red!black tree
horizontal red links
2-3 tree
E J
H L
M
R
P S XA C
1!1 correspondence between red-black and 2-3 trees
XSH
P
J R
E
A
M
CL
XSH P
J REA
M
C L
red!black tree
horizontal red links
2-3 tree
E J
H L
M
R
P S XA C
black links connect 2-nodes and 3-nodes
red links “glue” nodes within a 3-node
2–3 tree corresponding red–black BST
![Page 16: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/16.jpg)
Left-leaning red–black BSTs: 1–1 correspondence with 2–3 trees
Key property. 1–1 correspondence between 2–3 trees and LLRB trees.
16
XSH
P
J R
E
A
M
CL
red!black tree
1!1 correspondence between red-black and 2-3 trees
XSH
P
J R
E
A
M
CL
XSH P
J REA
M
C L
red!black tree
horizontal red links
2-3 tree
E J
H L
M
R
P S XA C
1!1 correspondence between red-black and 2-3 trees
XSH
P
J R
E
A
M
CL
XSH P
J REA
M
C L
red!black tree
horizontal red links
2-3 tree
E J
H L
M
R
P S XA C
![Page 17: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/17.jpg)
An equivalent definition of LLRB trees (without reference to 2–3 trees)
A BST such that:
独No node has two red links connected to it.
独Red links lean left.
独Every path from root to null link has the same number of black links.
17
“perfect black balance”
symmetric order
color invariants
XSH
P
J R
E
A
M
CL
red!black tree
![Page 18: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/18.jpg)
Balanced search trees: quiz 3
Which LLRB tree corresponds to the following 2–3 tree?
C. Both A and B.
D. Neither A nor B.18
J
Z
A
E
H L
B.A.
E J
L ZG H
G
A
L
Z
E
G
J
A H
![Page 19: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/19.jpg)
Search implementation for red–black BSTs
Observation. Search is the same as for BST (ignore color).
Remark. Many other ops (iteration, floor, rank, selection) are also identical.
19
public Value 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 return x.val; } return null; }
but runs faster (because of better balance)
1!1 correspondence between red-black and 2-3 trees
XSH
P
J R
E
A
M
CL
XSH P
J REA
M
C L
red!black tree
horizontal red links
2-3 tree
E J
H L
M
R
P S XA C
![Page 20: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/20.jpg)
Red–black BST representation
Each node is pointed to by precisely one link (from its parent) ⇒
can encode color of links in nodes.
20
private static final boolean RED = true;private static final boolean BLACK = false;
private class Node{ Key key; // key Value val; // associated data Node left, right; // subtrees int N; // # nodes in this subtree boolean color; // color of link from // parent to this node
Node(Key key, Value val) { this.key = key; this.val = val; this.N = 1; this.color = RED; }}
private boolean isRed(Node x){ if (x == null) return false; return x.color == RED;}
JG
E
A DC
Node representation for red!black trees
hh.left.color
is REDh.right.color
is BLACK private class Node { Key key; Value val; Node left, right; }
private boolean isRed(Node x) { if (x == null) return false; return x.color == RED; } null links are black
private static final boolean RED = true; private static final boolean BLACK = false;
boolean color; // color of parent link
![Page 21: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/21.jpg)
1!1 correspondence between red-black and 2-3 trees
XSH
P
J R
E
A
M
CL
XSH P
J REA
M
C L
red!black tree
horizontal red links
2-3 tree
E J
H L
M
R
P S XA C
how we draw LLRB trees(color in links)
Review: the road to LLRB trees
21
A
H
SR
X
CE
XS
RC
E
H
A
AC
E
HR
SX
BST possibilities
best case
typical case
worst case
BSTs(can get imbalanced)
XSH
P
J R
E
A
M
CL
XSH P
J REA
M
C L
E J
H L
M
R
P S XA C
how we implement LLRB trees(color in nodes)
3-nodes “glued” together with red links
XSH
P
J R
E
A
M
CL
XSH P
J REA
M
C L
E J
H L
M
R
P S XA C
2–3 trees(balanced but awkward to implement)
XSH
P
J R
E
A
M
CL
XSH P
J REA
M
C L
E J
H L
M
R
P S XA C
![Page 22: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/22.jpg)
3.3 BALANCED SEARCH TREES
‣ 2–3 search trees
‣ red–black BSTs (representation)
‣ red–black BSTs (insertion)
‣ contextROBERT SEDGEWICK | KEVIN WAYNE
Algorithms
https://algs4.cs.princeton.edu
![Page 23: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/23.jpg)
Insertion into a LLRB tree: overview
Basic strategy. Maintain 1–1 correspondence with 2–3 trees.
During internal operations, maintain:
独Symmetric order.
独Perfect black balance.
独[ but not necessarily color invariants ]
Example violations of color invariants:
To restore color invariants: perform rotations and color flips.
23
A
E
S
right-leaningred link
A
E
S
two red children(a temporary 4-node)
A
E
S
left-left red(a temporary 4-node)
E
A
S
left-right red(a temporary 4-node)
![Page 24: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/24.jpg)
Elementary red–black BST operations
Left rotation. Orient a (temporarily) right-leaning red link to lean left.
Invariants. Maintains symmetric order and perfect black balance.
24
greater than S
x
h
S
between E and S
less than E
E
rotate E left(before)
private Node rotateLeft(Node h) { assert isRed(h.right); Node x = h.right; h.right = x.left; x.left = h; x.color = h.color; h.color = RED; return x; }
![Page 25: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/25.jpg)
Elementary red–black BST operations
Left rotation. Orient a (temporarily) right-leaning red link to lean left.
Invariants. Maintains symmetric order and perfect black balance.
25
greater than S
less than E
x
h E
between E and S
S
rotate E left(after)
private Node rotateLeft(Node h) { assert isRed(h.right); Node x = h.right; h.right = x.left; x.left = h; x.color = h.color; h.color = RED; return x; }
![Page 26: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/26.jpg)
Elementary red–black BST operations
Right rotation. Orient a left-leaning red link to (temporarily) lean right.
Invariants. Maintains symmetric order and perfect black balance.
26
rotate S right(before)
greater than S
less than E
h
x E
between E and S
S
private Node rotateRight(Node h) { assert isRed(h.left); Node x = h.left; h.left = x.right; x.right = h; x.color = h.color; h.color = RED; return x; }
![Page 27: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/27.jpg)
Elementary red–black BST operations
Right rotation. Orient a left-leaning red link to (temporarily) lean right.
Invariants. Maintains symmetric order and perfect black balance.
27
private Node rotateRight(Node h) { assert isRed(h.left); Node x = h.left; h.left = x.right; x.right = h; x.color = h.color; h.color = RED; return x; }
rotate S right(after)
greater than S
h
x
S
between E and S
less than E
E
![Page 28: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/28.jpg)
Elementary red–black BST operations
Color flip. Recolor to split a (temporary) 4-node.
Invariants. Maintains symmetric order and perfect black balance.
28
greater than S
between E and S
between A and E
less than A
h
SA
private void flipColors(Node h) { assert !isRed(h); assert isRed(h.left); assert isRed(h.right); h.color = RED; h.left.color = BLACK; h.right.color = BLACK; }
flip colors(before)
E
![Page 29: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/29.jpg)
Elementary red–black BST operations
Color flip. Recolor to split a (temporary) 4-node.
Invariants. Maintains symmetric order and perfect black balance.
29
greater than S
between E and S
between A and E
less than A
h
SA
private void flipColors(Node h) { assert !isRed(h); assert isRed(h.left); assert isRed(h.right); h.color = RED; h.left.color = BLACK; h.right.color = BLACK; }
flip colors(after)
E
![Page 30: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/30.jpg)
Balanced search trees: quiz 4
Which sequence of elementary operations transforms the red–black BST at left to the one at right?
A. Color flip R; left rotate E.
B. Color flip R; right rotate E.
C. Color flip E; left rotate R.
D. Color flip R; left rotate R.30
E
C
A H
R
S
R
SE
C
A
H
![Page 31: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/31.jpg)
Insertion into a LLRB tree
独Do standard BST insert.
独Color new link red.
独Repeat up the tree until color invariants restored:
– two left red links in a row? ⇒ rotate right
– left and right links both red? ⇒ color flip
– only right link red? ⇒ rotate left
34
H
E
RS
AC
S
S
R
EH
add newnode here
E
RS
AC
right link redso rotate left
two lefts in a rowso rotate right
E
HR
AC
both children redso flip colors
S
E
HR
AC
AC
inserting H
Insert into a 3-nodeat the bottom
H
E
RS
AC
S
S
R
EH
add newnode here
E
RS
AC
right link redso rotate left
two lefts in a rowso rotate right
E
HR
AC
both children redso flip colors
S
E
HR
AC
AC
inserting H
Insert into a 3-nodeat the bottom
H
E
RS
AC
S
S
R
EH
add newnode here
E
RS
AC
right link redso rotate left
two lefts in a rowso rotate right
E
HR
AC
both children redso flip colors
S
E
HR
AC
AC
inserting H
Insert into a 3-nodeat the bottom
H
E
RS
AC
S
S
R
EH
add newnode here
E
RS
AC
right link redso rotate left
two lefts in a rowso rotate right
E
HR
AC
both children redso flip colors
S
E
HR
AC
AC
inserting H
Insert into a 3-nodeat the bottom
H
E
RS
AC
S
S
R
EH
add newnode here
E
RS
AC
right link redso rotate left
two lefts in a rowso rotate right
E
HR
AC
both children redso flip colors
S
E
HR
AC
AC
inserting H
Insert into a 3-nodeat the bottom
to preserve symmetric order
to preserve perfect black balance
H
E
RS
AC
S
S
R
EH
add newnode here
E
RS
AC
right link redso rotate left
two lefts in a rowso rotate right
E
HR
AC
both children redso flip colors
S
E
HR
AC
AC
inserting H
Insert into a 3-nodeat the bottom
H
E
RS
AC
S
S
R
EH
add newnode here
E
RS
AC
right link redso rotate left
two lefts in a rowso rotate right
E
HR
AC
both children redso flip colors
S
E
HR
AC
AC
inserting H
Insert into a 3-nodeat the bottom
H
E
RS
AC
S
S
R
EH
add newnode here
E
RS
AC
right link redso rotate left
two lefts in a rowso rotate right
E
HR
AC
both children redso flip colors
S
E
HR
AC
AC
inserting H
Insert into a 3-nodeat the bottom
H
E
RS
AC
S
S
R
EH
add newnode here
E
RS
AC
right link redso rotate left
two lefts in a rowso rotate right
E
HR
AC
both children redso flip colors
S
E
HR
AC
AC
inserting H
Insert into a 3-nodeat the bottom
![Page 32: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/32.jpg)
Insertion into a LLRB tree
独Do standard BST insert.
独Color new link red.
独Repeat up the tree until color invariants restored:
– two left red links in a row? ⇒ rotate right
– left and right links both red? ⇒ color flip
– only right link red? ⇒ rotate left
35
P
S
R
E
add newnode here
right link redso rotate left
both childrenred so
flip colors
AC
HM
inserting P
S
R
E
AC
HM
P
S
R
E
AC
HM
PS
R
E
AC H
M
Passing a red link up the tree
two lefts in a rowso rotate right
P S
RE
AC H
M
both children redso flip colors
P S
RE
AC H
M
P
S
R
E
add newnode here
right link redso rotate left
both childrenred so
flip colors
AC
HM
inserting P
S
R
E
AC
HM
P
S
R
E
AC
HM
PS
R
E
AC H
M
Passing a red link up the tree
two lefts in a rowso rotate right
P S
RE
AC H
M
both children redso flip colors
P S
RE
AC H
M
P
S
R
E
add newnode here
right link redso rotate left
both childrenred so
flip colors
AC
HM
inserting P
S
R
E
AC
HM
P
S
R
E
AC
HM
PS
R
E
AC H
M
Passing a red link up the tree
two lefts in a rowso rotate right
P S
RE
AC H
M
both children redso flip colors
P S
RE
AC H
M
P
S
R
E
add newnode here
right link redso rotate left
both childrenred so
flip colors
AC
HM
inserting P
S
R
E
AC
HM
P
S
R
E
AC
HM
PS
R
E
AC H
M
Passing a red link up the tree
two lefts in a rowso rotate right
P S
RE
AC H
M
both children redso flip colors
P S
RE
AC H
M
P
S
R
E
add newnode here
right link redso rotate left
both childrenred so
flip colors
AC
HM
inserting P
S
R
E
AC
HM
P
S
R
E
AC
HM
PS
R
E
AC H
M
Passing a red link up the tree
two lefts in a rowso rotate right
P S
RE
AC H
M
both children redso flip colors
P S
RE
AC H
M
P
S
R
E
add newnode here
right link redso rotate left
both childrenred so
flip colors
AC
HM
inserting P
S
R
E
AC
HM
P
S
R
E
AC
HM
PS
R
E
AC H
M
Passing a red link up the tree
two lefts in a rowso rotate right
P S
RE
AC H
M
both children redso flip colors
P S
RE
AC H
M
![Page 33: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/33.jpg)
Red–black BST construction demo
36
C
A
E
X
S
P
M
R
L
H
insert S E A R C H X M P L
![Page 34: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/34.jpg)
Insertion into a LLRB tree: Java implementation
独Do standard BST insert and color new link red.
独Repeat up the tree until color invariants restored:
– only right link red? ⇒ rotate left
– two left red links in a row? ⇒ rotate right
– left and right links both red? ⇒ color flip
37
private Node put(Node h, Key key, Value val) { if (h == null) return new Node(key, val, RED); int cmp = key.compareTo(h.key); if (cmp < 0) h.left = put(h.left, key, val); else if (cmp > 0) h.right = put(h.right, key, val); else h.val = val;
return h; }
if (isRed(h.right) && !isRed(h.left)) h = rotateLeft(h); if (isRed(h.left) && isRed(h.left.left)) h = rotateRight(h); if (isRed(h.left) && isRed(h.right)) flipColors(h);
insert at bottom (and color it red)
restore color invariants
only a few extra lines of code provides near-perfect balance
![Page 35: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/35.jpg)
Insertion into a LLRB tree: visualization
38
255 insertions in random order
![Page 36: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/36.jpg)
Insertion into a LLRB tree: visualization
39
255 insertions in ascending order
![Page 37: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/37.jpg)
Insertion into a LLRB tree: visualization
40
254 insertions in descending order
![Page 38: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/38.jpg)
Balance in LLRB trees
Proposition. Height of LLRB tree is ≤ 2 log2 n.
Pf.
独Black height = height of corresponding 2–3 tree ≤ log2 n.
独Never two red links in-a-row.
⇒ height of LLRB tree ≤ (2 ! black height) + 1
≤ 2 log2 n + 1.
独[ A slightly more refined arguments show height ≤ 2 log2 n. ]
41
height ≤ 2 log2 n
![Page 39: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/39.jpg)
ST implementations: summary
42
implementation
guarantee average caseordered
ops?key
interfacesearch insert delete search insert delete
sequential search(unordered list)
n n n n n n equals()
binary search(sorted array)
log n n n log n n n ✔ compareTo()
BST n n n log n log n √ n ✔ compareTo()
2–3 tree log n log n log n log n log n log n ✔ compareTo()
red–black BST log n log n log n log n log n log n ✔ compareTo()
hidden constant c is small (≤ 2 log2 n compares)
![Page 40: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/40.jpg)
3.3 BALANCED SEARCH TREES
‣ 2–3 search trees
‣ red–black BSTs (representation)
‣ red–black BSTs (insertion)
‣ contextROBERT SEDGEWICK | KEVIN WAYNE
Algorithms
https://algs4.cs.princeton.edu
![Page 41: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/41.jpg)
Balanced trees in the wild
Red–black BSTs are widely used as system symbol tables.
独Java: java.util.TreeMap, java.util.TreeSet.
独C++ STL: map, multimap, multiset.
独Linux kernel: CFQ I/O scheduler, linux/rbtree.h.
Other balanced BSTs. AVL trees, splay trees, randomized BSTs, ….
B-trees (and cousins) are widely used for file systems and databases.
独Windows: NTFS.
独Mac OS X: HFS, HFS+, APFS.
独Linux: ReiserFS, XFS, ext4, JFS, Btrfs.
独Databases: Oracle, DB2, Ingres, SQL, PostgreSQL.
44
![Page 42: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/42.jpg)
War story 1: red–black BSTs
Telephone company contracted with database provider to build real-time database
to store customer information.
Database implementation.
独Red–black BST.
独Exceeding height limit of 80 triggered error-recovery process.
Extended telephone service outage.
独Main cause = height bound exceeded!
独Telephone company sues database provider.
独Legal testimony:
45
should support up to 240 keys
“ If implemented properly, the height of a red–black BST
with n keys is at most 2 log2 n. ” — expert witness
![Page 43: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/43.jpg)
War story 2: red–black BSTs
46
https://twitter.com/cyberomin/status/835888786462625792
![Page 44: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/44.jpg)
The red–black tree song (by Sean Sandys)
47
I see a brand new node,I want to paint it black.We need a balanced tree,we’ve got to paint it black.
I see a brand new node,I want to paint it black.No time for AVL trees,We must paint it black.
I want to find my key inlog n time—that’s all.Rotating subtrees ’round,sure can be a ball.
I see a brand new node,I want to paint it black.Can’t have a lot of red nodes,we must paint them black.
Unfortunately, coding themcan be a $!#%.If we had half a brain,to splay trees we would switch.
And if they’re still confusing,you should have no fear.Because outside this class,of them you’ll never hear.
I wanna see it,painted, painted black.Black is nice.
I wanna see the nodes painted black.Black is nice.
I wanna see ’empainted, painted, painted, painted black.
Mm mm mm mm mm mm mm.Mm mm mm mm mm-mm.Mm mm mm mm mm mm mm.Mm mm mm mm mm-mmm.
performed by U. Washington CSE Band ’02
![Page 45: Algorithms...・Add new key to 3-node to create temporary 4-node. ・Move middle key in 4-node into parent. ・Repeat up the tree, as necessary. ・If you reach the root and it’s](https://reader036.fdocuments.net/reader036/viewer/2022071109/5fe3fa0bb6e60b5b51460e87/html5/thumbnails/45.jpg)
© Copyright 2020 Robert Sedgewick and Kevin Wayne
48