Btrees Deletion Deletion 14 5 1 3 5
Btrees Deletion
Deletion 14 5 1 3 5 14 59 89 14 26 59 79 89 Delete(59) 5 1 3 5 79 89 14 26 79 89
Deletion and Adoption A leaf has too few keys! 14 5 1 3 5 14 Delete(5) 79 89 14 26 79 ? 79 89 1 3 89 14 26 79 So, adopt from a neighbor 14 3 1 3 3 79 89 14 26 79 89 89
Deletion with Propagation A leaf has too few keys! 14 3 1 14 Delete(3) 79 89 3 14 26 79 ? 1 89 79 89 14 26 79 89 And no neighbor with surplus! 14 79 89 But now a node has too few subtrees! 1 14 26 79 So, delete the leaf 89
Finishing the Propagation (More Adoption) 14 79 Adopt a neighbor 79 89 1 14 26 79 89 14 1 89 14 26 79 89
A Bit More Adoption 79 14 1 14 26 79 89 89 79 Delete(1) (adopt a neighbor) 26 14 26 89 79 89
Pulling out the Root A leaf has too few keys! And no neighbor with surplus! 79 79 26 14 26 89 79 Delete(26) 89 14 89 79 89 A node has too few subtrees and no neighbor with surplus! 79 But now the root has just one subtree! 14 79 79 89 89 Delete the internal node 89 14 79 89 So, delete the leaf
Pulling out the Root (continued) The root has just one subtree! Just make the one child the new root! 79 89 14 79 But that’s silly! 89 14 79 89
Deletion in Two Boring Slides of Text Remove the key from its leaf If the leaf ends up with fewer than L/2 items, underflow! ◦ Adopt data from a neighbor; update the parent ◦ If adoption won’t work, delete node and divide keys between neighbors ◦ If the parent ends up with fewer than M/2 items, underflow! If the root ends up with only one child, make the child the new root of the tree.
Why will distributing the node's children and deleting the leaf always work if adoption doesn’t? 1. If adoption failed, then neighbor is at the minimum size, so merging won't overflow. 2. Deleting the leaf will remove all the children, guaranteeing the removal of the target. 3. Adoption will always work. 4. If you hide children under piles of leaves, they cease to exist.
Deletion Slide Two If an internal node ends up with fewer than M/2 items, underflow! ◦ Adopt subtrees from a neighbor; update the parent ◦ If borrowing won’t work, delete node and divide subtrees between neighbors ◦ If the parent ends up with fewer than M/2 items, underflow! If the root ends up with only one child, make the child the new root of the tree
Thinking about B-Trees B-Tree insertion cause (expensive) splitting and propagation B-Tree deletion cause (cheap) adoption or (expensive) deletion and propagation Propagation is rare if M and L are large Repeated insertions and deletion cause thrashing If M = L = 128, then a B-Tree of height 4 will store at least 30, 000 items
B+ Trees and Indices To make a B+ Tree into a data structure suitable for an index, we need to make a few small additions: ◦ The leaf nodes need to keep track of values associated with each key. ◦ You can achieve this with a second list of values, with each value corresponding to a key at the same index. ◦ Each leaf needs a pointer to the next sibling leaf. This allows you to move from one key to the next highest to perform key range lookups efficiently. http: //infolab. stanford. edu/~nsample/cs 245/hando uts/hw 2 sol/sol 2. html
- Slides: 13