B+-Tree
B+-tree is a rooted tree satisfying the following properties
All paths from root to leaf are of the same length
Each node that is not a root or a leaf has between n/2 and n
children.
A leaf node has between (n–1)/2 and n–1 values
Special cases:
If the root is not a leaf, it has at least 2 children.
If the root is a leaf (that is, there are no other nodes in the tree), it
can have between 0 and (n–1) values.
1/18/2023
B+ Tree Example
2
To Records
1/18/2023
B+-Tree Node Structure
3
Typical node
Ki are the search-key values
Pi are pointers to children (for non-leaf nodes) or pointers to
records or buckets of records (for leaf nodes).
The search-keys in a node are ordered
K1 < K2 < K3 < . . . < Kn–1
1/18/2023
Leaf Nodes in B+-Trees
4
Properties of a leaf node:
For i = 1, 2, . . ., n–1, pointer Pi either points to a file record with search-key
value Ki, or to a bucket of pointers to file records, each record having search-
key value Ki.
If Li, Lj are leaf nodes and i < j, Li’s search-key values are less than Lj’s search-
key values
Pn points to next leaf node in search-key order
1/18/2023
Non-Leaf Nodes in B+-Trees
5
Non leaf nodes form a multi-level sparse index on the leaf nodes. For
a non-leaf node with m pointers:
All the search-keys in the subtree to which P1 points are less than
K1
For 2 i n – 1, all the search-keys in the subtree to which Pi
points have values greater than or equal to Ki–1 and less than Ki
All the search-keys in the subtree to which Pn points have values
greater than or equal to Kn–1
1/18/2023
Sample non-leaf
120
150
180
to keys to keys to keys
< 120 120 k<150 150k<180 180
1/18/2023
Sample leaf node
7
From non-leaf node
to next leaf
in sequence
120
130
with key 120
with key 130
To record
To record
1/18/2023
3
5
11
30
30
35
100
101
B+ Tree Example
110
8
100
To Records
120
130
150
156 120
179 150
180
180
200
1/18/2023
Insert into B+ tree
9
First lookup the proper leaf
(a) simple case
leaf not full: just insert (key, pointer-to-record)
(b) leaf overflow
(c) non-leaf overflow
(d) new root
1/18/2023
(a) Insert key = 32
10
n=3
100
30
11
30
31
32
3
5
1/18/2023
(b) Insert key = 7
11
n=3
100
30
7
57
11
30
31
3
5
1/18/2023
100
160
(c) Insert key = 160
150
156 120
179 150
180
12
160
179
180
180
n=3
200
1/18/2023
(d) New root, insert 45 n=3
13
new root Height grows at root
30
=> balance maintained
10
20
30
40
10
20
32
12
25
30
40
45
40
1
2
3
1/18/2023
Deletion from B+ tree
14
Again, first lookup the proper leaf;
(a): Simple case: no underflow;
(b): Borrow keys from an adjacent sibling
(if it doesn't become too empty);
(c): Underflow
1/18/2023
(b) Delete 50
=> min # of keys
15
in a leaf = 5/2 = 2
n=4
40 35
100
10
35
10
20
30
35
40
50
1/18/2023
(c) Leaf Underflow Delete 50
n=4
16
100
40
20
40
20
30
40
50
1/18/2023
(d) Non-leaf underflow Delete 37
=> min # of keys in a
non-leaf =
(n+1)/2 - 1=3-1= 2
n=4
25
new root
40
25
40
10
20
30 30
30
37
10
14
20
22
25
26
40
45
1
3
17 1/18/2023
B-Tree
18
Similar to B+-tree, but B-tree allows search-key values to appear only
once; eliminates redundant storage of search keys.
Search keys in nonleaf nodes appear nowhere else in the B-tree; an
additional pointer field for each search key in a nonleaf node must be
included.
Generalized B-tree leaf node vs B+ tree
Nonleaf node – pointers Bi are the bucket or file record
pointers.
1/18/2023
B-Tree Index File Example
19
B-tree (above) and B+-tree (below) on same data
1/18/2023
Example
of
B+
tree
1
B+
Tree
Example
• A
balanced
tree
• Each
node
can
have
at
most
m
key
fields
and
m+1
pointer
fields
• Half-‐full
must
be
sa@sfied
(except
root
node):
• m
is
even
and
m=2d
– Leaf
node
half
full:
at
least
d
entries
– Non-‐leaf
node
half
full:
at
least
d
entries
• m
is
odd
and
m
=
2d+1
– Leaf
node
half
full:
at
least
d+1
entries
– Non-‐leaf
node
half
full:
at
least
d
entries
(i.e.,
d+1
pointers)
2
Show
the
tree
aLer
inser@ons
• Suppose
each
B+-‐tree
node
can
hold
up
to
4
pointers
and
3
keys.
• m=3
(odd),
d=1
• Half-‐full
(for
odd
m
value)
– Leaf
node,
at
least
2
(d+1)
entries
– Non-‐leaf
nodes,
at
least
2
(d+1)
pointers
(1
entry)
• Insert
1,
3,
5,
7,
9,
2,
4,
6,
8,
10
3
Insert
1,
3,
5,
7,
9,
2,
4,
6,
8,
10
• Insert
1
4
Insert
1,
3,
5,
7,
9,
2,
4,
6,
8,
10
1
• Insert
3,
5
5
Insert
1,
3,
5,
7,
9,
2,
4,
6,
8,
10
1
• Insert
3,
5
1 3 5
6
Insert
1,
3,
5,
7,
9,
2,
4,
6,
8,
10
1 3 5
• Insert
7
7
Insert
1,
3,
5,
7,
9,
2,
4,
6,
8,
10
1 3 5
• Insert
7
1 3 5 7
8
Insert
1,
3,
5,
7,
9,
2,
4,
6,
8,
10
5
1 3 5 7
• Insert
9
9
Insert
1,
3,
5,
7,
9,
2,
4,
6,
8,
10
5
1 3 5 7
• Insert
9
1 3 5 7 9
10
Insert
1,
3,
5,
7,
9,
2,
4,
6,
8,
10
5
1 3 5 7 9
• Insert
2
11
Insert
1,
3,
5,
7,
9,
2,
4,
6,
8,
10
5
1 3 5 7 9
• Insert
2
5
1 2 3 5 7 9
12
Insert
1,
3,
5,
7,
9,
2,
4,
6,
8,
10
5
1 2 3 5 7 9
• Insert
4
13
Insert
1,
3,
5,
7,
9,
2,
4,
6,
8,
10
5
1 2 3 5 7 9
• Insert
4
3 5
1 2 3 4 5 7 9
14
Insert
1,
3,
5,
7,
9,
2,
4,
6,
8,
10
3 5
1 2 3 4 5 7 9
• Insert
6
15
Insert
1,
3,
5,
7,
9,
2,
4,
6,
8,
10
3 5
1 2 3 4 5 7 9
• Insert
6
3 5 7
1 2 3 4 5 6 7 9
16
Insert
1,
3,
5,
7,
9,
2,
4,
6,
8,
10
3 5 7
1 2 3 4 5 6 7 9
• Insert
8
17
Insert
1,
3,
5,
7,
9,
2,
4,
6,
8,
10
3 5 7
1 2 3 4 5 6 7 9
• Insert
8
3 5 7
1 2 3 4 5 6 7 8 9
18
Insert
1,
3,
5,
7,
9,
2,
4,
6,
8,
10
3 5 7
1 2 3 4 5 6 7 8 9
• Insert
10
19
Insert
1,
3,
5,
7,
9,
2,
4,
6,
8,
10
3 5 7
1 2 3 4 5 6 7 8 9
• Insert
10
7
3 5 9
7 8 9 10
1 2 3 4 5 6
20