B-Trees Study Guide
Depth and Height. We define the depth of a node as how far it is from the root. For consistency, we say the root has a depth of 0. We define the height of a tree as the depth of the deepest node. Notice that depending on how we insert into our BST, our height could vary drastically. We say a tree is “spindly” if it has height close to N and a tree is “bushy” if its height is closer to log N. For operations such as getting a node, we want to have the height to be as small as possible, thus favoring “bushy” BSTs
B-Trees. Two specific B-Trees in this course are 2-3 Trees (a B-Tree where each node has 2 or 3 children), and 2-3-4/2-4 Trees (a B-Tree where each node has 2, 3, or 4 children). The key idea of a B-Tree is to over stuff the nodes at the bottom to prevent increaseing the height of the tree. This allows us to ensure a max height of log N. Make sure you know how to insert into a B-Tree. With our restriction on height, we get that the runtime for contains
and add
are both Theta(log N).
B-Tree Invariants. Because of how we add to our tree, we get two nice invariants for B-Trees:
- 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.
Recommended Problems
- Draw the 2-3 tree that results when you insert the keys
A B C D E F G
in order. How many compares does it take in the worst case to decide whether to go left, middle, or right from a 3 node?
- Q4a from CS 61B 15sp MT2
- Q1c from CS 61B 18sp MT2
- Q3b from CS 61BL 18su MT3
- Q1 from CS 61BL 19su MT2
- Q8b from CS 61B 16sp MT2