B+ Trees: Brian Lee CS157B Section 1 Spring 2006
B+ Trees: Brian Lee CS157B Section 1 Spring 2006
Table of Contents
History of B+ Trees Definition of B+ Trees Searching in B+ Trees Inserting into B+ Trees Deleting from B+ Trees Works Cited
History of B+ Trees
Description of B+ Trees
A variation of B-Trees B-Trees commonly found in databases and filesystems Sacrifices space for efficiency (not as much rebalancing required compared to other balanced trees)
4
B-Trees: data stored at every level (in every node) B+ Trees: data stored only in leaves
Internal nodes only contain keys and pointers All leaves are at the same level (the lowest one)
All nodes must have between ceil(n/2) and n keys (except for the root)
Searching in B+ Trees
Searching just like in a binary search tree Starts at the root, works down to the leaf level Does a comparison of the search value and the current separation value, goes left or right
If the tree is empty, add to the root Once the root is full, split the data into 2 leaves, using the root to hold keys and pointers If adding an element will overload a leaf, take the median and split it
10
Example:
11
12
In order to insert another number, like 5, we must split the root and create a new level.
13
14
Case 3: Adding to a full node Suppose we wanted to insert 7 into our tree:
15
7 goes with 5 and 6. However, since each node can only hold a maximum of 2 keys, we can take the median of all 3 (which would be 6), keep it with the left (5), and create a new leaf for 7. An alternative way is to keep the median with the right and create a new leaf for 5.
16
17
Case 4: Inserting on a full leaf, requiring a split at least 1 level up Using the last tree, suppose we were to insert 4.
18
We would need to split the leftmost leaf, which would require another split of the root.
A new root is created with the pointers referencing the old split root.
19
20
Deletion, like insertion, begins with a search. When the item to be deleted is located and removed, the tree must be checked to make sure no rules are violated.
The rule to focus on is to ensure that each node has at least ceil(n/2) pointers.
21
22
This would not require any rebalancing since the leaf that 5 was in still has 1 element in it.
23
24
Suppose we want to remove 6. This would require rebalancing, since removing the element 6 would require removal of the entire leaf (since 6 is the only element in that leaf).
25
Once we remove the leaf node, the parent of that leaf node no longer follows the rule. It has only 1 child, which is less than the 2 required ( ceil(3/2) = 2).
27
Bibliography
Wikipedia http://en.wikipedia.org/wiki/B_plus_tree
Silberschatz, Abraham, and Henry F. Korth, and S. Sundarshan. Database System Concepts. New York: McGraw-Hill, 2006.
28