Overview
This class continued the study of B-trees, focusing on the insertion and deletion algorithms. It began with a recap of B-tree properties and a detailed walkthrough of insertion, including handling full nodes via splitting and median promotion. The session then covered deletion from leaf nodes, explaining borrowing and merging operations to maintain tree properties. A comparison was made between B-trees and Binary Search Trees (BSTs), highlighting their respective use cases in hierarchical storage and in-memory operations. The class concluded with administrative notes on assignments.
Learning outcomes
- Describe the properties of a B-tree of order m.
- Perform insertion into a B-tree, including handling node splits and cascading promotions.
- Perform deletion from a leaf node in a B-tree, applying borrowing from siblings or merging nodes as needed.
- Compare and contrast the applications of B-trees and Binary Search Trees.
- Identify scenarios that trigger cascading operations during insertion and deletion.
Core concepts
B-tree Properties
A B-tree is a balanced M-way tree designed to prevent skew. Key properties ensure all leaves are at the same level and nodes (except the root) maintain a minimum key count, guaranteeing O(log n) complexity for operations. This structure is crucial for efficient data management in secondary storage like databases.
Insertion Algorithm
Insertion always starts with a search. If the target node has space, the key is added in order. If the node is full, it splits: the median key is promoted to the parent, and the node is divided into two children. This can cause cascading splits upward, making the tree grow in height. This process maintains balance after every insertion.
Deletion from a Leaf
When deleting a key from a leaf, if the leaf has more than the minimum keys, removal is simple. If the leaf is at the minimum, we first try to borrow a key from an immediate sibling (via the parent). If both siblings are also at their minimum, the leaf is merged with a sibling, and a key is brought down from the parent. This can trigger cascading merges upward.
B-tree vs. Binary Search Tree (BST)
BSTs, with one entity per node, are optimal for in-memory operations like duplicate removal. B-trees, with multiple entities per node, are designed for hierarchical, multi-level storage (e.g., on disk). A single B-tree node fetched into RAM can access large data chunks, making it more efficient than managing many BST root pointers for large datasets.
Formulas and maths
- Median for Splitting: For a node with
nkeys.- If
nis odd: Median position =n/2 + 1. - If
nis even: Median position =n/2. Used to find the key to promote during a node split in insertion.
- If
- Order m B-tree Constraints:
- Maximum keys per node:
m - 1. - Minimum keys per internal node (except root):
ceil(m/2) - 1. - Minimum children for internal node:
ceil(m/2). - Root can have 1 to
m-1keys (or 2 tomchildren as per property). Used to define the valid state of a B-tree and check for violations during operations.
- Maximum keys per node:
Worked examples
Insertion (Order 5)
- Inserting 4 and 5 into a node with space: added directly.
- Inserting 58 into node
[37, 46, 55, 86]: Node becomes full[37, 46, 55, 58, 86]. Median (odd count: 5/2+1) is 55. 55 pops up to parent, node splits into two children:[36, 37, 46]and[58, 86]. - Inserting 6 into node
[2, 4, 5, 7]: Node is full[2, 4, 5, 6, 7]. Median 5 pops up, causing a cascade where the parent also becomes full, leading to further promotion.
Insertion (Order 3)
- Inserting 75 into node
[65, 70]: becomes[65, 70, 75]. Median 70 pops up, splits node into children[65]and[75], 70 joins parent. - Inserting 57 into node
[55, 58]: becomes[55, 57, 58]. Median 57 pops up, splits into[55]and[58], 57 joins parent. Parent node[50, 60, 57, 70]becomes full, causing median 60 to pop up further (cascading).
Deletion from Leaf (Order 5)
- Delete 64 from leaf node
[62, 64, 66](has 3 keys, minimum is 2): Simply remove 64 →[62, 66]. - Delete 23 from leaf node
[20, 23](bare minimum 2 keys). Left sibling[10, 16, 18]has 3 keys (> min). Borrow largest key (16) from left sibling via parent: 16 replaces 20 in parent, 20 moves down to replace 23. Result: leaf[16, 20], sibling[10, 18], parent updated. - Delete 72 from leaf node
[72, 73](bare minimum). Right sibling[75, 77, 78]has 3 keys (> min). Borrow smallest key (77) from right sibling via parent: 77 replaces 75 in parent, 75 moves down to replace 72. Result: leaf[73, 75], sibling[77, 78], parent updated. - Delete 65 from leaf node
[65](context: left sibling[60]and right sibling[70]both have bare minimum keys). Merge with left sibling and bring parent key down: merge[60]and[70](removing 65), bringing down parent key 60, resulting in node[60, 70].
Complex Deletion & Internal Node Deletion
- Delete 20: From a structure with root 50, left child 16, right child containing 20. Leaf
[20]has left sibling[14,15](bare minimum). Merge by bringing down parent key 16 to form[14,15,16], removing 20. This causes the parent node (was[16]) to violate min keys. Resolve by bringing down higher parent key 50 and merging with its siblings, forming a new root[10, 50, 70, 77]with a child[14, 15, 16, 27]. - Delete 70 (internal): Replace with in-order predecessor 68 (from left child), as the right child had the bare minimum.
- Delete 95 (internal): Replace with in-order successor 100 (from right child), as the left child had the bare minimum.
- Delete 77 (internal): Both left and right siblings had bare minimum. Bring 77 down and merge the two sibling nodes.
- Delete 80 (internal): Can be replaced by either in-order predecessor 79 or successor 81, as both have more than the minimum keys.
- Delete 100 (internal): Removing it would violate min key property. Solution: Bring down 79, form a new node, and merge affected siblings.
- Delete 50: Process involves bringing down 68 and merging nodes. Result: root becomes
[10, 68]with appropriate children.
Questions asked in class
- Q: For insertion of 75 into
[65,70], why are 65 and 75 separated after sending 70 up?- A: When a full node splits, the median key (70) is promoted. The keys below the median (65) and above the median (75) form the two new child nodes.
- Q: Why use B-trees over BSTs if both have O(log n) search?
- A: B-trees are for hierarchical/multi-level storage (e.g., databases). A single B-tree node fetched into RAM accesses many keys, whereas multiple BSTs would require storing many root pointers in RAM, which is inefficient.
- Q: In an order 3 tree, can the root have two keys (e.g., 50 and 60)?
- A: Yes, the root can have from 1 to m-1 keys (for order 3, up to 2 keys).
- Q: When both siblings have the bare minimum, which sibling is chosen for merging?
- A: It is a choice; either can be used. Typically, left is preferred by convention.
- Q (Jessie): When deleting 65, why not bring 68 up to the parent and bring 60 down instead of the taught merge?
- A: The taught rule is standard. The proposed variant could create a node with a null child pointer. In standard B/B+ trees, non-leaf nodes must have a child pointer for all key positions; such a variant would fail in database implementations.
- Q: Is borrowing during deletion similar to AVL tree rotations?
- A: Yes, it is a analogous rotation operation to balance the tree and maintain rules.
- Q: Will Assignment 3 cover up to AVL trees?
- A: Yes, it will cover up to AVL trees, not beyond.
Key takeaways
- A B-tree of order m ensures all leaves are at the same level, guaranteeing balanced height.
- During insertion, a full node (with m-1 keys) splits, promoting its median key to the parent.
- Deletion from a leaf with the minimum keys (
ceil(m/2)-1) first attempts to borrow from a sibling with surplus keys. - If both immediate siblings also have the minimum keys, the leaf must merge with one sibling, bringing down a key from the parent.
- Merging can cascade upward if the parent is left with fewer than the minimum keys after bringing a key down.
- Internal node deletion is handled by replacing the key with its in-order predecessor or successor.
- Duplicate keys are not allowed in standard B-trees (but are allowed in B+ trees).
- B-trees are optimized for systems with hierarchical storage (like disks), while BSTs are better for in-memory operations.
- The root node is an exception: it can have as few as 2 children (or 1 key).
- Understanding the algorithms is required for exams, not necessarily implementing every edge case.
- A node with a null child pointer is not a standard configuration in a valid B-tree.
- Cascading operations (splits on insertion, merges on deletion) can propagate to the root, potentially changing the tree's height.
Exam-style questions
-
Question: Insert the keys [22, 10, 5, 30, 40, 50, 15, 25] sequentially into an initially empty B-tree of order 3. Show the tree after each insertion that causes a split.
- Answer Outline: Start with root
[22]. Insert 10:[10,22]. Insert 5: causes split of[5,10,22]; promote 10 to new root[10]with children[5]and[22]. Continue, showing splits when nodes exceed 2 keys, promoting the median each time.
- Answer Outline: Start with root
-
Question: In a B-tree of order 5, delete the key 20 from the following leaf node configuration: Parent node keys = [15, 30]; Target leaf = [18, 20]; Left sibling leaf = [10, 12, 14]; Right sibling leaf = [25, 26]. Show each step.
- Answer Outline: Leaf
[18,20]is at minimum (2 keys). Left sibling has surplus (3 keys). Borrow largest key (14) from left sibling via parent: 14 replaces 15 in parent, 15 moves down to replace 20. Final leaf:[15,18]; Left sibling:[10,12]; Parent:[14,30].
- Answer Outline: Leaf
-
Question: Compare and contrast B-trees and Binary Search Trees. Give one specific use case where each is preferred and explain why.
- Answer Outline: BST: In-memory operations (e.g., duplicate removal in RAM) due to simple pointer-based structure and cache efficiency. B-tree: Database indexing on disk due to high branching factor reducing disk I/O (reading a block containing many keys is efficient).
Glossary
- B-tree - A balanced, M-way tree where all leaves are at the same level, optimized for disk storage.
- Order (m) - The maximum number of children a node in a B-tree can have.
- Median Promotion - During a node split in insertion, the median key is moved up to the parent node.
- Bare Minimum Node - A node containing exactly the minimum allowed number of keys (
ceil(m/2)-1). - Borrowing - During deletion, taking a key from a sibling node via the parent to avoid merging.
- Merging - Combining a node with a sibling when both have the minimum keys, bringing down a parent key.
- In-order Predecessor - The largest key in the left subtree of a given node.
- In-order Successor - The smallest key in the right subtree of a given node.
- Cascading Split/Merge - An insertion or deletion operation that triggers further splits or merges propagating up to the root.
- B+ tree - A B-tree variant where all data pointers are stored only in the leaf nodes.
Follow-ups
- Deletion from internal nodes was discussed but not covered in full depth for all cases.
- Further details on B+ trees (mentioned briefly as a variant where duplicates are allowed) will be covered later.
- The precise tree structure after deleting 50 in the final example was described verbally but not fully drawn; the arrangement of children is unclear from the notes.
- Quiz and assignment logistics will be discussed in a lab session, likely after Assignment 4 is complete.