Documente Academic
Documente Profesional
Documente Cultură
Variants
Advanced Data Structure
Spring 2007
Zareen Alamgir
Motivation
12 19 32 39 73 84
3 5 15 17 21 23 30 31 34 37 45 51 69 71 75 79 90 94
Multiway(m-way)
Search Trees
Multiway(m-way) Search
Trees
In an m-way tree all the nodes have degree ≤ m.
Each node has the following structure:
P1 K1 P2 Ki-1 Pi Ki Kq-1 Pq
12 23 32 73 84
3 5 15 21 30 31 69 71 90 94
17 19
M-way tree
B-Trees
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 (either key or entire data record is store in a node)
12 19 32 39 73 84
3 5 15 17 21 23 30 31 34 37 45 51 69 71 75 79 90 94
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 m / 2 ≤ q ≤ m .
Each leaf node holds q-1 keys where m / 2 ≤ q ≤ m .
All lea are on the same level.
It is clear that B-tree is always at least half full, has
fewer levels and is perfectly balanced.
P1
Fr K1 P2 Ki-1 Pi Ki Kq-1 Pq
1 key in the root + 2(q-1) keys on the second level +……+ 2qh-2(q-1) keys in
the leaves (level h).
m-1 keys in the root + m(m-1) keys on the second level +……+ mh-1(m-1) keys in
the leaf nodes
(m - 1) + m(m - 1) + m 2 (m - 1) + …… + m h-1 (m - 1)
h −1 h −1
= ∑ (m − 1)m = ( m − 1)∑ m i
i
i =0 i =0
12 19 32 39 73 84
3 5 15 17 21 23 30 31 34 37 45 51 69 71 75 79 90 94
Search key 71
B-Tree Insert Operation
Insertion in B-tree is more complicated than in BST.
yes
5 8 14 19 20 23 32 38
Insert 3 in order
B-Tree Insert Operation
Case 2: The leaf in which key is to be placed is full.
16
10 19
25
3 5 8 14 19 20 23 32 38
19
14 16 20 23
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
13 27 33 38 48 52 57 61 72 77 86 92
9 12 14 19 20 2 3 3 2 38 47 51 59 75 32 38 4 7 51 59 75 32 38 47 51 59 75 32 3 8 47 51 59 75 32 38 47 51 59 75
B-Tree Insert Operation
Case 2: The height of the tree increases.
Insert 16
Insert 27 in parent in order 55
No room for 27 in parent, Split node
16
45
5555 67 81
13 19
27 33 38
3 3 4 5 5 7 3 3 4 5 5 7 3 3 4 5 5 7 3 3 4 5 5 7
2 8 7 1 9 5 2 8 7 1 9 5 2 8 7 1 9 5 2 8 7 1 9 5
9 12 14 19 20 23 29 31 35 36 41 42
19
14 16 20 23
B-Tree Delete Operation
Deletion is analogous to insertion, but a little
more complicated.
Two major cases
Case 1: Deletion from leaf node
Case 2: Deletion from nonleaf node
Apply delete by copy technique used in BST, this will
reduce this case to case 1.
In delete by copy, 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
m
Redistribute: if number of keys in siblings > 2 − 1 .
Merge nodes if number of keys in siblings < m
2 − 1 .
Merging leads to decrease in tree height.
B-Tree Delete Operation
After deletion node is at least half full. (inverse of insertion
case 1)
Search key 3
10 25
3 5 8 14 19 32 38 40 45
Search key 14
Delete 14
10 25
5 8 14 19 32 38 40 45
5 8 19 25 38 40
Delete 21
70
Underflow occurs,
merge nodes
8 32 79 85
3 5 21 27 47 66 73 75 78 81 83 88 90 92
Index Set
15 21 34 45 75 90
3 5 15 17 21 23 30 31 34 37 45 51 69 71 75 79 90 94
B+-tree of order 5
B+-Tree Insert
Case: The leaf node has room for the key to be inserted.
5 8 14 19 20 23 32 38
B+-Tree Insert Operation
Case 2: The leaf in which key is to be placed is full.
16
10 19
25
3 5 8 14 19 20 23 32 38
14 16
19 20 23 19
18
No room for key 18, 18
Split node: create a new
sequence set node and
10 17 20 23 18
move m / 2 keys to the
new node.
Create a new index set node and
make it a root node.
Deletion cases:
After deletion node is at least half full.
After deletion underflow occurs
m
Redistribute: if number of keys in siblings > 2 − 1 .
Merge nodes if number of keys in siblings < m
2 − 1 .
Merging lead to decrease in tree height.
B+-Tree Delete
Case: After deletion node is at least half full.
Search key 14
Delete 14
Note: key 14 in the parent node is
14 32 still a valid separator key. It is not
modified.
3 5 8 14 19 21 32 38 40
Separator
Search key
key 14 in the parent
& delete it. is
Delete 14 no longer valid
14 38
32 Insert a copy of the first key of
the sibling node in parent in order.
5 8 14 19 32 38 40
5 8 19 32 38 40
45 55 67 81
13 27 33 38 48 52 57 61 72 77 86 92
In B*-tree split operation two nodes are split into three instead of
one into two as in B-tree.
Insert 22
22 32
10 12 15 16 21 24 25 29 35 42 47 51 53
Insert 72
72 32
10 12 15 16 21 24 25 29 35 42 47 51 53 55 57 59
Deletion cases:
After deletion node is at least two third full.
After deletion underflow occurs
2m − 1
Redistribute: if number of keys in siblings > .
3
2m − 1
Merge nodes if number of keys in siblings < .
3