B Trees Slides
B Trees Slides
Robert Horvick
SOFTWARE ENGINEER
@bubbafat www.roberthorvick.com
B-tree Overview
- Minimal degree
Overview - Height
Searching
Adding
- Splitting Nodes
Removing
- Push down
- Rotation
B-tree
A sorted, balanced, tree structure typically used to
access data on slow mediums such as disk or tape
drives.
B-tree Properties
3 6 10
1 2 4 5 7 8 9 11 12
B-tree Properties
1
3 6 10
2 3 4 5
1 2 4 5 7 8 9 11 12
B-tree Properties
3 6 10
1 2 4 5 7 8 9 11 12
B-tree Properties
3 6 10
1 2 4 5 7 8 9 11 12
B-tree Properties
3 6 10
1 2 4 5 7 8 9 11 12
Smaller Larger
B-tree Properties
3 6 10
1 2 4 5 7 8 9 11 12
B-tree Properties
3 6
1 2 4 5 7 8 9
B-tree Properties
3 6 10 12
… … … … …
Nodes will always have one
more child than values
Minimal Degree
The minimum number of children that every non-root
node must have. Represented as the variable “T”.
Minimal Degree
3 6 10
1 2 4 5 7 8 9 11 12
T = 3 (Invalid)
3 6 10
1 4 5 7 8 9 11 12
T = 3 (Invalid)
3 6 10
1 4 5 7 8 9 11 12
T = 3 (Invalid)
3 6
1 2 4 5 7 8 9 10 11 12
T = 3 (Invalid)
3 6
1 2 4 5 7 8 9 10 11 12
Height
The number of edges between the root and the leaf
nodes. All B-tree leaf nodes must have the same height.
Height (Valid)
3 6 10
1 2 4 5 7 8 9 11 12
Height (Valid)
3 6 10
1
1 2 4 5 7 8 9 11 12
Height (Invalid)
3 6 15
1
1 2 4 5 7 10 14 16 17
2
8 9 11 12 13
Height (Invalid)
3 6 15
1
1 2 4 5 7 10 14 16 17
2
8 9 11 12 13
Searching
Searching
3 6 10
1 2 4 5 7 8 9 11 12
Smaller Larger
Searching
3 6 10
1 2 4 5 7 8 9 11 12
Searching
3 6 10
1 2 4 5 7 8 9 11 12
Searching
3 6 10
1 2 4 5 7 8 9 11 12
Searching
3 6 10
1 2 4 5 7 8 9 11 12
Searching
3 6 10
1 2 4 5 7 8 9 11 12
Searching
3 6 10
1 2 4 5 7 8 9 11 12
Searching
3 6 10
1 2 4 5 7 8 9 11 12
Searching
3 6 10
1 2 4 5 7 8 9 11 12
Searching
3 6 10
1 2 4 5 7 8 9 11 12
bool search(node, valueToFind)
Searching
Compare each value in the current node with the sought value, and then
recursively check every child’s value.
Searching node values can
be performed using a
binary search
Adding Values
Add Rules
Full nodes are split before the algorithm enters them (ensuring leaf
nodes are never full before adding an item)
add(T value) {
if Empty
Root = new BTreeNode(value)
else {
if Root.Full
SplitRootNode(Root)
InsertNonFull(Root, value)
}
Count++
}
if (child.Full)
SplitChildNode(node)
InsertNonFull(child, value)
}
}
3
Adding Values
3 5
Adding Values
3 4 5
Adding Values
1 3 4 5
Adding Values
1 2 3 4 5
Adding Values
1 2 3 4 5 6
Adding Values
1 2 3 4 5
6
Adding Values
1 2 3 4 5
Adding Values
1 2 3 4 5
Adding Values
1 2 3 4 5
Adding Values
1 2 4 5
Adding Values
3
1
1 2 4 5
Splitting the root is the only
way that the B-tree height
increases
Adding Values
1 2 4 5
Adding Values
1 2 4 5
Adding a Value to a Leaf Node (T=3)
1 2 4 5 6
Root Node Splitting
Splitting two child nodes out of the root node to prevent
the root node from exceeding the maximum number of
values allowed by the B-tree degree.
Adding a Value to a Full Leaf (T=3)
1 2 4 5 6 7 8
Adding a Value to a Full Leaf (T=3)
1 2 4 5 6 7 8 9
Adding a Value to a Full Leaf (T=3)
1 2 4 5 6 7 8
Adding a Value to a Full Leaf (T=3)
1 2 4 5 6 7 8
Adding a Value to a Full Leaf (T=3)
1 2 6
4 5 7 8
Adding a Value to a Full Leaf (T=3)
3
1
1 2 6
2
4 5 7 8
Adding a Value to a Full Leaf (T=3)
1 2 4 5 6 7 8
Adding a Value to a Full Leaf (T=3)
1 2 4 5 6 7 8
Adding a Value to a Full Leaf (T=3)
3 6
1 2 4 5 7 8
Adding a Value to a Full Leaf (T=3)
3 6
1 2 4 5 7 8
Adding a Value to a Full Leaf (T=3)
3 6
1 2 4 5 7 8
Adding a Value to a Full Leaf (T=3)
3 6
1 2 4 5 7 8
Adding a Value to a Full Leaf (T=3)
3 6
1 2 4 5 7 8 9
Node Splitting
Splitting a child value in half by pulling the middle value
up to the parent.
Removing Values
Remove Rules
Ensure that all nodes visited during the remove process have T values
before entering them
Balancing Operations
3 6 9
1 2 4 5 7 8 10 11
Pushing Down
3 6 9
1 2 4 5 7 8 10 11
Pushing Down
3 9
1 2 4 5 7 8 10 11
Pushing Down
3 9
1 2 4 5 7 8 10 11
Pushing Down
3 9
1 2 4 5 7 8 10 11
Pushing Down
3 6 9
1 2 4 5 7 8 10 11
Pushing Down
3 6 9
1 2 4 5 7 8 10 11
Pushing Down
3 9
1 2 4 5 6 7 8 10 11
Pushing Down
3 6 9
1 2 4 5 7 8 10 11
Pushing Down
3 6 9
1 2 4 5 7 8 10 11
Pushing Down
3 6 9
1 2 4 5 7 8 10 11
Pushing Down
3 9
1 2 4 5 6 7 8 10 11
Pushing Down
3 9
1 2 4 5 6 7 8 10 11
Pushing Down
3 9
1 2 4 5 7 8 10 11
Push Down Minimal Root
When the root has a single value and two minimal
children, they can be combined with it to form a full root
node.
Pushing Down Minimal Root
1 2 4 5
.. .. .. .. .. .. .. .. .. .. .. ..
Pushing Down Minimal Root
1 2 4 5
.. .. .. .. .. .. .. .. .. .. .. ..
Pushing Down Minimal Root
1 2 4 5
.. .. .. .. .. .. .. .. .. .. .. ..
Pushing Down Minimal Root
1 2 4 5
.. .. .. .. .. .. .. .. .. .. .. ..
Pushing Down Minimal Root
1 2 3 4 5
.. .. .. .. .. .. .. .. .. .. .. ..
Rotation
Rotating a value from a non-minimal child, to a sibling
minimal child
Rotation
3 9
1 2 4 5 7 8 10 11
Rotation
3 9
1 2 4 5 7 8 10 11
Rotation
3 9
1 2 4 5 7 8 11
Rotation
3 9
1 2 4 5 7 8 10 11
Rotation
1 2 4 5 7 8 9 10 11
Right Rotation
3 9
1 2 4 5 7 8 10 11
Right Rotation
3 9
1 2 4 5 7 8 ? 10 11
Right Rotation
3 9
1 2 4 5 7 8 ? 10 11
Right Rotation
3 ?
1 2 4 5 7 8 9 10 11
Right Rotation
3 ?
1 2 4 5 7 8 9 10 11
Right Rotation
3 8
1 2 4 5 7 9 10 11
Right Rotation
3 9
1 2 4 5 7 8 10 11
Right Rotation
3 9
1 2 4 5 7 8 10 11
Right Rotation
3 8
1 2 4 5 7 9 10 11
Right Rotation
3 8
1 2 4 5 7 9 10 11
Right Rotation
3 8
1 2 4 5 7 9 11
Left Rotation
3 9
1 2 4 5 7 8 10 11
Left Rotation
3 9
1 2 4 5 7 8 10 11
Left Rotation
3 9
1 2 4 5 7 8 10 11
Left Rotation
4 9
1 2 3 5 7 8 10 11
Left Rotation
4 9
1 2 3 5 7 8 10 11
Left Rotation
4 9
1 3 5 7 8 10 11
Balancing Operations
Pushing Down
Used when removing a value would leave a child node with too few values, the
parent has multiple values, and the sum of the adjacent child and the current
child is less than 2*T-1.
Rotation
Used when removing a node and pushing down would not work because the
parent has too few values, but a sibling has a value to spare.
Splitting Nodes
Used when adding a node and the destination node would have too many
values.
Demo Sample B-tree Implementation
- Search
- Add
- Remove
- Balancing Operations