Seyed Masoud Hosseini · Overview · Study log · Weekly summaries · Ideas · Search · Transcript · RSS feed

Design & Analysis of Algorithms · Lecture 5 of 34 · 30:45

Recitation 2: 2-3 Trees and B-Trees

R2. 2-3 Trees and B-Trees on YouTube

Study guide

What this lecture covers

This recitation covers B-trees, generalized balanced search trees where each node holds multiple keys and has multiple children, starting from the simplest case, the 2-3 tree. The TA motivates B-trees with the memory hierarchy: unlike a plain binary search tree, a B-tree can be tuned so each node fits one disk block, cutting the number of expensive disk accesses needed for a search.

After watching, you can state the structural rules for a B-tree with branching factor B, walk through searching a B-tree, and carry out the split operation used for insertion and the rotation and merge operations used for deletion, including how overflow and underflow can propagate up toward the root.

Key ideas

  • B-tree structure: each non-root node has between B and 2B - 1 children and between B - 1 and 2B - 2 keys; the root is exempt from the lower bound; all leaves sit at the same depth.
  • Why B-trees over binary search trees: both give Theta(log n) depth, but B-trees are designed around the memory hierarchy, letting one disk access fetch a whole node (sized to match a cache/disk block) instead of one key at a time.
  • Search: at each node, scan its sorted keys to find where the target fits, either finding it directly or descending into the appropriate child interval, giving Theta(log n) time overall.
  • Insertion and overflow: inserting a key can push a node's key count to 2B - 1, one over the limit; the fix is a split, removing the middle key, dividing the remaining keys into two nodes, and inserting the middle key into the parent, which may itself overflow and require splitting, potentially all the way to the root.
  • Deletion at an internal node: deleting a key that isn't in a leaf is handled by swapping it with the rightmost key of its left subtree or leftmost key of its right subtree (both of which are leaves), reducing the problem to deleting from a leaf.
  • Deletion and underflow: removing a key from a leaf can drop a node below the minimum key count; the fix is either a rotation (borrowing a key from a sibling that has more than the minimum, via the parent) or a merge (combining the node, a sibling, and a parent key into one node), and a merge can propagate underflow upward just as a split propagates overflow.

Walkthrough

Why use B-trees over binary search trees (0:00)

The TA defines the 2-3 tree, where each node has one or two keys and two or three children, sorted so an in-order traversal yields sorted keys, exactly like a binary search tree but with more branching per node. He then poses the motivating question: since both structures give logarithmic depth, why prefer B-trees? The answer is the memory hierarchy, described with a simplified two-level model of a fast, finite-size cache holding fixed-size blocks and a slower, effectively infinite disk. Accessing disk brings in a whole block at once, so a data structure whose nodes are sized to match a block lets each disk access do useful work across many keys, unlike a binary search tree, which is stored one key per node.

B-tree specification (2:16)

The branching factor B bounds the number of children per node between B and 2B - 1, and correspondingly the number of keys between B - 1 and 2B - 2; the root is exempt from the lower bound. All leaves are required to sit at the same depth, and only leaves have no children. Searching a B-tree mirrors binary search tree search but scans each node's sorted list of keys to decide which child interval to descend into, giving Theta(log n) time.

Insertion and the split operation (13:40)

Inserting a key can leave a node with 2B - 1 keys, one too many. The split operation removes the middle key, divides the remaining keys evenly into two new nodes, and inserts the middle key (with pointers to the two new nodes) into the parent. This can cause the parent to overflow in turn, requiring another split, and the process can chain all the way up to the root; if the root itself overflows, a new root is created. The recitation works through inserting keys 16 and then 2 into a B = 4 example tree, showing a split propagate one level up to the parent.

Deletion, rotation, and merge (21:27)

Deletion has two stages. First, if the key to delete is in an internal node, it is swapped with the rightmost key of its left subtree or leftmost key of its right subtree, both leaves, reducing the problem to deleting from a leaf. Second, once a leaf loses a key it may become underful. If a sibling has more than the minimum number of keys, a rotation borrows one: a key moves from the sibling up into the parent, and the corresponding key moves from the parent down into the underful node. If no sibling can spare a key, a merge combines the underful node, a sibling, and the separating key from the parent into a single node; this shrinks the parent, which can itself become underful and require the same fix one level up. The recitation demonstrates both cases: deleting 38 and then 41 triggers a chain of merges that propagates up two levels, while deleting 20 and 24 later triggers a straightforward rotation.

Before you watch

  • Review binary search tree operations (search, insertion, deletion) since this recitation builds directly on that model.
  • Familiarity with the two lectures on divide and conquer earlier in the week is helpful context for the course, though not required to follow this session on B-trees.

Check your understanding

  1. Why does a B-tree reduce the number of disk accesses compared to a binary search tree, even though both have Theta(log n) depth?
  2. What are the minimum and maximum number of keys a non-root node can hold in a B-tree with branching factor B, and why is the root exempt from the minimum?
  3. Walk through what happens when inserting a key causes a node to overflow: what does a split do, and under what condition does the overflow propagate further up the tree?
  4. Why does deleting a key from an internal node get converted into deleting a key from a leaf, and which leaf is chosen?
  5. What distinguishes the rotation case from the merge case when a node becomes underful after deletion, and why can a merge cause underflow to propagate upward?

Vocabulary

B-tree (noun)
A balanced tree structure where each node can hold multiple keys and children.
A B-tree is often used to store data efficiently on disk.
branching factor (noun)
The number of children a node in a tree can have.
The branching factor B controls how many keys fit in each node.
leaf (noun)
A node in a tree with no children.
All leaves in a B-tree sit at the same depth.
memory hierarchy (noun)
The arrangement of storage types from fast and small to slow and large.
The memory hierarchy explains why B-trees are efficient on disk.
disk access (noun)
A single trip to read or write data from a hard drive.
Reducing disk access count speeds up searches significantly.
block (noun)
A fixed-size chunk of data transferred at once between memory and storage.
A disk block is read all at once regardless of how much data you need.
split (operation) (noun)
Dividing an overfull node into two smaller nodes.
A split fixes a node that has too many keys after an insertion.
overflow (noun)
A situation where a node has more keys than the maximum allowed.
Overflow can propagate up the tree after several splits.
underflow (noun)
A situation where a node has fewer keys than the minimum allowed.
Deleting a key can cause underflow in a leaf node.
rotation (noun)
Moving a key between a node and its sibling through their shared parent.
A rotation fixes underflow by borrowing from a neighboring node.
merge (nodes) (verb)
To combine two nodes and a separating key into a single node.
The tree will merge an underful node with a sibling if borrowing isn't possible.
propagate (verb)
To spread or continue upward or outward through a structure.
A merge can propagate underflow further up the tree.
in-order traversal (noun)
A way of visiting a tree's nodes that produces them in sorted order.
An in-order traversal of a B-tree yields the sorted keys.
cache (noun)
A small, fast storage area used to hold frequently needed data.
The memory hierarchy model includes a fast cache and a slow disk.
descend (tree) (verb)
To move down from a node into one of its children.
The search algorithm descends into the correct child interval.
sibling (noun)
A node that shares the same parent as another node.
A rotation borrows a key from a neighboring sibling.
exempt (adjective)
Not required to follow a particular rule that applies to others.
The root is exempt from the minimum-key requirement.
structural rule (noun)
A requirement about how a data structure must be shaped or arranged.
Every B-tree must follow the same structural rules at every node.
borrow (a key) (verb)
To temporarily take an item from a neighbor to fix a shortage.
The underful node can borrow a key from a sibling with extra.
chain (of splits) (noun)
A series of connected events happening one after another.
A chain of splits can reach all the way to the root.
specification (noun)
A detailed description of exactly how something must be built or behave.
The B-tree specification fixes the range of children per node.
reduce (a problem) to (phrase)
To turn a problem into a simpler, already-solved version of itself.
Deleting from an internal node reduces to deleting from a leaf.
efficiently (adverb)
In a way that uses time or resources well, without waste.
B-trees store data efficiently for slow disk access.
finite (adjective)
Having a limited, countable size rather than being unlimited.
The cache is modeled as fast but finite in size.
trip (to disk) (phrase)
A single access to slow storage to fetch data.
Each trip to disk should retrieve as much useful data as possible.

Chapters

From the YouTube description

MIT 6.046J Design and Analysis of Algorithms, Spring 2015
View the complete course: http://ocw.mit.edu/6-046JS15
Instructor: Amartya Shankha Biswas

In this recitation, problems related to 2-3 Trees and B-Trees are discussed.

License: Creative Commons BY-NC-SA
More information at http://ocw.mit.edu/terms
More courses at http://ocw.mit.edu

← Lecture 3: Divide & Conquer: FFT · 4. Divide & Conquer: van Emde Boas Trees →