Advanced Data Structures
(CS365)
m-way SearchTree
Dr. Sourav Kanti Addya
email: souravkaddya@nitk.edu.in
Department of Computer Science and Engineering
National Institute of Technology Karnataka, Surathkal
Dr. Sourav Kanti Addya || Dept of CSE, NITK Surathkal
m-Way Search Tree
 An m-way search tree T may be an empty tree.
 If T is non-empty, it satisfies the following properties:
(i) For some integer m known as the order of the tree, each node
has at most m child nodes.
(ii) A node may be represented as
A0 , (K1,A1), (K2,A2) …. (Km-1 ,Am-1 )
where Ki , 1 ≤ i ≤ m-1 are the keys and Ai, 0 ≤ i ≤ m-1 are the
pointers to the subtree of T
(iii) If the node has c child nodes where c ≤ m, then the node can
have only (c-1) keys, K1 , K2 , …… Kc-1
(iv) The keys in a node are ordered, i.e., K1<K2< …… <Kc-1
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
m-Way Search Tree
(v) For a node A0 , (K1 ,A1), (K2 ,A2) , …. (Km-1 ,Am-1 ), if Si is
the subtree pointed byAi, 0 ≤ i ≤ m-1then
 Key(S0)<K1
 Key(Sm-1)>Km-1
 Ki < Key(Si) < Ki+1 , 1 ≤ i ≤ m-2
A0 A1 A2 A3
< < >
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
m-Way Search Tree
(vi) Each of the subtreeAi , 0 ≤ i ≤ m-1 are also m-way search tree
m-Way SearchTree [ m=4]
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
m-Way Search Tree [ m=5]
18 44 76 198
X X
7 12
X X
80 92 141 262
8 10
148 151 172 186
X X X
X X X X X
X X X
77
X X
272 286 350
X X X X
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
Searching in an m-Way Search Tree
18 44 76 198
X X
7 12
X X
80 92 141 262
8 10
148 151 172 186
X X X
X X X X X
X X X
77
X X
272 286 350
X X X X
Look for 77
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
Insertion in an m-Way Search Tree
18 44 76 198
X X
7 12
X X
80 92 141 262
8 10
148 151 172 186
X X X
X X X X X
X X X
77
X X
272 286 350
X X X X
Insert 6
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
Insertion in an m-Way Search Tree
18 44 76 198
X X
7 12
X X
80 92 141 262
8 10
148 151 172 186
X X X
X X X X X
X X X
77
X X
272 286 350
X X X X
Insert 6
6
X
Insert 146
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
Insertion in an m-Way Search Tree
18 44 76 198
X X
7 12
X X
80 92 141 262
8 10
148 151 172 186
X X X
X X X X
X X X
77
X X
272 286 350
X X X X
Insert 6
6
X
Insert 146
146
X X
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
Deletion in an m-Way Search Tree
Let K be the key to be deleted from the m-way search tree.
K
Ai Aj
K : Key
Ai ,Aj : Pointers to subtree
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
Deletion in an m-Way Search Tree
1) If (Ai = Aj = NULL) then delete K
2) If (Ai  NULL, Aj = NULL ) then choose the largest of
the key elements K’ in the subtree pointed to by Ai and
replace K by K’.
3) If (Ai = NULL, Aj  NULL ) then choose the smallest of
the key element K” from the subtree pointed to by Aj ,
delete K” and replace K by K”.
4) If (Ai  NULL, Aj  NULL ) then choose the largest of
the key elements K’ in the subtree pointed to by Ai or the
smallest of the key element K” from the subtree pointed to
by Aj to replace K.
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
5-Way Search Tree
18 44 76 198
X X
7 12
X X
80 92 141 262
8 10
148 151 172 186
X X X
X X X X X
X X X
77
X X
272 286 350
X X X X
Delete 151
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
5-Way Search Tree
18 44 76 198
X X
7 12
X X
80 92 141 262
8 10
148 172 186
X X X
X X X X
X X X
77
X X
272 286 350
X X X X
Delete 151
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
5-Way Search Tree
18 44 76 198
X X
7 12
X X
80 92 141 262
8 10
148 151 172 186
X X X
X X X X X
X X X
77
X X
272 286 350
X X X X
Delete 12
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
5-Way Search Tree
18 44 76 198
X X
7 10
X X
80 92 141 262
8
148 151 172 186
X X X
X X X X X
X X
77
X X
272 286 350
X X X X
Delete 12
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
5-Way Search Tree
18 44 76 198
X X
7 12
X X
80 92 141 262
8 10
148 151 172 186
X X X
X X X X X
X X X
77
X X
272 286 350
X X X X
Delete 18
--Delete 12
--Replace 18 by 12
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
5-Way Search Tree
Delete 18
--Delete 12
--Replace 18 by 12
18 44 76 198
X X
7 10
X X
80 92 141 262
8
148 151 172 186
X X X
X X X X X
X X
77
X X
272 286 350
X X X X
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
5-Way Search Tree
Delete 18
--Delete 12
--Replace 18 by 12
12 44 76 198
X X
7 10
X X
80 92 141 262
8
148 151 172 186
X X X
X X X X X
X X
77
X X
272 286 350
X X X X
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
5-Way Search Tree
18 44 76 198
X X
7 12
X X
80 92 141 262
8 10
148 151 172 186
X X X
X X X X X
X X X
77
X X
272 286 350
X X X X
Delete 262
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
5-Way Search Tree
18 44 76 198
X X
7 12
X X
80 92 141 272
8 10
148 151 172 186
X X X
X X X X X
X X X
77
X X
286 350
X X X
Delete 262
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
5-Way Search Tree
18 44 76 198
X X
7 12
X X
80 92 141 262
8 10
148 151 172 186
X X X
X X X X X
X X X
77
X X
272 286 350
X X X X
Delete 198
--delete186
--replace 198 by 186
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
5-Way Search Tree
18 44 76 198
X X
7 12
X X
80 92 141 262
8 10
148 151 172
X X X
X X X X
X X X
77
X X
272 286 350
X X X X
Delete 198
--delete186
--replace 198 by 186
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
5-Way Search Tree
18 44 76 186
X X
7 12
X X
80 92 141 262
8 10
148 151 172
X X X
X X X X
X X X
77
X X
272 286 350
X X X X
Delete 198
--delete186
--replace 198 by 186
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
5-Way Search Tree
18 44 76 198
X X
7 12
X X
80 92 141 262
8 10
148 151 172 186
X X X
X X X X X
X X X
77
X X
272 286 350
X X X X
Delete 198
--delete 262
--replace 198 by 262
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
5-Way Search Tree
18 44 76 198
X X
7 12
X X
80 92 141 272
8 10
148 151 172 186
X X X
X X X X X
X X X
77
X X
286 350
X X X
Delete 198
--delete 262
--replace 198 by 262
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
5-Way Search Tree
18 44 76 262
X X
7 12
X X
80 92 141 272
8 10
148 151 172 186
X X X
X X X X X
X X X
77
X X
286 350
X X X
Delete 198
--delete 262
--replace 198 by 262
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
B Trees
B tree is a balanced m-way search tree
 A B tree of order m, if non empty, is an m-way
search tree in which
i. the root has at least two child nodes and at most m
child nodes
ii. internal nodes except the root have at least m/2 child
nodes and at most m child nodes
iii. all leaf nodes are on the same level
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
B Tree of order 5
48
31 45
56 64 85
87 88 100 112
X X X X X
49 51 52
X X X X
46 47
36 40 42
10 18 21
X X X X
X X X
X X X X
X X X
58 62
67 75
X X X
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
Searching a B Tree
 Searching for a key in a B-tree is similar to the
one on an m-way search tree.
 The number of accesses depends on the height h
of the B-tree
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
Insertion in a B-Tree
1. Attempt to insert the new key into a leaf
2. If this would result in that leaf becoming too big, split the
leaf into two, promoting the middle key to the leaf’s
parent
3. If this would result in the parent becoming too big, split
the parent into two, promoting the middle key
4. This strategy might have to be repeated all the way to the
top
5. If necessary, the root is split in two and the middle key is
promoted to a new root, making the tree one level higher
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
• Suppose we start with an empty B-tree and keys arrive in the
following order:1 12 8 2 25 6 14 28 17 7 52 16 48
68 3 26 29 53 55 45
• We want to construct a B-tree of order 5
• The first four items go into the root:
• To put the fifth item in the root would over-fill it
• Therefore, when 25 arrives, pick the middle key to make a
new root
Constructing a B-tree
12
8
1 2
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
Constructing a B-tree
Add 25 to the tree
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
12
8
1 2 25
Exceeds Order.
Promote middle and
split.
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
Constructing a B-tree (contd.)
6, 14, 28 get added to the leaf nodes:
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
12
8
1 2 25
12
8
1 2 25
6
1 2 28
14
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
Constructing a B-tree (contd.)
Adding 17 to the right leaf node would over-fill it, so we take
the middle key, promote it (to the root) and split the leaf
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
12
8
2 25
6
1 2 28
14 28
17
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
Constructing a B-tree (contd.)
7, 52, 16, 48 get added to the leaf nodes
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
12
8
25
6
1 2 28
14
17
7 52
16 48
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
Constructing a B-tree (contd.)
Adding 68 causes us to split the right most leaf,
promoting 48 to the root
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
8 17
7
6
2
1 16
14
12 52
48
28
25 68
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
Constructing a B-tree (contd.)
Adding 3 causes us to split the left most leaf
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
48
17
8
7
6
2
1 16
14
12 25 28 52 68
3 7
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
Constructing a B-tree (contd.)
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
Add 26, 29, 53, 55 then go into the leaves
48
17
8
3
1 2 6 7 52 68
25 28
16
14
12 26 29 53 55
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
Constructing a B-tree (contd.)
Add 45 increases the trees level
1
12
8
2
25
6
14
28
17
7
52
16
48
68
3
26
29
53
55
45
48
17
8
3
29
28
26
25 68
55
53
52
16
14
12
6 7
1 2 45
Exceeds Order.
Promote middle and
split.
Exceeds Order.
Promote middle and
split.
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
Exercise in Inserting a B-Tree
 Insert the following keys to a 5-way B-tree:
3, 7, 9, 23, 45, 1, 5, 14, 25, 24, 13, 11, 8, 19, 4, 31, 35, 56
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
Answer to Exercise
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
Delete from a B-tree
1. If the key is already in a leaf node, and removing
it doesn’t cause that leaf node to have too few
keys, then simply remove the key to be deleted.
2. If the key is not in a leaf then it is guaranteed (by
the nature of a B-tree) that its predecessor or
successor will be in a leaf -- in this case can we
delete the key and promote the predecessor or
successor key to the non-leaf deleted key’s
position.
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
Removal from a B-tree (2)
• If (1) or (2) lead to a leaf node containing less than the
minimum number of keys then we have to look at the siblings
immediately adjacent to the leaf in question:
– 3: if one of them has more than the min’ number of keys then
we can promote one of its keys to the parent and take the
parent key into our lacking leaf
– 4: if neither of them has more than the min’ number of keys
then the lacking leaf and one of its neighbours can be combined
with their shared parent (the opposite of promoting a key) and
the new leaf will have the correct number of keys; if this step
leave the parent with too few keys then we repeat the process
up to the root itself, if required
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
Type #1: Simple leaf deletion
12 29 52
2 7 9 15 22 56 69 72
31 43
Delete 2: Since there are enough
keys in the node, just delete it
Assuming a 5-way
B-Tree, as before...
Note when printed: this slide is animated
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
Type #2: Simple non-leaf deletion
12 29 52
7 9 15 22 56 69 72
31 43
Delete 52
Borrow the predecessor
or (in this case) successor
56
Note when printed: this slide is animated
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
Type #3: Enough siblings
12 29
7 9 15 22 69
56
31 43
Delete 22
Demote root key and
promote leaf key
Note when printed: this slide is animated
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
Type #3: Enough siblings
12
29
7 9 15
31
69
56
43
Note when printed: this slide is animated
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
Type #4: Too few keys in node and its
siblings
12 29 56
7 9 15 22 69 72
31 43
Delete 72
Too few keys!
Join back together
Note when printed: this slide is animated
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
Type #4: Too few keys in node and its
siblings
12 29
7 9 15 22 69
56
31 43
Note when printed: this slide is animated
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
Exercise in Removal from a B-Tree
 Given 5-way B-tree created by these data (last
exercise):
3, 7, 9, 23, 45, 1, 5, 14, 25, 24, 13, 11, 8, 19, 4, 31, 35, 56
 Add these further keys: 2, 6,12
 Delete these keys: 4, 5, 7, 3, 14
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
Answer to Exercise
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
5-Way B Tree (insertion examples)
8 96 116
2 7
X X X
104 110
37 46 55 86
X X X X X X X X
137 145
X X X
Insert 4, 5, 58, 6 in the order
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
5-Way B Tree (insertion examples)
8 96 116
104 110
37 46 55 86
X X X X X X X X
137 145
X X X
Search tree after inserting 4
2 4 7
X X X X
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
5-Way B Tree (insertion examples)
8 96 116
104 110
37 46 55 86
X X X X X X X X
137 145
X X X
Search tree after inserting 4, 5
2 4 5 7
X X X X X
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
5-Way B Tree (insertion examples)
8 96 116
104 110
37 46 55 86
X X X X X X X X
137 145
X X X
2 4 5 7
X X X X X
37,46,55,58,86
Split the node at its median into two node, pushing
the median element up by one level
Insert 58
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
5-Way B Tree (insertion examples)
104 110
X X X
137 145
X X X
2 4 5 7
X X X X X
37 46
X X X
58 86
X X X
8 96 116
Insert 55 in the root
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
5-Way B Tree (insertion examples)
104 110
8 55 96 116
X X X
137 145
X X X
Search tree after inserting 4, 5, 58
2 4 5 7
X X X X X
37 46
X X X
58 86
X X X
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
5-Way B Tree (insertion examples)
104 110
8 55 96 116
X X X
137 145
X X X
Insert 6
2 4 5 7
X X X X X
37 46
X X X
58 86
X X X
2,4,5,6,7
Split the node at its median into two node, pushing
the median element up by one level
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
5-Way B Tree (insertion examples)
104 110
8 55 96 116
X X X
137 145
X X X
Insert 5 at the root
37 46
X X X
58 86
X X X
6 7
2 4
X X X
X X X
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
5-Way B Tree (insertion examples)
104 110
X X X
137 145
X X X
Insert 5 at the root
37 46
X X X
58 86
X X X
6 7
2 4
X X X
X X X
96 116
5 8
55
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
5-Way B Tree (insertion examples)
104 110
X X X
137 145
X X X
Insert 5 at the root
37 46
X X X
58 86
X X X
6 7
2 4
X X X
X X X
96 116
5 8
55
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
B-tree of Order 5 (deletion examples)
110
65 86 120 226
70 81
32 44
X X X
90 95 100
X X X
X X X
115 118
200 221
X X X
X X X X 300 440 550 601
X X X X X
Delete 95, 226, 221, 70
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
B-tree of Order 5
110
65 86 120 226
70 81
32 44
X X X
90 100
X X X
X X X
115 118
200 221
X X X
X X X 300 440 550 601
X X X X X
B-tree after deleting 95
delete 226
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
B-tree of Order 5
110
65 86 120 300
70 81
32 44
X X X
90 100
X X X
X X X
115 118
200 221
X X X
X X X 300 440 550 601
X X X X X
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
B-tree of Order 5
110
65 86 120 300
70 81
32 44
X X X
90 100
X X X
X X X
115 118
200 221
X X X
X X X 440 550 601
X X X X
B-tree after deleting 95, 226
Delete 221
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
B-tree of Order 5
110
65 86 120 440
70 81
32 44
X X X
90 100
X X X
X X X
115 118
200 300
X X X
X X X 550 601
X X X
B-tree after deleting 95, 226, 221
Delete 70
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
B-tree of Order 5
110
65 86 120 440
65 81
32 44
X X X
90 100
X X
X X X
115 118
200 300
X X X
X X X 550 601
X X X
Delete 65
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
B-tree of Order 5
110
86 120 440
65 81
32 44
X X X
90 100
X X
X X X
115 118
200 300
X X X
X X X 550 601
X X X
B-tree after deleting 95, 226, 221, 70
Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal

8_m-way tree_Btree data structures and algorithms

  • 1.
    Advanced Data Structures (CS365) m-waySearchTree Dr. Sourav Kanti Addya email: souravkaddya@nitk.edu.in Department of Computer Science and Engineering National Institute of Technology Karnataka, Surathkal
  • 2.
    Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 3.
    m-Way Search Tree An m-way search tree T may be an empty tree.  If T is non-empty, it satisfies the following properties: (i) For some integer m known as the order of the tree, each node has at most m child nodes. (ii) A node may be represented as A0 , (K1,A1), (K2,A2) …. (Km-1 ,Am-1 ) where Ki , 1 ≤ i ≤ m-1 are the keys and Ai, 0 ≤ i ≤ m-1 are the pointers to the subtree of T (iii) If the node has c child nodes where c ≤ m, then the node can have only (c-1) keys, K1 , K2 , …… Kc-1 (iv) The keys in a node are ordered, i.e., K1<K2< …… <Kc-1 Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 4.
    m-Way Search Tree (v)For a node A0 , (K1 ,A1), (K2 ,A2) , …. (Km-1 ,Am-1 ), if Si is the subtree pointed byAi, 0 ≤ i ≤ m-1then  Key(S0)<K1  Key(Sm-1)>Km-1  Ki < Key(Si) < Ki+1 , 1 ≤ i ≤ m-2 A0 A1 A2 A3 < < > Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 5.
    m-Way Search Tree (vi)Each of the subtreeAi , 0 ≤ i ≤ m-1 are also m-way search tree m-Way SearchTree [ m=4] Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 6.
    m-Way Search Tree[ m=5] 18 44 76 198 X X 7 12 X X 80 92 141 262 8 10 148 151 172 186 X X X X X X X X X X X 77 X X 272 286 350 X X X X Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 7.
    Searching in anm-Way Search Tree 18 44 76 198 X X 7 12 X X 80 92 141 262 8 10 148 151 172 186 X X X X X X X X X X X 77 X X 272 286 350 X X X X Look for 77 Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 8.
    Insertion in anm-Way Search Tree 18 44 76 198 X X 7 12 X X 80 92 141 262 8 10 148 151 172 186 X X X X X X X X X X X 77 X X 272 286 350 X X X X Insert 6 Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 9.
    Insertion in anm-Way Search Tree 18 44 76 198 X X 7 12 X X 80 92 141 262 8 10 148 151 172 186 X X X X X X X X X X X 77 X X 272 286 350 X X X X Insert 6 6 X Insert 146 Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 10.
    Insertion in anm-Way Search Tree 18 44 76 198 X X 7 12 X X 80 92 141 262 8 10 148 151 172 186 X X X X X X X X X X 77 X X 272 286 350 X X X X Insert 6 6 X Insert 146 146 X X Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 11.
    Deletion in anm-Way Search Tree Let K be the key to be deleted from the m-way search tree. K Ai Aj K : Key Ai ,Aj : Pointers to subtree Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 12.
    Deletion in anm-Way Search Tree 1) If (Ai = Aj = NULL) then delete K 2) If (Ai  NULL, Aj = NULL ) then choose the largest of the key elements K’ in the subtree pointed to by Ai and replace K by K’. 3) If (Ai = NULL, Aj  NULL ) then choose the smallest of the key element K” from the subtree pointed to by Aj , delete K” and replace K by K”. 4) If (Ai  NULL, Aj  NULL ) then choose the largest of the key elements K’ in the subtree pointed to by Ai or the smallest of the key element K” from the subtree pointed to by Aj to replace K. Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 13.
    5-Way Search Tree 1844 76 198 X X 7 12 X X 80 92 141 262 8 10 148 151 172 186 X X X X X X X X X X X 77 X X 272 286 350 X X X X Delete 151 Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 14.
    5-Way Search Tree 1844 76 198 X X 7 12 X X 80 92 141 262 8 10 148 172 186 X X X X X X X X X X 77 X X 272 286 350 X X X X Delete 151 Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 15.
    5-Way Search Tree 1844 76 198 X X 7 12 X X 80 92 141 262 8 10 148 151 172 186 X X X X X X X X X X X 77 X X 272 286 350 X X X X Delete 12 Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 16.
    5-Way Search Tree 1844 76 198 X X 7 10 X X 80 92 141 262 8 148 151 172 186 X X X X X X X X X X 77 X X 272 286 350 X X X X Delete 12 Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 17.
    5-Way Search Tree 1844 76 198 X X 7 12 X X 80 92 141 262 8 10 148 151 172 186 X X X X X X X X X X X 77 X X 272 286 350 X X X X Delete 18 --Delete 12 --Replace 18 by 12 Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 18.
    5-Way Search Tree Delete18 --Delete 12 --Replace 18 by 12 18 44 76 198 X X 7 10 X X 80 92 141 262 8 148 151 172 186 X X X X X X X X X X 77 X X 272 286 350 X X X X Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 19.
    5-Way Search Tree Delete18 --Delete 12 --Replace 18 by 12 12 44 76 198 X X 7 10 X X 80 92 141 262 8 148 151 172 186 X X X X X X X X X X 77 X X 272 286 350 X X X X Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 20.
    5-Way Search Tree 1844 76 198 X X 7 12 X X 80 92 141 262 8 10 148 151 172 186 X X X X X X X X X X X 77 X X 272 286 350 X X X X Delete 262 Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 21.
    5-Way Search Tree 1844 76 198 X X 7 12 X X 80 92 141 272 8 10 148 151 172 186 X X X X X X X X X X X 77 X X 286 350 X X X Delete 262 Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 22.
    5-Way Search Tree 1844 76 198 X X 7 12 X X 80 92 141 262 8 10 148 151 172 186 X X X X X X X X X X X 77 X X 272 286 350 X X X X Delete 198 --delete186 --replace 198 by 186 Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 23.
    5-Way Search Tree 1844 76 198 X X 7 12 X X 80 92 141 262 8 10 148 151 172 X X X X X X X X X X 77 X X 272 286 350 X X X X Delete 198 --delete186 --replace 198 by 186 Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 24.
    5-Way Search Tree 1844 76 186 X X 7 12 X X 80 92 141 262 8 10 148 151 172 X X X X X X X X X X 77 X X 272 286 350 X X X X Delete 198 --delete186 --replace 198 by 186 Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 25.
    5-Way Search Tree 1844 76 198 X X 7 12 X X 80 92 141 262 8 10 148 151 172 186 X X X X X X X X X X X 77 X X 272 286 350 X X X X Delete 198 --delete 262 --replace 198 by 262 Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 26.
    5-Way Search Tree 1844 76 198 X X 7 12 X X 80 92 141 272 8 10 148 151 172 186 X X X X X X X X X X X 77 X X 286 350 X X X Delete 198 --delete 262 --replace 198 by 262 Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 27.
    5-Way Search Tree 1844 76 262 X X 7 12 X X 80 92 141 272 8 10 148 151 172 186 X X X X X X X X X X X 77 X X 286 350 X X X Delete 198 --delete 262 --replace 198 by 262 Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 28.
    B Trees B treeis a balanced m-way search tree  A B tree of order m, if non empty, is an m-way search tree in which i. the root has at least two child nodes and at most m child nodes ii. internal nodes except the root have at least m/2 child nodes and at most m child nodes iii. all leaf nodes are on the same level Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 29.
    B Tree oforder 5 48 31 45 56 64 85 87 88 100 112 X X X X X 49 51 52 X X X X 46 47 36 40 42 10 18 21 X X X X X X X X X X X X X X 58 62 67 75 X X X Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 30.
    Searching a BTree  Searching for a key in a B-tree is similar to the one on an m-way search tree.  The number of accesses depends on the height h of the B-tree Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 31.
    Insertion in aB-Tree 1. Attempt to insert the new key into a leaf 2. If this would result in that leaf becoming too big, split the leaf into two, promoting the middle key to the leaf’s parent 3. If this would result in the parent becoming too big, split the parent into two, promoting the middle key 4. This strategy might have to be repeated all the way to the top 5. If necessary, the root is split in two and the middle key is promoted to a new root, making the tree one level higher Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 32.
    Dr. Sourav KantiAddya|| Dept of CSE, NITK Surathkal
  • 33.
    • Suppose westart with an empty B-tree and keys arrive in the following order:1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45 • We want to construct a B-tree of order 5 • The first four items go into the root: • To put the fifth item in the root would over-fill it • Therefore, when 25 arrives, pick the middle key to make a new root Constructing a B-tree 12 8 1 2 Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 34.
    Constructing a B-tree Add25 to the tree 1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45 12 8 1 2 25 Exceeds Order. Promote middle and split. Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 35.
    Constructing a B-tree(contd.) 6, 14, 28 get added to the leaf nodes: 1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45 12 8 1 2 25 12 8 1 2 25 6 1 2 28 14 Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 36.
    Constructing a B-tree(contd.) Adding 17 to the right leaf node would over-fill it, so we take the middle key, promote it (to the root) and split the leaf 1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45 1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45 12 8 2 25 6 1 2 28 14 28 17 Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 37.
    Constructing a B-tree(contd.) 7, 52, 16, 48 get added to the leaf nodes 1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45 12 8 25 6 1 2 28 14 17 7 52 16 48 Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 38.
    Constructing a B-tree(contd.) Adding 68 causes us to split the right most leaf, promoting 48 to the root 1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45 8 17 7 6 2 1 16 14 12 52 48 28 25 68 Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 39.
    Constructing a B-tree(contd.) Adding 3 causes us to split the left most leaf 1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45 48 17 8 7 6 2 1 16 14 12 25 28 52 68 3 7 Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 40.
    Constructing a B-tree(contd.) 1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45 Add 26, 29, 53, 55 then go into the leaves 48 17 8 3 1 2 6 7 52 68 25 28 16 14 12 26 29 53 55 Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 41.
    Constructing a B-tree(contd.) Add 45 increases the trees level 1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45 48 17 8 3 29 28 26 25 68 55 53 52 16 14 12 6 7 1 2 45 Exceeds Order. Promote middle and split. Exceeds Order. Promote middle and split. Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 42.
    Exercise in Insertinga B-Tree  Insert the following keys to a 5-way B-tree: 3, 7, 9, 23, 45, 1, 5, 14, 25, 24, 13, 11, 8, 19, 4, 31, 35, 56 Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 43.
    Answer to Exercise Dr.Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 44.
    Delete from aB-tree 1. If the key is already in a leaf node, and removing it doesn’t cause that leaf node to have too few keys, then simply remove the key to be deleted. 2. If the key is not in a leaf then it is guaranteed (by the nature of a B-tree) that its predecessor or successor will be in a leaf -- in this case can we delete the key and promote the predecessor or successor key to the non-leaf deleted key’s position. Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 45.
    Removal from aB-tree (2) • If (1) or (2) lead to a leaf node containing less than the minimum number of keys then we have to look at the siblings immediately adjacent to the leaf in question: – 3: if one of them has more than the min’ number of keys then we can promote one of its keys to the parent and take the parent key into our lacking leaf – 4: if neither of them has more than the min’ number of keys then the lacking leaf and one of its neighbours can be combined with their shared parent (the opposite of promoting a key) and the new leaf will have the correct number of keys; if this step leave the parent with too few keys then we repeat the process up to the root itself, if required Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 46.
    Type #1: Simpleleaf deletion 12 29 52 2 7 9 15 22 56 69 72 31 43 Delete 2: Since there are enough keys in the node, just delete it Assuming a 5-way B-Tree, as before... Note when printed: this slide is animated Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 47.
    Type #2: Simplenon-leaf deletion 12 29 52 7 9 15 22 56 69 72 31 43 Delete 52 Borrow the predecessor or (in this case) successor 56 Note when printed: this slide is animated Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 48.
    Type #3: Enoughsiblings 12 29 7 9 15 22 69 56 31 43 Delete 22 Demote root key and promote leaf key Note when printed: this slide is animated Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 49.
    Type #3: Enoughsiblings 12 29 7 9 15 31 69 56 43 Note when printed: this slide is animated Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 50.
    Type #4: Toofew keys in node and its siblings 12 29 56 7 9 15 22 69 72 31 43 Delete 72 Too few keys! Join back together Note when printed: this slide is animated Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 51.
    Type #4: Toofew keys in node and its siblings 12 29 7 9 15 22 69 56 31 43 Note when printed: this slide is animated Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 52.
    Exercise in Removalfrom a B-Tree  Given 5-way B-tree created by these data (last exercise): 3, 7, 9, 23, 45, 1, 5, 14, 25, 24, 13, 11, 8, 19, 4, 31, 35, 56  Add these further keys: 2, 6,12  Delete these keys: 4, 5, 7, 3, 14 Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 53.
    Answer to Exercise Dr.Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 54.
    5-Way B Tree(insertion examples) 8 96 116 2 7 X X X 104 110 37 46 55 86 X X X X X X X X 137 145 X X X Insert 4, 5, 58, 6 in the order Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 55.
    5-Way B Tree(insertion examples) 8 96 116 104 110 37 46 55 86 X X X X X X X X 137 145 X X X Search tree after inserting 4 2 4 7 X X X X Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 56.
    5-Way B Tree(insertion examples) 8 96 116 104 110 37 46 55 86 X X X X X X X X 137 145 X X X Search tree after inserting 4, 5 2 4 5 7 X X X X X Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 57.
    5-Way B Tree(insertion examples) 8 96 116 104 110 37 46 55 86 X X X X X X X X 137 145 X X X 2 4 5 7 X X X X X 37,46,55,58,86 Split the node at its median into two node, pushing the median element up by one level Insert 58 Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 58.
    5-Way B Tree(insertion examples) 104 110 X X X 137 145 X X X 2 4 5 7 X X X X X 37 46 X X X 58 86 X X X 8 96 116 Insert 55 in the root Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 59.
    5-Way B Tree(insertion examples) 104 110 8 55 96 116 X X X 137 145 X X X Search tree after inserting 4, 5, 58 2 4 5 7 X X X X X 37 46 X X X 58 86 X X X Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 60.
    5-Way B Tree(insertion examples) 104 110 8 55 96 116 X X X 137 145 X X X Insert 6 2 4 5 7 X X X X X 37 46 X X X 58 86 X X X 2,4,5,6,7 Split the node at its median into two node, pushing the median element up by one level Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 61.
    5-Way B Tree(insertion examples) 104 110 8 55 96 116 X X X 137 145 X X X Insert 5 at the root 37 46 X X X 58 86 X X X 6 7 2 4 X X X X X X Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 62.
    5-Way B Tree(insertion examples) 104 110 X X X 137 145 X X X Insert 5 at the root 37 46 X X X 58 86 X X X 6 7 2 4 X X X X X X 96 116 5 8 55 Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 63.
    5-Way B Tree(insertion examples) 104 110 X X X 137 145 X X X Insert 5 at the root 37 46 X X X 58 86 X X X 6 7 2 4 X X X X X X 96 116 5 8 55 Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 64.
    B-tree of Order5 (deletion examples) 110 65 86 120 226 70 81 32 44 X X X 90 95 100 X X X X X X 115 118 200 221 X X X X X X X 300 440 550 601 X X X X X Delete 95, 226, 221, 70 Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 65.
    B-tree of Order5 110 65 86 120 226 70 81 32 44 X X X 90 100 X X X X X X 115 118 200 221 X X X X X X 300 440 550 601 X X X X X B-tree after deleting 95 delete 226 Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 66.
    B-tree of Order5 110 65 86 120 300 70 81 32 44 X X X 90 100 X X X X X X 115 118 200 221 X X X X X X 300 440 550 601 X X X X X Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 67.
    B-tree of Order5 110 65 86 120 300 70 81 32 44 X X X 90 100 X X X X X X 115 118 200 221 X X X X X X 440 550 601 X X X X B-tree after deleting 95, 226 Delete 221 Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 68.
    B-tree of Order5 110 65 86 120 440 70 81 32 44 X X X 90 100 X X X X X X 115 118 200 300 X X X X X X 550 601 X X X B-tree after deleting 95, 226, 221 Delete 70 Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 69.
    B-tree of Order5 110 65 86 120 440 65 81 32 44 X X X 90 100 X X X X X 115 118 200 300 X X X X X X 550 601 X X X Delete 65 Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal
  • 70.
    B-tree of Order5 110 86 120 440 65 81 32 44 X X X 90 100 X X X X X 115 118 200 300 X X X X X X 550 601 X X X B-tree after deleting 95, 226, 221, 70 Dr. Sourav KantiAddya || Dept of CSE, NITK Surathkal