This preview shows page 1. Sign up to view the full content.
Unformatted text preview: Symbol table review 3.3 BALANCED SEARCH TREES guarantee average case implementation ordered
iteration? operations
on keys search RO BERT SEDGEWICK search hit insert delete N N N N/2 N N/2 no equals() binary search
(ordered array) FOURTH delete sequential search
(linked list) Algo r it h m s insert 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() ‣ 23 search trees
‣ redblack BSTs
‣ Btrees EDITION K EVIN W AYNE Challenge. Guarantee performance.
This lecture. 23 trees, leftleaning redblack BSTs, Btrees.
Algorithms, 4th Edition · Robert Sedgewick and Kevin Wayne · Copyright © 2002–2011 · introduced to the world
in COS 226, Fall 2007 October 18, 2011 6:58:42 AM 2 23 tree
Allow 1 or 2 keys per node. •
• 2node: one key, two children.
3node: 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. ‣ 23 search trees
‣ redblack BSTs
‣ Btrees 3node
smaller than E AC EJ
H between E and J 3 2node M
larger than J L P R
SX
null link Anatomy of a 23 search tree 4 Search in a 23 tree Insertion in a 23 tree •
•
• Case 1. Insert into a 2node at bottom. Compare search key against keys in node. •
• Find interval containing search key.
Follow associated link (recursively).
successful search for H look to the left M
R EJ
H AC look in the middle
R EJ AC H R H P L search for K ends here
M
SX P L AC H P H AC Successful (left) and unsuccessful (right) search in a 23 tree KL P SX replace 2node with
new 3node containing K SX Insert into a 2node B is between A and C so look in the middle
link is null so B is not in the tree (search miss) found H so return value (search hit) R EJ R
L SX P L R EJ
SX H AC M EJ R M M AC SX P L EJ
SX P L H M EJ R B is less than E
so look to the left H is between E and L so M M EJ
SX P L H inserting K B is less than M so look to the left AC Replace 2node with 3node. unsuccessful search for B H is less than M so AC Search for key, as usual. 5 Insertion in a 23 tree 6 Insertion in a 23 tree
inserting D Case 2. Insert into a 3node at bottom. •
• Case 2. Insert into a 3node at bottom. •
•
• Add new key to 3node to create temporary 4node.
Move middle key in 4node into parent. why middle key? Move middle key in 4node into parent.
Repeat up the tree, as necessary. search for Z ends
at this 3node R EJ
H P L H P replace 2node
with new 3node
containing
middle key EJ
AC H RX
L P S ACD R add middle key C to 3node
to make temporary 4node P A D 7 A D H SX M M L P SX P EM
R J
D H L P SX split 4node into two 2nodes
pass middle key to parent R
L SX R H add middle key E to 2node
to make new 3node A CEJ split 4node into two 2nodes
pass middle key to parent P L split 4node into two 2nodes
pass middle key to parent M L R CEJ SX SX M H C Z Insert into a 3node whose parent is a 2node H P L add new key D to 3node
to make temporary 4node
EJ SXZ M H P L add new key D to 3node
to make temporary 4node
EJ R EJ
AC H add middle key C to 3node
to make temporary 4node M search for D ends
at this 3node replace 3node with
temporary 4node
containing Z R
L inserting D SX M
EJ
AC C ACD M R EJ Add new key to 3node to create temporary 4node.
A inserting Z AC M search for D ends
at this 3node SX Insert into a 3node whose parent is a 3node 8 A 23 tree construction trace
inserting D Case 2. Insert into a 3node at bottom. •
•
•
• Add new key to 3node to create temporary search for D ends
at this
4node. 3node Move middle key in 4node into parent.
If you reach the root and it's a 4node, split H ACD EJ
L A add new key D to 3node
to make temporary 4node
EJ
ACD E H L S insert A add middle key C to 3node
to make temporary 4node
CEJ Remark. Splitting the root increases height by 1.
A
E
E
A R 23 A D H S L A R AC EH A X M L C A E L AC E L A E L PR MR A E E HL P less
than a L PR between
a and b P L S E E H
C b
SX cd A MR
E L P SX same keys in increasing order between
b and c between
d and e between
c and d greater
than e 23 construction traces ace
MR A X ae standard indexing client C CS H
X P L b S d L
X E H CR H C L
H L AP ES X M standard indexing client
H A less
than a MR
E L P between
a and b between
b and c between
c and d between
d and e greater
than e SX Splitting a 4node is a local transformation that preserves balance same keys in increasing order C A 10 M C R H R P P
construction traces
E 23L C SX P
M AC M S M MC
A same M H R E
P L C A EH P H A H C R H L
M H R A AC C LM AS X E P A C
M MH L SX H
ER LM E A A
E
SX HM H X CH P E C P SAXC ER E A L P
S
Splitting a 4node is a localMtransformation: constant number ofHoperations. H A H AC
P M ER C AC E S H insert A A R Local transformations C a H23 tree
SX
A in
M E C
A S ER M S CH SX P standard indexing client AC A EH M
AC H X CH
AC L ER H L CH
L Splitting E Rroot
the RS H R C R E E A The same keys AA C D R SH ascending order.
inserted in L
H M E E A C H RS
split 4node into A
tree construction E
trace
three 2nodes
increasing tree
Cheight by 1
CE
J SX HM E
A CL AC H A ER P RS C A SX H A P AC
C M S E 9 split 4node into two 2nodes
pass middle key to parent
E H A ER J A
D
AC
Splitting the root
C C ES C AC L M S H E E
A E
C X ES A split 4node into two 2nodes
pass middle key to parent
split 4node into
three 2nodes
increasing tree
height by 1A ER AC E L H D RS AC
insert A S insert S CEJ H AC L E H add middle key C to 3node
to make temporary 4node search for D ends
at this 3node insert S H RS AC L add new key D to 3node
to make temporary 4node
EJ
it into three 2nodes. inserting D C H E
A EJ
AC Repeat up the tree, as necessary. R Standard indexing client. Insertion in a 23 tree S M
E L P
23 construction traces 11 12 Global properties in a 23 tree 23 tree: performance Invariants. Maintains symmetric order and perfect balance. Perfect balance. Every path from root to null link has same length. Pf. Each transformation maintains symmetric order and perfect balance. root parent is a 3node b abc a left c Typical 23 tree built from random keys
de bde a abc
parent is a 2node left
abc right middle bd d a ae a ac
bcd b right b ab d •
•
•
• ace bcd c Tree height. c d abd c cde e Worst case: lg N. [all 2nodes] Best case: log3 N ≈ .631 lg N. [all 3nodes] Between 12 and 20 for a million nodes.
Between 18 and 30 for a billion nodes. Guaranteed logarithmic performance for search and insert. Splitting a temporary 4node in a 23 tree (summary)
13 ST implementations: summary 15 23 tree: implementation?
Direct implementation is complicated, because: guarantee average case implementation ordered
iteration? •
•
•
• operations
on keys search insert delete search hit insert delete sequential search
(linked 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 c lg N c lg N c lg N c lg N c lg N c lg N yes Need multiple compares to move down tree.
Need to move back up the tree to split 4nodes. compareTo() 23 tree Maintaining multiple node types is cumbersome. compareTo() Large number of cases for splitting. Bottom line. Could do it, but there's a better way. constants depend upon
implementation 16 17 Leftleaning redblack BSTs (GuibasSedgewick 1979 and Sedgewick 2007)
3node 1. Represent 2–3 tree as a BST.
red−black tree 2. less
Use "internal"Mleftleaning links as "glue" forbetween
3–nodes.greater J
C H a bP L less
than a A than a R 3node E b S greater
than b between
a and b M E
A C H less
than a less
than a R P L X S red−black tree M H M
J R EJ
AC black links connect
2nodes and 3nodes red links "glue"
nodes within a 3node Encoding a 3node with two 2nodes
connected by a leftleaning red link 23 tree greater
than b between
a and b Encoding a 3node with two 2nodes
connected by a leftleaning red link greater
than b between
a and b larger key is root a b J than b a and b X a horizontal red links ‣ 23 search trees
‣ redblack BSTs
‣ Btrees ab P L E
C SX R
P L X
S H A 23 tree corresponding redblack BST 1−1 correspondence between redblack and 23 trees horizontal red links M 18 A An equivalent definition C H R
P L S X Leftleaning redblack BSTs: 11 correspondence with 23 trees
23 tree A BST such that: •
•
• 19 J E M Key property. 1–1 correspondence between 2–3 andJLLRB.
E No node has two red links connected to it. H AC Every path from root to null link has the same number of black links. red−black tree Red links lean left. L R
P SX M
1−1 correspondence between redblack and 23 trees
J
R "perfect black balance" E
C P L X
S H A red−black tree M horizontal red links J
E
C R
P L M
J E X A C H R
P L H A 23 tree horizontal red links A C AC M
J
H X M
R EJ E S S R
L P H L P SX 1−1 correspondence between redblack and 23 trees S X 20 21 Search implementation for redblack BSTs Redblack BST representation Observation. Search is the same as for elementary BST (ignore color). Each node is pointed to by precisely one link (from its parent) ⇒
can encode color of links in nodes. but runs faster because of better balance 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;
} private static final boolean RED
= true;
private static final boolean BLACK = false;
red−black tree M
J
E
C private class Node
{
Key key;
Value val;
Node left, right;
boolean color;
// color of parent link
} R
P L X
S H A
horizontal red links M
J E
A C H private boolean isRed(Node x)
{
if (x == null) return false;
return x.color == RED;
} R
P L 23 tree S X null links are black M Elementary redblack BST operations H L P SX Elementary redblack BST operations Left rotation. Orient a (temporarily) rightleaning red link to lean left. Left rotation. Orient a (temporarily) J
D h.right.color
is BLACK G private static final boolean RED
= true;
private static final boolean BLACK = false;
private class Node
{
Key key;
Value val;
Node left, right;
int N;
boolean color; 22 1−1 correspondence between redblack and 23 trees E C
A Remark. Most other ops (e.g., ceiling, selection, iteration) J
R
E are also identical.
AC h h.left.color
is RED } Node(Key key,
{
this.key
this.val
this.N
this.color
} //
//
//
//
//
// key
associated data
subtrees
# nodes in this subtree
color of link from
parent to this node Value val)
=
=
=
= key;
val;
1;
RED; 23 private boolean isRed(Node x)
{
if (x ==
rightleaning rednull) to leanfalse;
link return left.
return x.color == RED;
} Node representation for red−black trees rotate E left rotate E left (before)
h E
S x less
than E
between
E and S greater
than S (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;
} S
h x E
greater
than S less
than E Invariants. Maintains symmetric order and perfect black balance. between
E and S 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;
} Invariants. Maintains symmetric order and perfect black balance.
24 25 Elementary redblack BST operations Elementary redblack BST operations Right rotation. Orient a leftleaning red link to (temporarily) lean right. Right rotation. Orient a leftleaning red link to (temporarily) lean right. rotate S right rotate S right (before) S
h x E
greater
than S less
than E (after) 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;
} between
E and S h E
S x less
than E
between
E and S Invariants. Maintains symmetric order and perfect black balance. greater
than 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;
} Invariants. Maintains symmetric order and perfect black balance.
26 27 Elementary redblack BST operations Elementary redblack BST operations Color flip. Recolor to split a (temporary) 4node. Color flip. Recolor to split a (temporary) 4node. ﬂip colors ﬂip colors (before)
h E A less
than A S between
A and E between
E and S greater
than S (after) private void flipColors(Node h)
{
assert !isRed(h);
assert isRed(h.left);
asset isRed(h.right);
h.color = RED;
h.left.color = BLACK;
h.right.color = BLACK;
} h E A less
than A Invariants. Maintains symmetric order and perfect black balance. S between
A and E between
E and S greater
than S private void flipColors(Node h)
{
assert !isRed(h);
assert isRed(h.left);
asset isRed(h.right);
h.color = RED;
h.left.color = BLACK;
h.right.color = BLACK;
} Invariants. Maintains symmetric order and perfect black balance.
28 29 b Insertion in a LLRB tree: overview Insertion in a LLRB tree Basic strategy. Maintain 11 correspondence with 23 trees by Warmup 1. Insert into a tree with exactly 1 node. b search ends
at this null link applying elementary redblack BST operations. insert C C
insert E
E
SS A right link red
sorotate left
rotate left RS search ends
at this null link a b E
E
RR
E
E
R
R a SS E S
S AC right RS 23 tree a search ends
at this null link
attached new node
with red link
b
root red link to
new node
containing a
converts 2node
to 3node b
a rotated left
to make a
legal 3node Insert into a single
2node (two cases) root search ends
at this null link a Insert into a 2node
LLRB tree
at the bottom red link to
new node
containing a
converts 2node
to 3node
root a root C
C C
C right root b R
R add new
add new
node here
node here A
A left E A
A A
A a root attached new node
with red link
b 30 31 root b Insertion in a LLRB tree rotated left
a
to make
Insertion in a LLRB tree a
legal 3node Case 1. Insert into a 2node at the bottom. Warmup 2. Insert into a single with exactly 2 nodes.
Insert into a tree
2node (two cases) •
• Do standard BST insert; color new link red. larger If new red link is a right link, rotate left. larger b
a insert C C
insert E
E add new
add new
node here
node here
right link red
sorotate left
rotate left
A
A A
A SS RS b
a E
E
RR
E
E
R
R SS b
S
S Insert into a 2node
LLRB tree
at the bottom a E
AC searchc
ends
at this
null
b link RS 23 tree between smaller
between between
c
c
c b a at this null link R
R C
C C
C A smaller c search ends
a
search a
search ends ends at this null link
at link
at this null this null link search ends
search endssearch ends
at this null link at this null link
c E A
A larger
smaller b
search ends search ends c
b
at this
at this
a
null link
a
b
null link attached new
c
node with
red
b link c ca a
attached new c
attached new attached new
node with
attached new with
ab
node
attached new node with bc
b
red link
b
node with
b
node with red link
c
attached new red link
ab
ab
red link
c
attached new
a link
red
node with
a
c
c
attached new
node with
red link c
a
b
node with red link
cb
red link
rotated
a
rotated left
rotated b
a right rotated left
b
colors flippedrighta
c
rotated
b
a
to black
rotated
rotated left
colors flipped
b
a right c
b
b
right
rotated
c
a
colors flipped to black
b
c
a
c
right
a
a
to black c
rotated
b colors flipped
a to right c
b
black
c
colors flipped
colors flipped
b
cc
a
a
to black
b
to black
colors flipped
colors flipped
a to blackc
b
b
c
a
to black
colors flipped
c
c
a
b a to black
Insert into a single 3node (three cases)
c
a
Insert into a single 3node (three cases) 32 33 Insert into a single 3node (three cases) so rotate right node here
C E
C Insertion in a LLRB tree S A inserting H E Case 2. Insert into a 3node at the bottom. •
•
•
• S Do standard BST insert; color new link red.
inserting H Rotate to balance the 4node (if needed).
E
C
S
Flip colors to pass red link up one level.
inserting H
A E add new
node here R inserting H E
C
A add new
node here R add new
node here
E
S
two lefts in a rowC
so rotate right
R
A
E C
R E
C both children red
so flip colors S C H ES
C
right link red
so rotate A
left A
R E
C R E S E
R A C
H SH A
R SC
A S
H A
Insert into a 3node
at the bottom 34 H H H Flip colors to pass red link up one level.
C S
M inserting P H
add new
node here S E C A
S H A E add new
C
node here
A
R H S M
H S M
E R M both children
red so
flip colors P H right link red
M
so P
rotate left
R
A
H
S
E
two lefts in a row
M
so rotate right C
P
A
RH inserting P
Repeat case 1 or case 2 up the M
tree (if needed).
C R M
R C R E A
Rotate to make lean left (if needed).
R C
A E inserting P Rotate to balance the 4node (if needed). R R S H LLRB tree insertion demo
C H C M right link red
so rotate left Do standard BST insert; color new link red. E R
E •
•
•
•
• inserting P S H R H
C
S
add new
S
A
H
node here A
R
both children red
two lefts in a row
H
so flip colors link red
so rotate right
right
E
so rotate left
E
both children redC
R
C
S
so flip colors
E
S
A
H
A
RE
C
R
R
HC
right link red
S
A
H
S so rotate left
A
H
both children red
so flip colors
right link red
R
E
E
so rotate left
S
E
C
R
C
R
A A Case 2. Insert into a 3node at the bottom. E R E R S H
right link red
so rotate left S A
two lefts in a row
H
so rotate right R A E S A C
two lefts in a row
so rotate right E
Rotate to make lean left (if needed).
C both children red
so flip colors R H add new
S
node here
both children
R
red so
P
flip colors
S
E E
C A H C A Insertion in a LLRB tree: passing red links up the tree R M AR S
two lefts in a row
P so rotate right R
H
M
S
both children red
P
E
so flip colors
C
H
MA
both children red
R
E
so flip colors
P
C
H
S
M
A
R
E
M
P
C
H
S
R
E
A
P
C
H
S
M
A
R
E C
S
A
both children
red so
P
flip colors add new
S
node here E
C
M
right link red
C
M
so rotate left
R
both children
A
H
R
red so
P
A
H
S
E
add new E
S
flip colors
node here
C
M
C
M
both children
right link red
P
A
H
R
red so
P
A
H
so rotatecolors
R
flip left
S
E
two
S lefts in a row
E
C
M
so rotate right
right children
both link red
C
M
red so
R
P so rotate left
A
H
R
flip colors
P
A
H
M
S
S
E
C
P
H
E
S
Passing a red link up the tree P
C
M
right link red
two lefts in a row
so rotate left
so rotate right
A
C
R
H
P
A
H
R
A
S
E
both children red
two lefts in a row
M
S
Passing a red link up the tree
C
M
so flip colors
so rotate right
P
E
P
A
H
R
C
H
M
M
S
two lefts in a row
A
R
E
P
E
so rotate right
both children red
P
H
S
so flip colors C
RC
H
A
M
AS
M
both children red
P
E
M
so flip colors E
R
C
H
R
E
P
C
H
S
M
A
P
C
H
S
A
both children red E
R
A
so flip colors
M
P
C
H
S
R
E
M
A
Passing a red link up the tree 35 LLRB tree insertion trace SInsert into a 3node
at the bottom A Standard indexing client. Insert into a 3node
at the bottom R E C S
E Insert into a 3node
H at the bottom
C P H A C
M A A
R E
C Insert into a 3node
at the bottom H P C M
S insert S
E A
P H E P H S insert A S R A S S
Passing a red link up S tree
the E C C ES A S
Passing a red link up the tree A E A A Passing a red link up the tree E S C E A A S E
R C A E S E
C E S A AC R
R H C A H
E
H L
RS L C
A E
H ER S E H
RS A R C E AC H H S
M M C
A L E A
R 36 redblack BST X E C H S X H corresponding 23 tree C P A M
E L 37 P H E C
LLRB tree insertion trace C S A L R A E
H R Standard indexing client (continued).
S E H C Insertion in a LLRB tree: Java implementation L C H Same code for both cases. M C M A •
•
• L E A
R H
X E X C P S H E RC R
X E M M C
A C E P H X A M
R E
C
A P
E S
M L H
R C H
A L P H X X
AC S standard indexing client E C
H
A LE right
rotate 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 if (cmp == 0) h.val = val; R
P S
AC h left
rotate h flip
colors Passing a red link up a redblack tree M
SX
L M
R Both children red: flip colors. h H H
M Left child, leftleft grandchild red: rotate right. P ES XL ER
C
HM
A
E RA C S E P A
H AC A M Right child red, left child black: rotate left. H P R SX
M
L
P
R
SX
M
L
P S 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); R
X
S return h;
} same keys in increasing order redblack BST Redblack tree construction traces
corresponding 23 tree lean left
balance 4node
split 4node only a few extra lines of code
to provide nearperfect balance 38 Insertion in a LLRB tree: visualization insert at bottom
(and color red) 39 Insertion in a LLRB tree: visualization 255 insertions in ascending order 255 insertions in descending order 40 41 Insertion in a LLRB tree: visualization Balance in LLRB trees
Proposition. Height of tree is ≤ 2 lg N in the worst case.
Pf. •
• Every path from root to null link has same number of black links.
Never two red links inarow. 255 random insertions Property. Height of tree is ~ 1.00 lg N in typical applications.
42 ST implementations: summary 43 War story: why redblack? guarantee Xerox PARC innovations. [ 1970s ] average case
ordered
iteration? N/2 no equals() N/2 N/2 yes compareTo() 1.39 lg N 1.39 lg N ? yes compareTo() c lg N c lg N c lg N yes •
•
•
•
•
•
•
•
• operations
on keys compareTo() implementation
search insert delete search hit insert delete sequential search
(linked list) N N N N/2 N binary search
(ordered array) lg N N N lg N BST N N N 23 tree c lg N c lg N c lg N Alto.
GUI.
Ethernet.
Smalltalk.
InterPress.
Laser printing.
Bitmapped display.
WYSIWYG text editor. Xerox Alto ...
A DIClIROlV1ATIC FUAl\lE\V()HK Fon BALANCED TREES redblack BST 2 lg N 2 lg N 2 lg N 1.00 lg N * 1.00 lg N * 1.00 lg N * yes compareTo() Leo J. Guibas
.Xerox Palo Alto Research Center,
Palo Alto, California, and
CarnegieAfellon University * exact value of coefficient unknown but extremely close to 1
ABSTUACT 44 I () this paper we present a uniform framework for the implementation
and study of halanced tree algorithms. \Ve show how to imhcd in this
framework the best known halanced tree tecilIliques and thell usc the
framework to deVl'lop new
which perform the update and
rebalancing in one pass, Oil the way down towards a leaf. \Ve
conclude with a study of performance issues and concurrent updating. O. Introduction and Robert Sedgewick*
Program in Computer Science
Brown University
Providence, R. I. the way down towards a leaf. As we will see, this has a number of
significant advantages ovcr the older methods. We shall cxamine a
numhcr of variations on a common theme and exhibit full
implementations which are notable for their brcvity. One
imp1cn1entation is exatnined carefully, and some properties about its
behavior are proved.n both sections 1 and 2 particular attention is paid to practical
implementation issues, and cOlnplcte impletnentations are given for
all of the itnportant algorithms. '1l1is is significant because one
measure under which balanced tree algorithtns can differ greatly is 45 File system model
Page. Contiguous block of data (e.g., a file or 4,096byte chunk).
Probe. First access to a page (e.g., from disk to memory). ‣ 23 search trees
‣ redblack BSTs
‣ Btrees slow fast Property. Time required for a probe is much larger than time to access
data within a page.
Cost model. Number of probes.
Goal. Access data using minimum number of probes. 47 48 Btrees (BayerMcCreight, 1972) Searching in a Btree Btree. Generalize 23 trees by allowing up to M  1 keylink pairs per node. •
•
• •
•
•
• At least 2 keylink pairs at root. choose M as large as possible so
that M links fit in a page, e.g., M = 1024 At least M / 2 keylink pairs in other nodes. Start at root.
Find interval for search key and take corresponding link.
Search terminates in external node. External nodes contain client keys.
Internal nodes contain copies of keys to guide search. each red key is a copy
of min key in subtree *DH
external
3node
DEF
client keys (black)
are in external nodes HIJ *DH KQU KMNOP QRT KQU
follow this link because
E is between D and H external 4node external 5node (full) *K follow this link because
E is between * and K internal 3node sentinel key *BC searching for E 2node *K *BC UWXY DEF HIJ KMNOP QRT UWX search for E in
this external node all nodes except the root are 3, 4 or 5nodes
Anatomy of a Btree set (M = 6) Searching in a Btree set (M = 6) 49 50 Insertion in a Btree Balance in Btree •
•
• Search for new key. Proposition. A search or an insertion in a Btree of order M with N keys Insert at bottom. requires between log M1 N and log M/2 N probes. Split nodes with M keylink pairs on the way up the tree.
inserting A Pf. All internal nodes (besides root) have between M / 2 and M  1 links. *HKQU *BCEF HIJ KMNOP QRT UWX In practice. Number of probes is at most 4. *HKQU *ABCEF HIJ KMNOP new key (A) causes
overflow and split *AB CEF QRT *CHKQU HIJ UWX M = 1024; N = 62 billion
log M/2 N≤4 Optimization. Always keep root page in memory. new key (C) causes
overflow and split KMNOP QRT UWX *K *CH *AB CEF root split causes
a new root to be created
HIJ KMNOP KQU QRT UWX Inserting a new key into a Btree set
51 Building a large B tree 52 Balanced trees in the wild
Redblack trees are widely used as system symbol tables. full page, about to split •
•
• external nodes
(line segment of length proportional
to number of keys in that node) Java: java.util.TreeMap, java.util.TreeSet.
C++ STL: map, multimap, multiset.
Linux kernel: completely fair scheduler, linux/rbtree.h. Btree variants. B+ tree, B*tree, B# tree, …
Btrees (and variants) are widely used for file systems and databases. •
•
•
• 53 Building a large Btree Windows: HPFS.
Mac: HFS, HFS+.
Linux: ReiserFS, XFS, Ext3FS, JFS.
Databases: ORACLE, DB2, INGRES, SQL, PostgreSQL. 54 Redblack BSTs in the wild Common sense. Sixth sense.
Together they're the
FBI's newest team. 55 ...
View Full
Document
 Spring '10
 VALERIEKING
 Binary Search

Click to edit the document details