B-Trees & its Variants Advanced Data Structure Spring 2007 Zareen Alamgir
Data Retrieval from External Storage In database programs, the data is too large to fit in
memory, therefore, it is stored on secondary storage (disks or tapes).
Disk access is very expensive, the disk I/O operation takes milliseconds while CPU processes data on the order of nanoseconds, one million times faster.
When dealing with external storage the disk accesses dominate the running time.
Balanced Binary Search Trees Balanced binary search trees (AVL & Red-Black) have
good performance if the entire data can fit in the main memory.
These trees are not optimized for external storage and require many disk accesses, thus give poor performance.
Reduce Disk Accesses Data is transfer to and from the disk in block. (typically block
are of 512, 2048, 4096 or 8192 bytes)
We can reduce disk accesses by Storing multiple records in a block on the disk. Reducing the height of the tree by increasing the
number of subtrees of a node.
To achieve above goals we use Multiway (m-way) search tree, which is a generalization of BST, binary search tree.
12 19 32 39 73 84
25 62
34 37 90 9475 7969 7130 31 45 513 5 15 17 21 23
Multiway(m-way) Search Trees In an m-way tree all the nodes have degree ≤ m. Each node has the following structure:
Ki are the keys, where 1 ≤ i ≤ q-1, q<=m
Pi are pointers to subtrees, where 1 ≤ i ≤ q, q<=m
The keys in each node are in ascending order K1 ≤ K2 ≤ ... ≤ Ki
The key Ki is larger than keys in subtree pointed by Pi and smaller than keys in subtree pointed by Pi+1 .
The subtrees are the m-way trees.
P1 K1
keys<K1
P2 Kq-1 PqKi-1 Pi Ki
Ki-1<keys<Ki
Kq-1<keys
Multiway(m-way) Search Trees M-way tree is a generalization of BST, its working, benefits
& issues are same. Problems
The tree is not balanced. Leaf nodes are on different
levels. Bad space usage, tree can
become skew.
Benefits Fast information
retrieval. Fast update.
M-way tree
12 23 32 73 84
25 62
90 9469 7130 313 5 15 21
17 19
B-Trees B-Tree is a balanced m-way tree that is tuned to
minimize disk accesses.
The node size of B-Tree is usually equal to the disk block size and the number of keys in a node depends on Key size Disk block size Data organization (keys or entire data records are store in nodes)
Access paths from root to leaf nodes are small.
12 19 32 39 73 84
25 62
34 37 90 9475 7969 7130 31 45 513 5 15 17 21 23
B-Tree: Definition A B-tree of order m has following properties
The root has at least two subtrees unless it is a leaf. Each non-root and each non-leaf node holds q-1 keys
and q pointers to subtrees, where . Each leaf node holds q-1 keys where . All leaves are on the same level.
It is clear that B-tree is always at least half full, has fewer levels and is perfectly balanced.
mqm 2/ mqm 2/
FrP1 K1
keys<K1
P2 Kq-1 PqKi-1 Pi Ki
Ki-1<keys<Ki
Kq-1<keys
B-Tree B-Tree can have a field KeyTally, KT, to indicate the number of keys
currently stored in the node.
B-Tree node usually contains key and data pointer pair. The data pointer points to the data record which is not stored in the node, with this scheme we can pack more keys & pointers in a B-Tree node.
KT P1
keys<K1
Kq-1 PqKi-1 Pi Ki
Ki-1<keys<Ki
Kq-1<keys
K1 D1 Ki-1 Di-1 Ki Di Kq-1 Dq-1
Data pointer Data pointer
Height of B-Tree Maximum height of the B-Tree with n keys is important as it
bound the number of disk accesses.
The height of the tree is maximum when each node has minimum number of the subtree pointers, . 2/mq
Height of B-Tree The height of B-tree is maximum if all nodes have minimum
number of keys.
1 key in the root + 2(q-1) keys on the second level +……+ 2qh-2(q-1) keys in the leaves (level h).
12
1log
21
:as given is hheight of Tree- Bin keys ofnumber theThus,
21
1
1)1(21
nprogressio geometric offormula theApplying
2)1(1
1)-(q2q1)-2q(q1)-2(q 1
1
1
1
2
0
2-h
nh
qn
q
q
q
h
h
h
ih
i
Height of B-Tree The height of B-tree is minimum if all nodes are full, thus we have
m-1 keys in the root + m(m-1) keys on the second level +……+ mh-1(m-1) keys in the leaf nodes
12
1log)1(log
)1(log
1
:as given is hheight of Tree- Bin keys ofnumber theThus,
1
1
1)1(
nprogressio geometric offormula theApplying
)1()1(
1)-(m1)-(m1)-m(m 1)-(m1
0
1
0
1-h2
nhn
nh
mn
m
m
mm
mmmm
mm
qm
m
h
h
h
h
i
iih
i
Height of B-Tree If number of nodes in B-tree equal 2,000,000 (2 million)
and m=200 then maximum height of B-tree is 3, where as the binary tree would be of height 20.
Note: Order m is chosen so that B-tree node size is nearly equal to the disk block size.
Search in a B-Tree Search in a B-tree is similar to the search in BST
except that in B-tree we make a multiway branching decision instead of binary branching in BST.
Search key 71
12 19 32 39 73 84
25 62
34 37 90 9475 7969 7130 31 45 513 5 15 17 21 23
B-Tree Insert Operation Insertion in B-tree is more complicated than in BST.
In BST, the keys are added in top down fashion resulting in a unbalanced tree.
B-tree is built bottom up, the keys are added in the leaf node, if the leaf node is full another node is created, keys are evenly distributed and middle key is promoted to the parent. If parent is full, the process is repeated.
B-tree can also be built in top down fashion using pre-splitting technique.
Basic IdeaFind position for the key
in the appropriate leaf node
Is node full ?
Split node: •Create a new node • Move half of the keys from the full node to the new node•Promote the median key (before split) to the parent.Split guarantees that each node has keys.
If parent is full
12/ m
Cases in B-Tree Insert Operation In B-tree insertion we have the following cases:
Case 1: The leaf node has room for the new key. Case 2: The leaf in which key is to be placed is full.
This case can lead to the increase in tree height.
Now we explain these cases in detail.
B-Tree Insert Operation Case 1: The leaf node has room for the new key.
10 25
14 19 20 23 32 38
Insert 3
3
85
Insert 3 in order
Find appropriate leaf node for key 3
B-Tree Insert Operation Case 2: The leaf in which key is to be placed is full.
10
3 5 8 14 19 20 23 32 38
Insert 16
No room for key 16 in leaf node
14 20 2319
Insert key 19 in parent node in order
2516
Find appropriate leaf node for key 16
16
19
Move median key 19 up andSplit node: create a new node and move keys to the new node.
B-Tree Insert Operation Case 2: The leaf in which key is to be placed is full and
this lead to the increase in tree height.45 55 67 81
14 19 20 23 32 389 12 47 51 59 75
13 27 33 38
32 38 47 51 59 75 32 38 47 51 59 75 32 38 47 51 59 75 32 38 47 51 59 75
48 52 57 61 72 77 86 92
55
B-Tree Insert Operation Case 2: The height of the tree increases.
14 19 20 23 29 319 12 35 36 41 42
55
13 27
3
2
3
8
4
7
5
1
5
9
7
5
3
2
3
8
4
7
5
1
5
9
7
5
3
2
3
8
4
7
5
1
5
9
7
5
3
2
3
8
4
7
5
1
5
9
7
5
48 52 57 61 72 77 86 92
Insert 16
16
No room for key 16,Move median key 19 up & Split node
Insert 19 in parent node in order
No room for 19 in parent,Split parent node
14 16 20 2319
33 38
Insert 27 in parent in order
67 8145
55
2719
No room for 27 in parent, Split node
B-Tree Delete Operation Deletion is analogous to insertion, but a little
more complicated.
Two major cases Deletion from leaf node Deletion from nonleaf node
Apply delete by copying technique used in BST to reduce this case to deleting a key from leaf.
In delete by copying, the key to be deleted is replaced by the largest key in the right subtree or smallest in left subtree (which is always a leaf).
B-Tree Delete Operation Leaf node deletion cases:
After deletion node is at least half full. After deletion underflow occurs
Redistribute: if number of keys in siblings > . Merge nodes if number of keys in siblings < . Merging leads to decrease in tree height.
12
m
12
m
B-Tree Delete Operation After deletion node is at least half full. (inverse of insertion
case 1)
10 25
3 5 8 14 19 32 38 40
Search key 3
Key found, delete key 3.Move others keys in the node to eliminate the gap.
45
B-Tree Delete Operation Underflow occurs, evenly redistribute the keys if left or right
sibling has keys .
Delete 14
10
5 8 14 19 32 38
Underflow occurs, evenly redistribute keysin the underflow node, in its sibling and the separator key.
40
25
12/ m
Search key 14
45
B-Tree Delete Operation Underflow occurs and the keys in the left & right sibling are
. Merge the underflow node and a sibling.
12/ m
Delete 25
10
5 8 19 25 38 40
Underflow occurs, merge nodes.
32
Move separator key down.
Move the keys to underflow node and discard the sibling.
B-Tree Delete Operation Underflow occurs, height decreases after merging.
8
3 5 21 27 47 66
Delete 21
79 85
73 75 78 81 83 88 90 92
70
Underflow occurs, merge nodes by moving separator key and the keys in sibling node to the underflow node.
32
Underflow occurs, merge nodes
Issues in B-tree In B-tree, accessing data in sequential order is not
efficient. In-order traversal of the B-tree requires many disk accesses.
In B-tree, data pointers are stored in each node, thus resulting in less subtree pointers per node and more tree levels.
KT free spaceP1
keys<K1
Kq-1 PqKi-1 Pi Ki
Ki-1<keys<Ki
Kq-1<keys
K1 D1 Ki-1 Di-1 Ki Di Kq-1 Dq-1
Data pointer Data pointer
B+ -Tree- Variant of B-Tree Resolves the issues in B-tree.
In B+ -tree Pointers to data is stored only in leaf nodes. Internal nodes contain only keys and subtree pointers
Can accommodate more keys in internal nodes. Less disk accesses due to fewer levels in the tree.
B+ -tree provides faster sequential access of data.
B+-Tree Structure B+-tree consist of two parts
Index Set Provides indexes for fast access of data. Consist of internal nodes that store only key & tree pointers.
Sequence Set Consist of leaf nodes that contain data pointers. Provide efficient sequential access of data(using doubly linked list).
Index Set
Sequence SetSequential Search
B+-Tree: Index Node Structure The basic structure of the B+-tree index node of
order m is same as that of B-tree node of order m.
The root has at least two subtrees unless it is a leaf. Each non-root index node holds q-1 keys and q subtree
pointers, where .
Only difference is that index node do not contain data pointers.
FrP1 K1
keys<K1
P2 Kq-1 PqKi-1 Pi Ki
Ki-1<keys<Ki
Kq-1<keys
mqm 2/
B+-Tree: Sequence Node Structure The structure of the B+-tree sequence node is as
follows:
K1 D1 Ki-1 Di-1 Ki Di Kq-1 Dq-1
Data pointer
Pointer to next leaf node in tree
Data pointer Data pointer Data pointer
Search in a B+-Tree The search in B+-tree works similar to B-tree but it
always ends at the leaf node because Pointer to the data is stored in the leaf node. Existence of the key in index set does not guarantee that the
particular record is present in the tree. A key can occur multiple time in the index set. (this does not
create problem because the key in index set node act only as a separator).
B+-tree of order m=5
Note 62 is not present in the sequence set
15 21 34 45 75 90
30 62
34 37 90 9475 7969 7130 31 45 513 5 15 17 21 23
B+-Tree Insert Case: The leaf node has room for the key to be inserted.
14 32
14 19 20 23 32 38
Insert 3
3
85
Find appropriate leaf node for key 3, and insert in order.
B+-Tree Insert Operation Case 2: The leaf in which key is to be placed is full.
10
3 5 8 14 19 20 23 32 38
Insert 16
No room for key 16,Split node: create a new node and move keys to the new node.
14
2516
Find appropriate leaf node for key 16
16 20 2319 19
Modify Sequence Set next node links
Insert a copy of the first key of the new node in the parent node in order.
19
2/m
B+ -Tree Insert Operation Case: Only root node exists, and it is full.
10 17
Insert 18 Find appropriate position for key 18
Create a new index set node and make it a root node.
No room for key 18,Split node: create a new sequence set node and move keys to the new node.
2/m18
Insert the first key of the new sequence set node in the new root.
18
18
18
20 23
B+-Tree Delete Operation B+-tree deletion follows same rules as that of B-
tree deletion but the separator in index set node is not removed when a key is deleted.
Deletion cases: After deletion node is at least half full. After deletion underflow occurs
Redistribute: if number of keys in siblings > . Merge nodes if number of keys in siblings < . Merging lead to decrease in tree height.
12
m
12
m
B+-Tree Delete Case: After deletion node is at least half full.
14 32
3 5 8 14 19 32 38 40
Search key 14
Key found, delete key 14.Move others keys in the node to eliminate the gap.
Delete 14
21
Note: key 14 in the parent node is still a valid separator key. It is not modified.
B+-Tree Delete Operation Underflow occurs, evenly redistribute the keys if left or right
sibling has keys .
Delete 14
14
5 8 14 19 32 38
Underflow occurs, evenly redistribute keysin the underflow node and in the sibling node.
Note: unlike B-tree, the separator key in the parent node is not included. Why?
40
32
12/ m
Search key 14 & delete it. Separator key in the parent isno longer valid
Insert a copy of the first key of the sibling node in parent in order.
38
B+-Tree Delete Operation Underflow occurs and the keys in the left & right sibling are
. Merge the underflow node and a sibling.
12/ m
14
5 8 19 32 38 40
Underflow occurs, merge nodes.
Move keys in sibling to underflow node and discard the sibling node.
38
Delete 32 Search key 32 & delete it.
45 55 67 81
13 27 33 38 48 52 57 61 72 77 86 92
Efficient Sequential Access in B+-Tree
Sequence Set Start
For efficient sequential access, start from the beginning of the sequence set and traverse the sequence set using the next pointers in sequence set nodes
Comparison B-Tree & B+-TreeB-Tree Data pointers stored in all
nodes No redundant keys Search can end at any
node Slow sequential access Higher trees
B+ -Tree Data pointers stored in leaf
nodes (sequence set)
Redundant keys exist Search ends always at
leaves Efficient sequential access Flatter trees (no data pointers
in index set nodes)
B* -Tree -- Variant of B-Tree Each node of a B-tree represent a block of secondary memory,
therefore, accessing a node is expensive operation. Thus, the fewer nodes that are created, the better.
In B*-tree is a variant of B-tree introduced by Donald Knuth and named by Douglas Comer.
In a B*-tree, all nodes except the root are required to be at least two-thirds full, not just half full as in B-tree.
The number of keys in all non-root nodes in a B*-tree of order m is k, where .
Average utilization of B*-tree is 81%.
13
12
mkm
B* -Tree Insert Operation In B*-tree, the frequency of node splitting is decreased by
delaying a split.
A split in a B*-tree is delayed by attempting to redistribute the keys between node and its sibling when node overflows.
In B*-tree split operation two nodes are split into three instead of one into two as in B-tree.
All three nodes participating in the split are guaranteed to be two-thirds full after split.
B*-Tree Insert Operation Overflow occurs, evenly redistribute the keys between node
and its sibling.
Overflow occurs, evenly redistribute keysin the overflow node, in its sibling including the separator key in parent and new key.
10 12 15 16 21 25
Insert 2222
35 42 47 51 53
32
2924
B*-Tree Insert Operation Overflow occurs, sibling is full, split node.
Overflow occurs, sibling is full, split node.
10 12 15 16 21 24 47
Insert 72
72
51 53 55 57 5935 42
32
25 29
B* -Tree Delete Operation B*-tree deletion follows same rules as that of B-tree deletion.
Deletion cases: After deletion node is at least two third full. After deletion underflow occurs
Redistribute: if number of keys in siblings > . Merge nodes if number of keys in siblings < .
Examples of deletion are omitted as they are similar to B-tree deletion.
3
12 m
3
12 m
References Data Structure and Algorithms in C++, Adam Drozek.
Introduction to Algorithms, T.H.Cormen, C.E.Leiserson, R.L.Rivest, and C.Stein.
Fundamentals of Database Systems, Elmasri Navathe.
Fundamentals of Data Structures in C++, E.Horowitz, S.Sahni and D.Mehta
The Ubiquitous B-Tree, DOUGLAS COMER, ACM Computing Surveys (CSUR), Volume 11 ,Issue 2(June 1979).