B-Trees (and 2-3 and 2-3-4 Trees)
1
Lecture 17 (Data Structures 3)
CS61B, Spring 2025 @ UC Berkeley
Slides credit: Josh Hug
3 5
1 2
6 7
4
2
6
4
1
3
5
7
BST Height, Big O vs. Worst Case Big Theta
Lecture 17, CS61B, Spring 2025
Binary Search Trees
B-Trees
Big Theta vs. Big O
Common student question: If we have big theta, why do we also have big O?
The answer is the same as: If we have =, why do we also have ≤.
Let’s dig on this point, and also get a better feel for BSTs as we go.
BST Tree Height
Let’s start today by carefully discussing the height of binary search trees.
Trees range from best-case “bushy” to worst-case “spindly”.
e
b
g
a
d
f
j
v
p
y
m
r
x
z
k
v
y
z
k
H=3
H=3
Tree Height: http://yellkey.com/article
Height varies dramatically between “bushy” and “spindly” trees.
Let H(N) be the height of a tree with N nodes. Give H(N) in Big-Theta notation for “bushy” and “spindly” trees, respectively:
e
b
g
a
d
f
j
v
p
y
m
r
x
z
k
v
y
z
k
H=3
H=3
Tree Height
Height varies dramatically between “bushy” and “spindly” trees.
H = Θ(N)
H = Θ(log(N))
e
b
g
a
d
f
j
v
p
y
m
r
x
z
k
v
y
z
k
H=3
H=3
Performance of operations on spindly trees can be just as bad as a linked list!
Statements about Tree Height: http://yellkey.com/network
Which of these statements are true?
Statements about Tree Height
Which of these statements are true?
All are true!
Statements about Tree Height: http://yellkey.com/head
Which of these statements is more informative?
Statements about Tree Height
Which of these statements is more informative?
Saying that the worst case has order of growth N is more informative than saying the height is O(N).
�Let’s see an analogy.
Question: http://yellkey.com/compare
Which statement gives you more information about a hotel?
Question
Which statement gives you more information about a hotel?
Most expensive room: $639/nt
All rooms <= $639/nt
BST Height
BST height is all four of these:
The middle two statements are more informative.
The Usefulness of Big O
Big O is still a useful idea:
*: Under certain assumptions and constraints not listed.
BST Height
BST height is both of these:
Let’s now turn to understanding the performance of BST operations.
Worst Case Performance
Lecture 17, CS61B, Spring 2025
Binary Search Trees
B-Trees
Height and Depth
Height and average depth are important properties of BSTs.
v
p
y
r
z
k
e
b
g
a
d
f
j
s
depth 0
depth 1
depth 2
depth 3
depth 4
height 4
Height, Depth and Runtime
Height and average depth determine runtimes for BST operations.
v
p
y
r
z
k
e
b
g
a
d
f
j
s
depth 0
depth 1
depth 2
depth 3
depth 4
height 4
BSTs in Practice
Suppose we want to build a BST out of the numbers 1, 2, 3, 4, 5, 6, 7.
Give an example of a sequence of add operations that results in:
4
2
6
1
3
5
7
4
2
6
1
3
5
7
BSTs in Practice
Give an example of a sequence of add operations that results in:
4
2
6
1
3
5
7
4
2
6
1
3
5
7
Height: 6
Average Depth: 3
Height: 2
Average Depth: 1.43
Important Question: What about Real World BSTs?
BSTs have:
… but what about trees that you’d build during real world applications?
One way to approximate this is to consider randomized BSTs.
Simulation: Trees Built from Random Inserts
Video courtesy of Kevin Wayne (Princeton University)
Nice Property. Random trees have Θ(log N) average depth and height.
Randomized Trees: Mathematical Analysis
Average Depth. If N distinct keys are inserted into a BST, the expected average depth is ~ 2 ln N = Θ(log N).
Tree Height. If N distinct keys are inserted in random order, expected tree height is ~ 4.311 ln N (see Reed, 2003).
Note: ~ is the same thing as Big Theta, but you don’t drop the multiplicative constants.
Important Question: What about Real World BSTs?
BSTs have:
In real world applications we expect both insertion and deletion.
Good News and Bad News
Good news: BSTs have great performance if we insert items randomly. Performance is Θ(log N) per operation.
Bad News: We can’t always insert our items in a random order. Why?
e
b
g
o
n
p
m
q
r
s
Good News and Bad News
Good news: BSTs have great performance if we insert items randomly. Performance is Θ(log N) per operation.
Bad News: We can’t always insert our items in a random order. Why?
In this lecture, we’ll do something totally different.
e
b
g
o
n
p
m
q
r
s
Splitting Juicy Nodes
Lecture 17, CS61B, Spring 2025
Binary Search Trees
B-Trees
Avoiding Imbalance
The problem is adding new leaves at the bottom.
Crazy idea: Never add new leaves at the bottom.
5
2
7
15
14
16
13
17
18
19
?
?
?
?
Q: What do we do with incoming keys?
Avoiding Imbalance through Overstuffing
The problem is adding new leaves at the bottom.
Avoid new leaves by “overstuffing” the leaf nodes.
5
2
7
15
14
16
13
5
2
7
15
14
16 17
13
5
2
7
15
14
16 17 18
13
Avoiding Imbalance through Overstuffing
Overstuffed trees are a logically consistent but very weird data structure.
Q: What is the problem with this idea?
5
2
7
15
14
16 17 18 19
13
5
2
7
15
14
16 17 18 19 20 21 22 23 24
13
Revising Our Overstuffed Tree Approach
Height is balanced, but we have a new problem:
Solution?
5
2
7
15
14
16 17 18 19
13
Revising Our Overstuffed Tree Approach: Moving Items Up
Height is balanced, but we have a new problem:
�
Solution?
Q: What’s the problem now?
5
2
7
15
14
16 17 18 19
13
5
2
7
15 17
14
16 18 19
13
move up
Revising Our Overstuffed Tree Approach
Height is balanced, but we have a new problem:
�
Solution?
Challenge for you:
5
2
7
15 17
14
16 18 19
13
5
2
7
15
14
16 17 18 19
13
Revising Our Overstuffed Tree Approach: Node Splitting
Height is balanced, but we have a new problem:
�
Solution?
5
2
7
15 17
14
18 19
13
16
5
2
7
15
14
16 17 18 19
13
Revising Our Overstuffed Tree Approach: Node Splitting
This is a logically consistent and not so weird data structure.
Examining a node costs us O(L) compares, but that’s OK since L is constant.
What if a non-leaf node gets too full? Can we split that?
5
2
7
15 17
14
18 19
13
16
add Understanding Check
5
2
7
15 17
13
5
2
7
15 17
13
14
18 19
16
14
18 19 20 21
16
add Understanding Check
5
2
7
15 17 19
13
14
18
16
20 21
5
2
7
15 17
13
5
2
7
15 17
13
14
18 19
16
14
18 19 20 21
16
Chain Reaction Splitting
Lecture 17, CS61B, Spring 2025
Binary Search Trees
B-Trees
add: Chain Reaction
Suppose we add 25, 26:
5
2
7
15 17 19
13
14
18
16
20 21
5
2
7
15 17 19
13
14
18
16
20 21 25 26
5
2
7
15 17 19 21
13
14
18
16
25 26
20
5
2
7
13 17
19 21
18
25 26
20
14
16
15
What Happens If The Root Is Too Full?
Challenge: Draw the tree after the root is split.
19 21
15
5
13 17
19 21 22 23
15
5
13 17
22 23
15
5
13 17 21
19
22 23 24 25
15
5
13 17 21
19
24 25
15
5
13 17 21 23
19
22
What Happens If The Root Is Too Full?
Challenge: Draw the tree after the root is split.
22 23 24 25
15
5
13 17 21
19
24 25
15
5
13 17 21 23
19
22
24 25
15
5
21 23
19
22
17
13
B-Tree Terminology
Lecture 17, CS61B, Spring 2025
Binary Search Trees
B-Trees
Perfect Balance
Observation: Splitting-trees have perfect balance.
We will soon prove: All operations have guaranteed O(log N) time.
24 25
15
5
21 23
19
22
17
13
The Real Name for Splitting Trees is “B Trees”
Splitting tree is a better name, but I didn’t invent them, so we’re stuck with their real name: B-trees.
The origin of "B-tree" has never been explained by the authors. As we shall see, "balanced," "broad," or "bushy" might apply. Others suggest that the "B" stands for Boeing. Because of his contributions, however, it seems appropriate to think of B-trees as "Bayer"-trees.
- Douglas Corner (The Ubiquitous B-Tree)
A note on Terminology
B-Trees are most popular in two specific contexts:
2-3-4 a.k.a. 2-4 Tree (L=3):�Max 3 items per node.
Max 4 non-null children per node.
s u w
r
y z
t
n
p
o
e
b
g
m q
v
2-3 Tree (L=2):�Max 2 items per node.
Max 3 non-null children per node.
s u
r
t
n
p
o
e
b
g
m q
v
Invariants
Lecture 17, CS61B, Spring 2025
Binary Search Trees
B-Trees
Exercise: Recorded Video Viewers Only
Add the numbers 1, 2, 3, 4, 5, 6, then 7 (in that order) into a regular BST.
Then try adding 1, 2, 3, 4, 5, 6, then 7 (in that order) into a 2-3 tree (L=2).
Exercise
Add the numbers 1, 2, 3, 4, 5, 6, then 7 (in that order) into a regular BST.
Resulting BST is spindly.
4
2
6
1
3
5
7
Exercise
Add 1, 2, 3, 4, 5, 6, 7 (in that order) into a 2-3 tree. For L=2, pass the middle item up.
1
Adding 1.
1 2
Adding 2.
Adding 3. Node is too full, so we need to split.
1 2 3
Before split
After split
1
3
2
Exercise
Add 1, 2, 3, 4, 5, 6, 7 (in that order) into a 2-3 tree. For L=2, pass the middle item up.
3 4
1
2
Adding 4.
Adding 5. Node is too full, so we need to split.
Before split
After split
3 4 5
1
2
2 4
1
5
3
After adding 3.
1
3
2
Exercise
Add 1, 2, 3, 4, 5, 6, 7 (in that order) into a 2-3 tree. For L=2, pass the middle item up.
After adding 5.
2 4
1
5
3
Adding 6.
Adding 7. Node is too full, so we need to split.
5 6
2 4
1
3
5 6 7
2 4
1
3
2
6
4
1
3
5
7
2 4 6
1
5
3
7
Before split
After one split
After two splits
Exercise
Add the numbers 1, 2, 3, 4, 5, 6, then 7 (in that order) into a regular BST.
Then try adding 1, 2, 3, 4, 5, 6, then 7 (in that order) into a 2-3 tree.
1
2
3
4
5
6
7
2
6
4
1
3
5
7
All leaves are at depth 2.
Exercise 2: Recorded Video Viewers Only
Find an order such that if you add the items 1, 2, 3, 4, 5, 6, and 7 in that order, the resulting 2-3 tree has height 1.
Exercise 2
Find an order such that if you add the items 1, 2, 3, 4, 5, 6, and 7 in that order, the resulting 2-3 tree has height 1.
Not sure why? Make sure to see https://tinyurl.com/balanceYD.
No matter the insertion order you choose, resulting B-Tree is always bushy!
3 5
1 2
6 7
4
All leaves are at depth 1.
B-Tree Invariants
Because of the way B-Trees are constructed, we get two nice invariants:
We have not proven these invariants rigorously, but try thinking them through.
2 3
5 6 7
4
1
B-Tree Invariants
Because of the way B-Trees are constructed, we get two nice invariants:
These invariants guarantee that our trees will be bushy.
Worst Case Performance
Lecture 17, CS61B, Spring 2025
Binary Search Trees
B-Trees
Height of a B-Tree with Limit L
L: Max number of items per node.
Height: Between ~logL+1(N) and ~log2(N)
N: 26 items
L: 2 max per node
H: 2
Height grows with log3(N)
*
*
*
* *
*
*
*
N: 8 items
L: 2 max per node
H: 2
Height grows with log2(N)
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
near
worstcase
best
case
Runtime for contains
Runtime for contains:
Since H = Θ(log N), overall runtime is O(L log N).
Runtime for add
Runtime for add:
Since H = Θ(log N), overall runtime is O(L log N).
Bottom line: contains and add are both O(log N).
Summary
BSTs have best case height Θ(log N), and worst case height Θ(N).
B-Trees are a modification of the binary search tree that avoids Θ(N) worst case.
Extra slides cover deletion. Not in scope for our class.