multi-way trees. m-way trees so far we have discussed binary trees only. in this lecture, we go over...
TRANSCRIPT
Multi-way
Trees
M-way trees
• So far we have discussed binary trees only.
• In this lecture, we go over another type of tree called m-way trees or trees of order m.
• In a binary tree • Each node has only one key and • Each node has up to two children
• In a m-way tree• Each node hold at least 1 and at most m-1 keys and • Each node has at most m children
B-Tree• An example of m-way tree is called B-trees• A B-tree of order m is a multiway search tree with the following
properties
• The root has at least two subtrees unless it is a leaf
• Except the root and the leaves, every other node has at most m children and at least m/2 children
• This means every node hold at most m-1 keys and at least (m/2) -1 keys
• For example, a tree of order m=5, holds at least 2 and at most 4 keys and 5 pointers.
• Similarly, a tree of order m=8, hold at least 3 and at most 7 keys in each node and has 8 pointers.
• Every leaf node holds at most m-1 keys and at least (m/2) -1 keys
• All leaves are on the same level
Inserting a key into a B-Tree
• Some of the differences of B-trees compare to binary trees is that:
• All leaves are in the last level of the tree. Note that this was not necessarily the case for the binary tree
• The tree is built bottom-up rather than up to bottom as it is in binary trees
• In general there are three cases to consider when we insert a element into a B-tree of order m.
Insertion into B Tree: Case 1:
• A key is placed in a leaf that still has some room
• For example, as shown in the following example, in a B-tree of order 5, a new key, 7, is placed in a leaf, preserving the order of keys in the leaf so that key 8 must be shifted to the right by one position
12
1385 15
12
1375 158
Insertion into B Tree: Case 2:
• The leaf in which a key should be placed is full.
• In this case the leaf is split, creating a new leaf, and half of the keys are moved from the leaf full node to the new leaf.
• The last key of the old leaf is moved to the parent and a pointer to the new leaf is placed in the parent as well.
12
1352 15
12
52 87 1513
126
52 87 13 15
6
move
7 8
Want to insert 6
Insertion into B Tree: Case 3:• Suppose you want to insert a key into a full node
• Because the node is full, the node is split and the middle key is moved to the parent node as we explained in case 2.
• What if the parent has no more room?
• If parent has no more room, we need to split the parent node, create two nodes and move the middle key up the tree into the parent of the parent.
• If parent of the parent does not exist, we create one
• If parent of the parent has room , we insert the middle key there
• If parent of the parent is also full, we repeat the same process again
6 12 20 30
2 3 4 5 7 8 10 11 14 15 18 19 21 23 25 28 31 33 34 35
6 12 20 30
2 3 4 5 7 8 10 11 13 14 15 21 23 25 28 31 33 34 3518 19
Move
6 12
2 3 4 5 7 8 10 11 13 14 21 23 25 28 31 33 34 3518 19
Insert 13
15
20 30
Algorithm for inserting into B-TreeBTreeInsert(K)Find a leaf node to insert
While (true){
Find a proper position in the leaf for K;
If there is space in that node Insert K in proper positionReturn
Else split in node in node1 and node2
Distribute keys and pointers evenly between node1 and node2K = the last key of node1If node was the root
Create a new root as parent of node1 and node2Put K and pointers to node1 and node2 in the root Return
Else node = its parent // now process the parent node if it is full
}
After inserting 3
8
14 15 2 3
2 8 14 15
After inserting 8, 14, 2, and 15
Step by step of insertion into a B-Tree• Insert 8, 14, 2, 15, 3, 1, 16, 6, 5, 27, 37, 18, 25, 7, 13, 20, 22, 23, 24
into a tree of order m = 5
After inserting 5
14 15 16 1 2 5 6
3 8
After inserting 1, 16, 6
8
14 15 16 1 2 3 6
1 2 5 6
3 8
14 15
16
27
After inserting 37
27 37
1 2 5 6
3 8 After inserting 27
14 15 27 16 27
1 2 5 6
3 8
14 15
16
27
After inserting 20
20187 13 27 37
25
1 2 5 6
3 8
14 15
16
27
After inserting 18, 25, 7, 13
27 37 25187 13
After inserting 24
1 2
3 8
2423 27 37 5 6 7 13 14 15 2018
22 25
16
After inserting 22, 23
1 2 5 6
3 8
14 15
16
27 20187 13 27 37
25
22 23
• Another example:
• This time we want to insert a set of numbers into a tree of order m = 4.
• For order 4, the number of keys in each node is at least 1 and at most 3.
• Insert 8, 14, 2, 15, 3, 1, 16, 6, 5, 27, 37, 18, 25, 7, 13, 20, 22, 23, 24
After inserting 15
2 14
After inserting 8, 14, and 2
8
2 14
8
15
After inserting 3, 1, and 16
2 14
8
1531 16
After inserting 6
14
8
151 1663
2
After inserting 5
14
8
151 1653
2
6
After inserting 27
14
8 15
1 53
2
6 16 27
After inserting 37, 13, 12
12
8 15
1 53
2
6 16 27 3713 14
After inserting 20
121 53
8
6 1613 14 27 37
2 2015
Another example:
• This time we present the B-tree with more detail that shows how the index keys are connected to specific records in the disk.
• Suppose we want to create indexing using B-tree of order m = 3 for the following employee records in the disk
• Assuming that the EmpId is unique in the employee table, the best index key can be the EmpId
• This example shows step by step of creating B-tree of order m=3 and illustrates how the pointers are linked to the records in the disk
2 Jack 30,000
80 Steve 32,000
8 John 50,000
71 Nancy 55,000
15 Rose 90,000
63 Abdul 35,000
90 Pat 42,000
55 Kathy 45,000
35 Melissa 38,000
51 Joe 39,000
EmpId Name Salary
• Insert index for record: 2 Jack 30,000
2
Null PointerBefore
After
• Insert index for record: 80 Steve 32,000
2 80
2Before
After
• Insert index for record: 8 John 50,000
8
2 80
2 80Before
After
• Insert index for record: 71 Nancy 55,000
8
2 71 80
8
2 80
Before
After
• Insert index for record: 15 Rose 90,000
8
2 15 80
71
8
2 71 80
Before
After
• Insert index for record: 63 Abdul 35,000
8
2 15 80
71
63
8
2 15 80
71Before
After
• Insert index for record: 90 Pat 42,000
8
2 15 80
71
63 90
8
2 15 80
71
63
Before
After
• Insert index for record: 55 Kathy 45,000
55
8 71
15 63 80 902
8
2 15 80
71
63 90
Before
After
• Insert index for record: 35 Melissa 38,000
55
8 71
15 63 80 902 35
55
8 71
15 63 80 902
Before
After
• Insert index for record: 51 Joe 39,000
55
8 71
15 632 80 9051
35
55
8 71
15 63 80 902 35
Before
After
55
871
15
632
80
9051
35
2 Jack 30,000
80 Steve 32,000
8 John 50,000
71 Nancy 55,000
15 Rose 90,000
63 Abdul 35,000
90 Pat 42,000
55 Kathy 45,000
35 Melissa 38,000
51 Joe 39,000
EmpId Name Salary
Deleting from a B-tree
• For the delete operation, there are two general cases:• Deleting a key from the leaf• Deleting a key from a non-leaf
• Case 1: Deleting from a leaf node:
• If after deleting a key K, the leaf is at least half full, simply delete the element
• If after deleting, the number of keys in the leaf is less than (m/2) -1, causing an underflow:
• If there is a left or right sibling with the number of keys exceeding the minimal (m/2) -1, then all keys from this leaf and this sibling are redistributed between the two nodes
1 2 5
3 8
14 15 277 13
Before deleting 7
1 2 5
3
8
14 15 27
13After deleting 7
• If after deleting, the number of keys in the leaf is less than (m/2) -1, causing an underflow:
• If neither left no right sibling have more than minimal f(m/2) -1, then merge the node with one of the siblings and place proper index in the parent node
Before deleting 8
1 2
3
5 13 27
After deleting 8
1 2 5
3
8
14 15 27
13
14 15
merge
• A particular case results in merging a leaf or nonleaf with its sibling when its parent is the root with only one key.
• In this case, the keys from the node and its sibling, along with the only key of the root, are put in the node which becomes a new root, and both the sibling and the old root nodes are discarded.
• This is the only case when two nodes disappear at the same time.
• Also the height of the tree is decreased by one
• See the next example.
1 2
3 13
Before Deleting 8
2423 27 37 5 8 14 15 2018
22 25
16
1 2
3
1 is deleted but process continues
2423 27 37 14 15 2018
22 25
16
3
135
1 2 2423 27 37 14 15 2018135
16 22 25
merge
merge
After deleting 8
Case 2:
• Deleting from a non-leaf node
• This can lead to problems with reorganization.
• Therefore, deleting from a nonleaf node should be reduced to deleting a key from a leaf to make the task simple
• The key to be deleted is replaced by its immediate predecessor (the successor could also be used) which can only be found in a leaf.
• This predecessor key is deleted from the leaf based on the algorithm we discussed in case 1
3
1 2 2423 27 37 14 15 2018135
16 22 25
Before deleting 16
3
1 2 2423 27 37 14 16 2018135
15 22 25
Swap 16 and 15 and delete 16
3
1 2 2423 27 37 14 2018135
15 22 25After deleting 16
B-Tree delete algorithm
Node = Search for the node that contains key K to be deleted;
If (node is not a leaf)Find a leaf with the closest successor/predecessor S of KCopy S over K in node;Node = the leaf containing SDelete S from node
Else delete K from node;
While (1){ If node does not underflow
Return else if there is a sibling of node with enough keys (i. e. more than (m/2)-1)
Redistribute keys between node and its siblingReturn
else if node’s parent is the rootIf the parent has only one key
Merge node, its sibling, and parent to form a new rootelse
merge node and its siblingReturn
else merge node and its siblingnode = its parent
}
B* Tree
• A “B*-Tree” is a variant of the B-Tree .
• All the nodes except the root are required to be at least two-third full.
• More precisely, the number of keys in all nodes except the root in a B*-tree of order m is k where
(2m-1/3) <=k <= m-1
• In this type of tree, the frequency of node splitting is decreased by delaying a split and when the time comes we split two nodes into three (not one node into two as done in B-tree)
• Lets see some examples of inserting into a B* tree
16
0 1 1210 18 25 30 2 75 9 27 28
Before inserting 6
After inserting 6
0 1
10
18 25 30 2 65 9 27 287 12 16
B* Tree – Cont.
• As shown in the previous slide, the key 6 is to be inserted into the left node which is already full
• Instead of splitting the node, all keys from this node and its sibling are evenly divided and the median key, key 10, is put into the parent
• Notice that this not only evenly divides the keys, but also it frees some space in the nodes for more key
• If the sibling is also full, a split occurs and one new node is created, the keys from the node and its sibling (along with the separating keys from the parent) are evenly divided among three nodes and two separating keys are put into the parent
• See the next slide for an example.
10
0 1 98 12 16 27 28 2 65 7 302918 25
Before inserting 4
6
0 1 7 8 12 2 4 9 10
After inserting 4
18 25 29 30 27 28
166
5
B+ Tree• Basically, a node in a B-tree structure represents one secondary
page or a disk block
• The passing from one node to another node can be a time consuming operation in case we need to do something like in-order traversal or print of the B-tree
• B+ tree is enhanced form of B-tree that allow us to access data sequentially in a faster manner than using in order traversal
• In a B-tree, references to data are made from any node of the tree but in a B+ tree, these references are made only from the leaves
• The internal nodes of a B-tree are indexes to the leaves for fast access to the data
B+ Tree Cont.• In a B+ tree, the leaves have a different structure than other
nodes of the B+ tree and usually they are linked sequentially to form a sequence set so that scanning this list of leaves results in data given in ascending order
• The reason this is called B+ tree is that• The internal nodes (not the leaves) all have the same
structure as the B-tree) plus • The leaves make a linked list of the keys
• Thus we can say that B+ tree is a combination of indexes plus a linked list of keys
• The internal node of B+ tree stores keys, and pointers to the next level nodes
• The leaves store keys, references to the records in a file, and pointer to the next leaf.
Algorithm for inserting into a B+ tree
• During the insert, when a leaf node is full and a new entry is inserted there, the node overflows and must split
• Given that the order of the B+-tree is p, the split of a leaf node causes the first p/2 entries (index keys) to remain in the original node and the rest move to the new node
• A copy of the middle key is placed into the parent node
• If the parent (non-leaf node) is full and we try to insert a new key there, the parent splits. Half of the nodes stay in the original node, the other half move to the new node, and the middle key is moved (not copied) to the parent node (just like B-tree).
• This process can be propagated all the way up to the root.
• In the next example, we go through step by step (with pointer details) of inserting into a B+ tree
Example of a B+-Tree of order 3
• Example of inserting the index for the following records into a B+-tree
8 Jack 30,000
5 Steve 32,000
1 John 50,000
7 Nancy 55,000
3 Rose 90,000
12 Abdul 35,000
9 Pat 42,000
6 Kathy 45,000
EmpId Name Salary
• Insert index for record: 8 Jack 30,000
8
Null PointerBefore
After
• Insert index for record: 5 Steve 32,000
5 8
8Before
After
• Insert index for record: 1 John 50,000
5
1 5 8
5 8Before
After
• Insert index for record: 7 Nancy 55,000
5
1 5 7 8
5
1 5 8
Before
After
• Insert index for record: 3 Rose 90,000
3
5 7 81 3
5
5
1 5 7 8
Before
After
• Insert index for record: 12 Abdul 35,000
3
5 7 81 3 12
8
5
3
5 7 81 3
5Before
After
• Insert index for record: 9 Pat 42,000
3
5 7 81 3 9
8
5
12
3
5 7 81 3 12
8
5
Before
After
• Insert index for record: 6 Kathy 45,000
3
5 71 3 9
8
5
12
Before
3
5 61 3 9
7
5
128
8
After
7
8
8 Jack 30,000
5 Steve 32,000
1 John 50,000
7 Nancy 55,000
3 Rose 90,000
12 Abdul 35,000
9 Pat 42,000
6 Kathy 45,000
EmpId Name Salary
3
67
13
9
7
5
1288
5
Deleting from B+ tree• If the deleting of a key does not cause underflow, we just have to
make sure other keys are properly sorted
• Even if the index of the key to be deleted is in the internal node, the index can still be there because it is just a separator
3
5 7 81 3
5
3
5 7 81
5After Deleting 3
Before Deleting 3
• When delete of a node from a leaf causes an underflow, then either the keys from this leaf and the keys of a sibling are redistributed between this leaf and its sibling or the leaf is deleted and the remaining keys are included in the sibling
3
5 71 3 8
7
5After Deleting 12
3
5 7 81 3 12
8
5Before Deleting 12
Trie• In the previous examples we have used the entire key (not just
part of it) to do searching of an index or an element
• A tree that uses parts of the key to navigate the search is called a trie (pronounced “try”)
• Each key is a sequence of characters and a trie is organized around these characters rather than entire keys
• Suppose that all keys are made of 5 letters A, E, I, P and R
• The next slide shows an example of a trie.• For example, search for word “ERIE”, we first check the first
level of trie, the pointer corresponding to the first letter of this word “E” is checked
• Since this pointer is not null, the second level is checked. Again it is not null and we follow the pointer from letter “R”
• Again other levels are checked till you either find the word or you reach NULL.
# A E I P R
# A E I P R
# A E I P R
# A E I P R
# A E I P R
# A E I P R
# A E I P R
# A E I P R
# A E I P R
# A E I P R
# A E I P R
Ara
Area
Era
EIre
IPA
A
Are
IRE
Rear
Rep
Pier
Pear
Peer
Per
Ere
Erie