Overview
This class completes the treatment of AVL deletion rotations (R0, R1, R−1 and their equivalence to LL/LR rotations), then moves to M-way trees: their properties, search, insertion, and deletion. Worked insertions are done in a 5-way tree (6, 146, 251) and a 3-way tree (1, 9, 22, 26), followed by the four deletion cases based on null/non-null child pointers, with cascade delete examples and a property-violation bug that is diagnosed and fixed. The class ends by flagging the B-tree as the balanced M-way tree that resolves the skewness problem.
Learning outcomes
- Perform AVL deletion rotations R0, R1, R−1 by reading the balance factor of node B, and relate them to LL/RR/LR/RL insertion rotations.
- State the properties of an m-way tree: maximum children = m, maximum keys = m − 1, sorted order, and range bounds imposed by ancestor keys.
- Search for a key in an m-way tree by following range-based child pointers from the root.
- Insert a key into an m-way tree, deciding whether the target node has space or requires a new node.
- Delete a key from an m-way tree by identifying which of the four pointer cases applies, and pull up the correct extreme element (highest from the left, smallest from the right).
- Explain cascade delete and why pulling up a non-extreme element violates the m-way tree property.
- Explain why a B-tree sizes each node to one disk block, and how an overfull node splits by popping the median up to the parent.
Core concepts
AVL deletion rotations (R0, R1, R−1)
Node A is the first node that violates the balance factor property from the point of deletion; node B is the node to the left of A (its left chain). The rotation chosen depends on B's balance factor being 0, 1, or −1. R0 and R1 are similar to LL rotation; R−1 is similar to LR rotation. This matters because knowing only the two basic rotations (LL and RR) is enough to perform LR, RL, R0, R1, and R−1 — it connects the previous lecture's insertion rotations to deletion.
M-way tree
A tree where each node can have up to m ways (access paths) to lower nodes, instead of only two as in a binary tree / binary search tree. It is an extension / multi-way variant of the BST, effectively combining multiple BSTs. It matters because M-way trees are primarily used in secondary storage; searching and traversal are easy (at each node perform a binary search or linear search), and the aim is to keep values in sorted order and do search/insert in log n time.
M-way tree properties
- A node does not hold a single value but an array of values. If m = 5: maximum children = 5, children may be 2 to 5, keys = 1 to 4.
- BST-like property with range bounds: for key Ki, values in the left are less than the key and values in the right are greater than the key; additionally, values must lie within the range imposed by ancestor keys (e.g., values in a subtree must be greater than 18 but less than 44).
- All values are always in sorted (ascending) order.
These properties are what every search, insertion, and deletion must preserve.
Duplicates and the search-before-insert rule
The BST does not allow duplicate entities; similarly the M-way tree does not allow duplicate insertion. Hence a search must precede any insertion (and deletion). This is why insertion explores to the extreme downward before placing a key.
Search procedure
Start at the root; at each node decide which range the key falls into and follow the corresponding child pointer; continue until found or until a null pointer shows the key is absent. This is the basis for both insertion and deletion.
Insertion procedure
- Search the key to be inserted (duplicate entries are not allowed).
- If we reach a node where the key could be inserted and it has available space, add the key with null child pointer(s) as applicable (two null child pointers as in the case of 146, or one null child pointer as in the case of 6).
- If the node already has the maximum number of keys, make a new node with one key and two null pointers.
Insertion always goes down rather than into a node with space because a downward pointer may hide the value below (duplicates are not allowed); this also avoids pointer shifting. This can cause skewness — a problem to be solved by the B-tree.
Rule of thumb for node limits
Minimum child should be 2 and maximum child should be m; minimum key should be 1 and maximum key should be m − 1. This is the rule of thumb every time.
Determining the order of the tree
Either it is given in the question, or you identify any node in the tree with the maximum number of children (e.g., the node with 5 children indicates a 5-way tree).
Trade-off of increasing order m
Increasing m allows each node to store more keys and have more children, which reduces the height of the tree, but increases the searching time within a node because there are more elements to search at each node.
Starting point of operations
Search, insertion, and deletion always start from the root. In traversal (inorder: left, root, right) we also start at the root but do not read/print its value — we keep following left pointers (using a stack to delay operations) until the leftmost node, then print. The root is so named because it is the first node encountered in any operation.
Four deletion cases
Stated via the child pointers a_i, a_j:
- If a_i and a_j are null → simply remove the key (case 1).
- If a_i not null and a_j null → pick the highest element from the left side (case 2).
- If a_i null and a_j not null → pick the smallest element from the right child (case 3).
- If both are not null → you can go with either case 2 or case 3 (case 4).
Leaf deletion is easy: if the node from which deletion is to happen is a leaf, we are in case 1 — simply remove the key and shrink the node.
Cascade delete
A deletion may itself require further deletions down the tree (e.g., removing 198 required removing 141, which required removing 148, which required removing 146). This chained operation is called cascade delete. The pulled-up element must be the extreme one: the left pointer must hold a value less than the root, and the right pointer a value greater than the root. Replacing with any value other than the correct extreme (e.g., the highest from the entire left subtree) violates the m-way tree property.
In-order predecessor / successor connection
The in-order predecessor of 198 is 186 (just less than it) and the in-order successor is 262 (just greater than it); this BST property can also be adopted here.
Search within a node
Linear search can be used if the number of elements is small (less than five or ten); otherwise binary search is preferred. The main role is to reduce the complexity of searching on each node, depending on the number of elements available.
Choosing left vs right route in case 4
The deletion algorithm for the m-way tree does not track the complexity of both paths; you can either always choose left or always choose right, or introduce a special function that tracks the complexity of both paths and prefer the path with fewer nodes (less traversing) — this would be a modification of the algorithm.
B-tree: node sizing and split on overflow
The B-tree is the balanced M-way tree that fixes the skewness problem. It is primarily designed so that a node is sized to one disk block, so a lookup costs a handful of disk reads instead of one per level. When a node becomes overfull during insertion, the median pops up to the parent and the rest splits into two nodes; if the parent then overflows, the same split repeats upward.
Formulas and maths
- Maximum children of an m-way tree: $m$ — the number of access paths a node may have; used to identify the order and capacity of the tree.
- Maximum keys in a node: $m - 1$ — the number of values a node may hold; used to decide whether a node is full during insertion.
- For $m = 5$: maximum children $= 5$, children may be $2$ to $5$, keys $= 1$ to $4$ — used when inserting into the 5-way tree examples.
- For a 3-way tree: maximum children $= 3$, maximum keys $= 2$ — used when inserting into the 3-way tree examples.
- Rule of thumb: minimum child $= 2$, maximum child $= m$; minimum key $= 1$, maximum key $= m - 1$ — applied every time a node's validity is checked.
Worked examples
5-way tree (given tree, not built from scratch — it may be the result of multiple insertions and deletions)
Root keys 18, 44, 76, 198, with lower nodes including 7, 12; 80, 92, 141, 148; 262, 276, 286, 350; and 77.
- Search for 350: 18 → right; 44 → right; 76 → right; 198 → right; explore the right branch; 262 → right; explore children; found among 276, 286, 350.
- Search for 77: 18 → right; 44 → right; 76 → right; 198 → left (since 77 < 198); go down; 80 → left; follow the link on the lower end; 76 is obtained.
- Insert 6: search: 18 → left; go down; 7 → left; 12 → left; no further entry on the left, so 6 is not present. The node (7, 12) has only 2 keys and a 5-way tree can accommodate up to 4 keys, so 6 is inserted there with a null pointer created. (If it were a 3-way tree, 6 could not be inserted there and would be inserted downwards.)
- Insert 146: 18 → right; 44 → right; 76 → right; 198 → left (146 is greater than 76 and less than 198); go down; 80 → right; 92 → right; 141 → right; follow the right pointer down; 148 → left (146 < 148); no further nodes downward. The node already has the maximum number of keys, so a new node is created on the left with value 146 and both null pointers marked.
- Insert 251: 18 → right; 44 → right; 76 → right; 198 → right; there is a pointer on the right, so go down; 262 → left (251 < 262); no pointer on the left; there is space, so 251 is added there.
3-way tree
Insertion sequence given: 4, 7, 8, 3, 2, 11, 17, 16, 44, 43, 21. Resultant tree described: root (4, 7); children (2, 3), (8, 11), (16, 17), (43, 44); and 21 in a node below (pointing from the left of 43).
- Insert 1: 4 → left; node (2, 3) is full, so a new node is created and 1 is inserted there.
- Insert 9: 4 → right; 7 → right; 8 → right; 11 → left; 9 is greater than 8 but less than 11, so it is placed in that common position (a new node is formed).
- Insert 22: 7 → right; 11 → right; 16 and 17 → right; 44 → left; 22 is accommodated there.
- Insert 26: 7 → right; 11 → right; 17 → right; 44 → left; 21 → right; 22 → right; space is already full, so 26 is inserted with a new node.
Deletion examples
- Leaf deletions (case 1): delete 77 (no child — easily removed), delete 92 (no child — easily removed, node shrinks), delete 44 (no child — delete and shrink). Also 272, 286, 350, 8, 10 have no child. If both left and right child pointers are null, simply remove the key k.
- Case 2 example: to remove 80, which has only a left child and nothing on the right — replace 80 with 77 (77 comes up, 80 is removed). In another instance, removing 80 (only left child) means picking the highest element from the left; the highest element is 12, so 12 replaces 80. This cascades: deletion of 80 needs deletion of 77; deletion of 18 needs removal of 12.
- Case 3 example: to remove 262 (right child case) — bring up the smallest element, 272. Deletion of 262 includes deletion of 272: 272 is stored in temporary storage, removed, then replaces 262.
- Case 2 with cascade: to delete 12 — 12 has a left node but no right node, so it is replaced with the highest element from the lower part; the highest element is 10, so 10 is first removed (stored in temporary storage) and then replaces 12.
- Case 4 example: to remove 198 (both children) — either pull up 141 (by removing it) or pull up 262. If pulling up 141: 141 has a right child, so from the right child pick 146. Cascade: removal of 198 required removal of 141, removal of 141 required removal of 146; 146 is removed and stored in temporary storage, then replaces 141, and 141 replaces 198. This preserves the m-way property (left less than root, root further less than right).
Tree drawn in class (after insertions)
18, 44, 76, 198; then 6, 7; then 80, 92, 141; then 262, 8, 10; then 77; then 148, 151, 172, 186; then 272, 286, 350; and 146 (having both pointers). Pointer details described: one node points to 146, others null; a node points to 272, 286, 350 with other fields null; 18 points to a node with two null pointers; a pointer to 77; a pointer to 148; etc.
Deletions on the drawn tree
- Remove 272 (two null pointers, no further nodes — simply removed, no pointer change).
- Remove 77 (pointer becomes null, 77 easily removed).
- Remove 198 (both children — either 141 or 262 can be pulled up).
The bug case
Attempting to remove 198 via the left direction, the class initially pulled 141, then 148, then 146 — but 141's left child is 148, which is not possible (violation of the property). The fix: pick the highest element from the entire left subtree, which is 186, so 186 replaces 198. From the right side, the smallest element from all of it is 262, which replaces 198.
Removal of 76 (middle node)
76 has only a right pointer, not a left pointer, so from the right side pick the smallest — 77 — and replace 76. Deletion is allowed from anywhere, not only the extreme left or right.
Insertion pointer example (slide 10)
If the key values in the last leaf node are 8 and 15 instead of 8 and 11, and 10 is to be inserted, 10 goes to the left of 15 (the pointer is common, so it goes to the same place; it lies between 8 and 15, i.e., greater than 8 and less than 15). Values like 9, 10, 11, 12, 14 could be inserted in that range.
B-tree overflow split
Inserting into a B-tree node that is already full makes it overfull: the median pops up to the parent and the remaining keys split into two nodes. If the parent is then overfull, the same median-up split repeats at that level.
Questions asked in class
Q (on the 146 example): In a 3-way tree maximum children is 3 and maximum keys is 2 — how do we first identify the number of children and keys and the limit at each node? A: It will be given in the question (e.g., "5-way"). If not given, identify any node in the tree with the maximum number of children — here the node with 5 children (the 48/148 node) indicates a 5-way tree. Either it is given or you check the node with the maximum possible children.
Q: When inserting 6, we inserted it into the existing key position (left of 7) without going to the leaves, but for 146 we went down through subtrees (80, 92, 141) and then inserted at the leaf even though a null pointer was available — why? A: In the 146 case the node's available keys are already 4, which is the maximum for a 5-way tree, so it is full and cannot accommodate the key; it only provides direction, so a new key is created and given pointer access from the left. If, say, 177 were to be inserted, it could be inserted in the middle of 172 and 186 by updating the null pointer.
Q (on inserting 1 in the 3-way tree): Could 1 be inserted at the root itself? A: First we must search — there are chances 1 may be present in the lower part, so we first establish that 1 is not present, then identify its correct position. If only 4 were at the root with pointers in both directions, 1 could be accommodated there, but since there is a lower pointer on the left, 1 might exist downward, so we dig down first. (This is why 6 was inserted directly: there was no downward pointer to the left.) The student also clarified confusion about (2, 3) having three pointers and being to the left of 4.
Q: In the 3-way example, when inserting 9, how do we choose which pointer to follow when there are many? A: 9 is stored to the right of 8 but to the left of 11 — that is the common place; it is greater than 8 but less than 11. In general the place is determined by the BST property: for 8, left is less and right is greater; for 11, left is less and right is greater, because the pointers are merged to form this structure.
Q: Does increasing the order m always reduce the height of the tree, and what trade-off is introduced in searching and insertion? A: Of course it reduces the height because each node accommodates more elements, but searching within a node takes longer when more elements are stored on each node — this is the trade-off.
Q (Hindi/Hinglish): When we insert a child pointer for 26, does it mean two children? A: 26 is a key with two null child pointers — that is the rule: