2-3 Trees and B-Trees 1 Recap: 1.1 Balanced Binary Search Trees
2-3 Trees and B-Trees 1 Recap: 1.1 Balanced Binary Search Trees
1 Recap
1.1 Balanced Binary Search Trees
Binary Search Trees that guarantee O(log(n)) height by rebalancing after Insert/Delete operations.
4 11 4 11 7
2
2 6 2 6 1
6 11
2 2-3 Trees
2.1 Properties
2-3 Trees are balanced search trees. Every node with children (non-leaf) has either two children
(2-node) and consists of one piece of data, or has three children (3-node) and consists of 2 pieces
of data.
3-Node 2-Node
A B A
C D E B C
30
10 17 38
3 7 14 20 24 32 41 48
2.3 Search
Very similar to Binary Search. Start at the root node, and traverse down tree in order. Tree is sorted
so only need to look at one node for each level of tree. Because of this Search runtime is O(lg(n))
Search(14)
30
10 17 38
3 7 14 20 24 32 41 48
Recitation 2: 2-3 Trees and B-Trees 3
2.4 Insert
Insert(X) Steps:
• While there is overflow, a node has more than 3 elements, Split node into left half, median,
and right half Then promote median up a level. If there is a parent, add it to the node. If
there is no parent, create a new node of just the Median node as the new root.
• Runtime is O(lg(n))
4 Recitation 2: 2-3 Trees and B-Trees
2.5 Delete
Delete(X) Steps:
• Swap item to delete with inorder successor if item is not already a leaf.
• Redistribute and merge nodes if there is underflowing in order to get back to a correct 2-3
tree
• Runtime is O(lg(n))
Recitation 2: 2-3 Trees and B-Trees 5
'HOHWH
'HOHWH
'HOHWH
'HOHWH
'HOHWH
6 Recitation 2: 2-3 Trees and B-Trees
24
Delete 30
10 32
7 20 30 41
10 24
Delete 30
7 20 32 41
3 B-Trees
B-Trees are tree data structures that store sorted data. B-Trees can be seen as a generalization of
Binary Search Trees where nodes can have more than one key/value and more than two children.
Similar to BSTs, they support search, insertion and deletion in logarithmic time.
3.1 Properties
A B-tree has a parameter called the minimum degree or branching factor. For the purposes of our
discussion let the branching factor be B.
• For any non leaf node, the number of children is one greater than the number of keys in that
node.
• Every non-root node contains at least B − 1 keys. Consequently, all internal (non-leaf and
non-root) nodes have at least B children.
• Every node contains at most 2B − 1 keys. Consequently, all nodes have at most 2B children.
The keys is a B-tree are sorted in a similar fashion to BSTs. Consider a node x with C children.
Let’s say that x has keys k1 < k2 < ... < kC . For ease of notation, we define k0 = ∞ and
kn + 1 = −∞. If K belongs to the ith (1 ≤ i ≤ n + 1) sub-tree of x, then ki−1 ≤ K ≤ ki .
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.