10.m way search tree
TRANSCRIPT
![Page 1: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/1.jpg)
1
Data Structure and Algorithm (CS 102)
Ashok K Turuk
![Page 2: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/2.jpg)
2
m-Way Search TreeAn 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. 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
![Page 3: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/3.jpg)
3
m-Way Search Tree[2] If the node has k child nodes where k<=m,
then the node can have only (k-1) keys, K1 , K2 , …… Kk-1 contained in the node such that Ki < Ki+1 and each of the keys partitions all the keys in the subtrees into k subsets
[3] For a node A0 , (K1 , A1), (K2 , A2) , …. (Km-
1 , Am-1 ) all key values in the subtree pointed to by Ai are less than the key Ki+1 , 0<=i<=m-2 and all key values in the subtree pointed to by Am-1 are greater than Km-1
![Page 4: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/4.jpg)
4
m-Way Search Tree[4] Each of the subtree Ai , 0<=i<=m-
1 are also m-way search tree
![Page 5: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/5.jpg)
5
m-Way Search Tree [ m=5] 18 44 76 19
8X X
7 12X X
80 92 141
262
8 10 148
151
172
186
X X X
X X X X XX X X
77X X
272
286
350X X X X
![Page 6: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/6.jpg)
6
Searching in an m-Way Search Tree
18 44 76 198X X
7 12X X
80 92 141
262
8 10 148
151
172
186
X X X
X X X X XX X X
77X X
272
286
350X X X X
Look for 77
![Page 7: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/7.jpg)
7
Insertion in an m-Way Search Tree
18 44 76 198X X
7 12X X
80 92 141
262
8 10 148
151
172
186
X X X
X X X X XX X X
77X X
272
286
350X X X X
Insert 6
![Page 8: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/8.jpg)
8
Insertion in an m-Way Search Tree
18 44 76 198X X
7 12X X
80 92 141
262
8 10 148
151
172
186
X X X
X X X XX X X
77X X
272
286
350X X X X
Insert 6
6X
Insert 146
146X
![Page 9: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/9.jpg)
9
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 : KeyAi , Aj : Pointers to subtree
![Page 10: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/10.jpg)
10
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 child node 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”.
![Page 11: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/11.jpg)
11
Deletion in an m-Way Search Tree
[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.
![Page 12: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/12.jpg)
12
5-Way Search Tree18 44 76 19
8X X
7 12X X
80 92 141
262
8 10 148
151
172
186
X X X
X X X X XX X X
77X X
272
286
350X X X X
Delete 151
![Page 13: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/13.jpg)
13
5-Way Search Tree18 44 76 19
8X X
7 12X X
80 92 141
262
8 10 148
172
186
X X X
X X X XX X X
77X X
272
286
350X X X X
Delete 151
![Page 14: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/14.jpg)
14
5-Way Search Tree18 44 76 19
8X X
7 12X X
80 92 141
262
8 10 148
151
172
186
X X X
X X X X XX X X
77X X
272
286
350X X X X
Delete 262
![Page 15: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/15.jpg)
15
5-Way Search Tree18 44 76 19
8X X
7 12X X
80 92 141
272
8 10 148
151
172
186
X X X
X X X X XX X X
77X X
286
350X X X
Delete 262
![Page 16: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/16.jpg)
16
5-Way Search Tree18 44 76 19
8X X
7 12X X
80 92 141
262
8 10 148
151
172
186
X X X
X X X X XX X X
77X X
272
286
350X X X X
Delete 12
![Page 17: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/17.jpg)
17
5-Way Search Tree18 44 76 19
8X X
7 10X X
80 92 141
262
8 148
151
172
186
X X X
X X X X XX X
77X X
272
286
350X X X X
Delete 12
![Page 18: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/18.jpg)
18
B TreesB 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
![Page 19: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/19.jpg)
19
B Trees[iii] the number of keys in each internal
node is one less than the number of child nodes and these keys partition the keys in the subtrees of nodes in a manner similar to that of m-way search trees
[iv] all leaf nodes are on the same level
![Page 20: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/20.jpg)
20
B Tree of order 5
48
31 4556 64 85
87 88 100
112X X X X X
49 51 52X X X X
46 47
36 40 42
10 18 21X X X X
X X X
X X X X
X X X
58 62
67 75X X X
![Page 21: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/21.jpg)
21
Searching a B TreeSearching 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
![Page 22: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/22.jpg)
22
Insertion in a B-TreeA key is inserted according to the
following procedure[1] If the leaf node in which the key is
to be inserted is not full, then the insertion is done in the node.
A node is said to be full if it contains a maximum of (m-1) keys given the order of the B-tree to be m
![Page 23: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/23.jpg)
23
Insertion in a B-Tree[2] If the node were to be full then insert the
key in order into the existing set of keys in the node. Split the node at its median into two nodes at the same level, pushing the median element up by one level. Accommodate the median element in the parent node if it is not full. Otherwise repeat the same procedure and this may call for rearrangement of the keys in the root node or the formation of new root itself.
![Page 24: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/24.jpg)
5-Way Search Tree8 96 11
6
2 7X X X
104
110
37 46 55 86X X X X X X X X
137
145X X X
Insert 4, 5, 58, 6 in the order
![Page 25: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/25.jpg)
5-Way Search Tree8 96 11
6
104
110
37 46 55 86X X X X X X X X
137
145X X X
Search tree after inserting 4
2 4 7X X X X
![Page 26: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/26.jpg)
5-Way Search Tree8 96 11
6
104
110
37 46 55 86X X X X X X X X
137
145X X X
Search tree after inserting 4, 5
2 4 5 7X X X X X
![Page 27: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/27.jpg)
5-Way Search Tree8 96 11
6
104
110
37 46 55 86X X X X X X X X
137
145X X X
2 4 5 7X X X X X
37,46,55,58,86Split the node at its median into two node, pushing the median element up by one level
![Page 28: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/28.jpg)
5-Way Search Tree
104
110X X X
137
145X X X
2 4 5 7X X X X X
37 46X X X
58 86X X X
8 96 116
Insert 55 in the root
![Page 29: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/29.jpg)
5-Way Search Tree
104
110
8 55 96 116
X X X
137
145X X X
Search tree after inserting 4, 5, 58
2 4 5 7X X X X X
37 46X X X
58 86X X X
![Page 30: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/30.jpg)
5-Way Search Tree
104
110
8 55 96 116
X X X
137
145X X X
Insert 6
2 4 5 7X X X X X
37 46X X X
58 86X X X
2,4,5,6,7Split the node at its median into two node, pushing the median element up by one level
![Page 31: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/31.jpg)
5-Way Search Tree
104
110
8 55 96 116
X X X
137
145X X X
Insert 5 at the root
37 46X X X
58 86X X X6 72 4
X X XX X X
![Page 32: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/32.jpg)
5-Way Search Tree
104
110X X X
137
145X X X
Insert 5 at the root
37 46X X X
58 86X X X6 72 4
X X XX X X
96 116
5 8
55
![Page 33: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/33.jpg)
5-Way Search Tree
104
110X X X
137
145X X X
Insert 5 at the root
37 46X X X
58 86X X X
6 7
2 4
X X X
X X X
96 1165 8
55
![Page 34: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/34.jpg)
34
Deletion in a B-TreeIt is desirable that a key in leaf node
be removed.
When a key in an internal node to be deleted, then we promote a successor or a predecessor of the key to be deleted ,to occupy the position of the deleted key and such a key is bound to occur in a leaf node.
![Page 35: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/35.jpg)
35
Deletion in a B-TreeRemoving a key from leaf node:If the node contain more than the
minimum number of elements, then the key can be easily removed.
If the leaf node contain just the minimum number of elements, then look for an element either from the left sibling node or right sibling node to fill the vacancy.
![Page 36: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/36.jpg)
36
Deletion in a B-TreeIf the left sibling has more than minimum
number of keys, pull the largest key up into the parent node and move down the intervening entry from the parent node to the leaf node where key is deleted.
Otherwise, pull the smallest key of the right sibling node to the parent node and move down the intervening parent element to the leaf node.
![Page 37: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/37.jpg)
37
Deletion in a B-TreeIf both the sibling node has minimum number of
entries, then create a new leaf node out of the two leaf nodes and the intervening element of the parent node, ensuring the total number does not exceed the maximum limit for a node.
If while borrowing the intervening element from the parent node, it leaves the number of keys in the parent node to be below the minimum number, then we propagate the process upwards ultimately resulting in a reduction of the height of B-tree
![Page 38: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/38.jpg)
38
B-tree of Order 5110
65
86
120
226
70
81
32
44 X X X
90 95
100
X X XX X X
115
118 20
0221X X X
X X X X 300
440 550 601X X X X X
Delete 95, 226, 221, 70
![Page 39: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/39.jpg)
39
B-tree of Order 5110
65
86
120
226
70
81
32
44 X X X
90 100
X X XX X X
115
118 20
0221X X X
X X X 300
440 550 601X X X X X
B-tree after deleting 95
![Page 40: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/40.jpg)
40
B-tree of Order 5110
65
86
120
300
70
81
32
44 X X X
90 100
X X XX X X
115
118 20
0221X X X
X X X 300
440 550 601X X X X X
![Page 41: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/41.jpg)
41
B-tree of Order 5110
65
86
120
300
70
81
32
44 X X X
90 100
X X XX X X
115
118 20
0221X X X
X X X 440 550 601X X X X
B-tree after deleting 95, 226
Delete 221
![Page 42: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/42.jpg)
42
B-tree of Order 5110
65
86
120
440
70
81
32
44 X X X
90 100
X X XX X X
115
118 20
0300X X X
X X X 550 601X X X
B-tree after deleting 95, 226, 221
Delete 70
![Page 43: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/43.jpg)
43
B-tree of Order 5110
65
86
120
440
65
81
32
44 X X X
90 100
X XX X X
115
118 20
0300X X X
X X X 550 601X X X
Delete 65
![Page 44: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/44.jpg)
44
B-tree of Order 5110
86
120
440
65
81
32
44 X X X
90 100
X XX X X
115
118 20
0300X X X
X X X 550 601X X X
B-tree after deleting 95, 226, 221, 70
![Page 45: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/45.jpg)
45
Heap Suppose H is a complete binary tree
with n elements
H is called a heap or maxheap if each node N of H has the following property
Value at N is greater than or equal to the value at each of the children of N.
![Page 46: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/46.jpg)
46
Heap 97
88 95
66 55
66 35
18 40 30 26
48
24
55
95
62 77
48
25 38
97
88
95
66
55
95
48
66
35
48
55
62
77
25
38
18
40
30
26
24
1 2 3 4 5 6 7 8 9 10
11
12
13
14
15
16
17
18
19
20
![Page 47: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/47.jpg)
47
Inserting into a HeapSuppose H is a heap with N elementsSuppose an ITEM of information is given.
Insertion of ITEM into heap H is given as follows:
[1] First adjoin ITEM at the end of H so that H is still a complete tree, but necessarily a heap
[2] Let ITEM rise to its appropriate place in H so that H is finally a heap
![Page 48: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/48.jpg)
48
Heap 97
88 95
66 55
66 35
18 40 30 26
48
24
55
95
62 77
48
25 38
97
88
95
66
55
95
48
66
35
48
55
62
77
25
38
18
40
30
26
24
1 2 3 4 5 6 7 8 9 10
11
12
13
14
15
16
17
18
19
20
Insert 70
70
![Page 49: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/49.jpg)
49
Heap 97
88 95
66 55
66 35
18 40 30 26
48
24
55
95
62 77
48
25 38
97
88
95
66
55
95
48
66
35
48
55
62
77
25
38
18
40
30
26
24
1 2 3 4 5 6 7 8 9 10
11
12
13
14
15
16
17
18
19
20
Insert 70
70
![Page 50: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/50.jpg)
50
Heap 97
88 95
66 55
66 35
18 40 30 26
70
24
55
95
62 77
48
25 38
97
88
95
66
55
95
48
66
35
48
55
62
77
25
38
18
40
30
26
24
1 2 3 4 5 6 7 8 9 10
11
12
13
14
15
16
17
18
19
20
Insert 70
48
![Page 51: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/51.jpg)
51
Heap 97
88 95
66 70
66 35
18 40 30 26
55
24
55
95
62 77
48
25 38
97
88
95
66
55
95
48
66
35
48
55
62
77
25
38
18
40
30
26
24
1 2 3 4 5 6 7 8 9 10
11
12
13
14
15
16
17
18
19
20
Insert 70
48
![Page 52: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/52.jpg)
52
Build a HeapBuild a heap from the following list
44, 30, 50, 22, 60, 55, 77, 55
![Page 53: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/53.jpg)
53
4444, 30, 50, 22, 60, 55, 77, 55
44
30
44
30 5050
30 4450
30 44
22
Complete the Rest Insertion 77
55 60
50 30
2244 55
![Page 54: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/54.jpg)
54
Deleting the Root of a Heap Suppose H is a heap with N elementsSuppose we want to delete the root R of HDeletion of root is accomplished as follows[1] Assign the root R to some variable ITEM[2] Replace the deleted node R by the last
node L of H so that H is still a complete tree but necessarily a heap
[3] Reheap. Let L sink to its appropriate place in H so that H is finally a heap.
![Page 55: 10.m way search tree](https://reader036.vdocuments.us/reader036/viewer/2022062523/58737a511a28ab3c1a8b7097/html5/thumbnails/55.jpg)
55
9585 70
55 33
1530 65
20 15 22
2285 70
55 33
1530 65
20 15
8522 70
55 33
1530 65
20 15
8555 70
22 33
1530 65
20 15