Solution: When 375 gets inserted into the tree, it gets inserted into the leaf node containing the keys 325, 350, 400, and 425. Since the B-tree is of order 5, nodes can only hold 4 keys. Hence we must split the leaf node and promote the middle element, which is 375, to the parent node. When we promote 375 to the parent node, it also overflows and must be split in two. We promote the middle element, which is 450, and it becomes the new root of the tree. The new tree is shown below:
Solution: 700 gets inserted into the leaf node containing the keys 550, 600, 650, and 750. Since the B-tree is of order 5, nodes can hold only 4 keys. Hence we must split the leaf node and promote the middle element, which is 650 (once 700 is included, we have 550, 600, 650, 700, and 750) to the parent node. When we split the leaf node, 550 and 600 go into the left leaf node and 700 and 750 go into the right leaf node.
When 650 gets promoted to the parent node, the parent node overflows because it can only hold 4 keys. Again 650 is the middle key (300, 500 650, 800, and 1000 are the keys) so we promote 650 to the root node and split the parent node. 300 and 500 move into one node and 800 and 1000 move into the other node. The resulting tree is shown below:
Solution: When we delete 80 from its leaf node, the leaf node no longer has the minimum number of keys. It first looks to see if it can take a key from either its left or right sibling, but both siblings have the minimum number of keys as well. Hence the leaf node must merge with its left sibling, which is the leaf node containing the keys 0 and 20. When this merger occurs, 70 gets demoted from the parent and included in the new leaf node because 70 used to point to the two separate leaf nodes and now they have been merged. Thus 70 must get bundled into the new leaf node as well. The resulting BTree is shown below:
Solution: When 250 gets deleted from its leaf node, the leaf node no longer has the minimum number of keys. It first looks to see if it can take a key from its left sibling, which it can't because the left sibling holds the minimum possible number of nodes. It then looks to see if it can take a key from its right sibling, and it can, because that leaf node contains 3 keys--400, 500, and 600--which is one more than the minimum required. However 400 cannot be directly moved into the leaf node because that would violate the BTree requirement for sorted order (400 is greater than 300 but would have to be less than 400 to be in the same leaf node as 200. Instead we rotate 400 up to the parent node and rotate 300 down to the leaf node where the deletion occurred, thus obtaining the tree shown below:
Solution: 900 is located in an internal node so we delete the largest key in its left subtree instead, which is 600 (this can also be stated as deleting the immediate predecessor of 900) and we replace 900 with 600. We then proceed with re-balancing the leaf node from which 600 was deleted. Since that leaf node now has only the key 500, which is fewer than the minimum number of required keys, 2, we look to take a key from a sibling and find that we can take a key from the right sibling. We rotate this key, which is 1000, up to the parent and demote the parent key, which is the newly promoted 600, down to the leaf node. Thus 600 gets promoted to the parent but ends up getting demoted back down to its original leaf node. The resulting BTree is:
Note that this is the same BTree that would have resulted if we deleted the smallest key from 900's right subtree, which is the way it is done in the course notes.
Solution: 400 is located in an internal node so we
delete the largest key in its left subtree instead, which
is 300 (this
can also be stated as deleting the immediate predecessor
of 400) and we replace 400 with 300. We then proceed with
re-balancing the leaf node from which 300 was deleted. Since
that leaf node now has only the key 250, which is fewer
than the minimum number of required keys, 2, we look to take
a key from a sibling and find that we can take a key from
the right sibling. We rotate this key, which is 500, up
to the parent and demote the parent key, which is the newly
promoted 300, down to the leaf node. Thus 300 gets promoted
to the parent but ends up getting demoted back down to its
original leaf node. The resulting BTree is:
Note that this is the same BTree that would have resulted if we deleted the smallest key from 400's right subtree, which is the way it is done in the course notes.
Solution: 100 is located in an internal node so we delete the largest key in its left subtree instead, which is 90 (this can also be stated as deleting the immediate predecessor of 100) and we replace 100 with 90. We then proceed with re-balancing the leaf node from which 90 was deleted. Since that leaf node now has only the key 80, which is fewer than the minimum number of required keys, 2, we look to take a key from a sibling and find that doing so is not possible because both siblings have the minimum number of keys. Thus we must merge 80's node with its left sibling, which contains the keys 0 and 20. When this merger occurs, we also demote 70 from the parent and include it in the new leaf node because 70 used to point to the two separate leaf nodes and now they have been merged. Thus 70 must get bundled into the new leaf node as well, giving us a leaf node that consists of the keys 0, 20, 70, and 80.
As a result of demoting 70, we are left with an internal node that consists of only 1 key, 100. Since this is less than the required minimum number of keys, we check its two siblings to see if we can borrow a key. There is no left sibling and the right sibling has the minimum number of keys, so we cannot take a key. Instead we must merge the internal node containing 100 with a sibling. Normally we would merge with the left sibling but 100 is the left-most node on this level so there is no left sibling. Thus we merge 100 with its right sibling. We must also demote the parent key for these two nodes, which is 200, thus obtaining a new internal node that consists of the keys 100, 200, 500, and 1000. Since 200 was the only key in the parent (it was the root node of the BTree), the parent disappears and the BTree decreases in height by 1 level. The newly merged internal node becomes the new root of the BTree. The resulting BTree is shown below: