```
B-Trees
Growing from the root rather than the leaves: a first look at balanced search tree structures.
Kevin Lin, with thanks to many others.
1
B-Tree Invariants
All leaves must be the same depth from the root.
A non-leaf node with K keys must have exactly K + 1 non-null children.
These invariants guarantee bushy trees.
The tree to the right is not a possible B-Tree based on these invariants.
2
2 3
5 6 7
4
1
2-3 Tree Insertion
Draw the 2-3 tree that results from inserting these items in order: 8, 2, 4, 5, 7, 9.
3
Q
Q1: Draw the 2-3 tree that results from inserting these items in order: 8, 2, 4, 5, 7, 9.
2-3 Tree Insertion
Draw the 2-3 tree that results from inserting these items in order: 8, 2, 4, 5, 7, 9.
4
A
2-3 Tree Insertion
Draw the 2-3 tree that results from inserting these items in order: 8, 2, 4, 5, 7, 9.
5
A
2 4 8
4
2
5 7 8
4 7
2
8 9
5
Max-Height and Min-Height 2-3 Trees
Give an insertion order for the keys 1, 2, 3, 4, 5, 6, 7 that results in a max-height 2-3 Tree.
What about for a min-height 2-3 Tree?
6
Q
?: What is the least number of keys we can stuff into a 2-3 Tree node? The greatest number of keys?
Q1: Give an insertion order for the keys 1, 2, 3, 4, 5, 6, 7 that results in a max-height 2-3 Tree.
Q2: What about for a min-height 2-3 Tree?
Max-Height and Min-Height 2-3 Trees
Give an insertion order for the keys 1, 2, 3, 4, 5, 6, 7 that results in a max-height 2-3 Tree.
What about for a min-height 2-3 Tree?
7
A
Max-Height and Min-Height 2-3 Trees
Give an insertion order for the keys 1, 2, 3, 4, 5, 6, 7 that results in a max-height 2-3 Tree.
What about for a min-height 2-3 Tree?
8
A
2
6
4
1
3
5
7
3 5
1 2
6 7
4
1, 2, 3, 4, 5, 6, 7
2, 3, 4, 5, 6, 1, 7
Height of a B-Tree with Node-Item Limit L
Largest possible height is all non-leaf nodes have just 1 key.
H(N) ~ log2(N) ∈ Θ(log N)
Smallest possible height is all nodes have L keys.
H(N) ~ logL + 1(N) ∈ Θ(log N)
9
*
*
*
* *
*
*
*
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
N=8, L=2, H(N) = 2
N=26, L=2, H(N) = 2
Search Runtime
Worst case number of nodes to inspect: H + 1
Worst case number of keys to inspect per node: L
Overall runtime: O(HL)
Since H(N) ∈ Θ(log N) and L is a constant, overall runtime is in O(log N).
10
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
?: Describe the procedure for searching in a B-Tree.
Insertion Runtime
Worst case number of nodes to inspect: H + 1
Worst case number of keys to inspect per node: L
Worst case number of split operations: H + 1
Overall runtime: O(HL)
Since H(N) ∈ Θ(log N) and L is a constant, overall runtime is O(log N).
11
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
?: How do we get the worst case number of split operations?
?: This assumes each split operation takes constant time. Give examples that demonstrate why this is the case.
2-3 Tree Insertion Order
Give two insertion orders that both result in the following 2-3 tree.
12
Q
4
2
6 8
1
5
7
9
3
Q1: Give two insertion orders that both result in the following 2-3 tree.
2-3 Tree Insertion Order
Give two insertion orders that both result in the following 2-3 tree.
13
A
4
2
6 8
1
5
7
9
3
2-3 Tree Insertion Order
Give two insertion orders that both result in the following 2-3 tree.
2, 4, 6, 1, 3, 5, 7, 8, 9
4, 2, 6, 1, 3, 5, 7, 8, 9
14
A
4
2
6 8
1
5
7
9
3
2-3 Tree Shapes
What is the minimum number of items that we can insert and increase the height?
What is the maximum number of items that we can insert and not increase the height?
15
Q
13
4 7
19 22
0 1
6
8 9
18
20
25
Q1: What is the minimum number of items that we can insert and increase the height?
Q2: What is the maximum number of items that we can insert and not increase the height?
2-3 Tree Shapes
What is the minimum number of items that we can insert and increase the height?
What is the maximum number of items that we can insert and not increase the height?
16
A
13
4 7
19 22
0 1
6
8 9
18
20
25
2-3 Tree Shapes
What is the minimum number of items that we can insert and increase the height?
3 items. Insertion order: 10, 26, 27.
What is the maximum number of items that we can insert and not increase the height?
17
A
13
4 7
19 22
0 1
6
8 9
18
20
25
2-3 Tree Shapes
What is the minimum number of items that we can insert and increase the height?
3 items. Insertion order: 10, 26, 27.
What is the maximum number of items that we can insert and not increase the height?
13 items. Finding insertion order is challenging: 5, 24, 26, 14, 16, 15, 17, 21, 28, 29, 23, 27, 30.
18
A
13 22
4 7
25 28
16 19
0 1
5 6
8 9
14 15
17 18
20 21
23 24
26 27
29 30
```