According to Knuth’s definition, a B-tree of order m is a tree which satisfies the following properties:
- Every node has at most m children.
- Every non-leaf node (except root) has at least ⌈m/2⌉ children.
- The root has at least two children if it is not a leaf node.
- A non-leaf node with k children contains k−1 keys.
- All leaves appear in the same level
Each internal node’s keys act as separation values which divide its subtrees. For example, if an internal node has 3 child nodes (or subtrees) then it must have 2 keys: a1 and a2. All values in the leftmost subtree will be less than a1, all values in the middle subtree will be between a1 and a2, and all values in the rightmost subtree will be greater than a2.
- Internal nodes
- Internal nodes are all nodes except for leaf nodes and the root node.
- They are usually represented as an ordered set of elements and child pointers. Every internal node contains a maximum of U children and a minimum of L children. Thus, the number of elements is always 1 less than the number of child pointers (the number of elements is between L−1 and U−1).
- U must be either 2L or 2L−1; therefore each internal node is at least half full. The relationship between U and L implies that two half-full nodes can be joined to make a legal node, and one full node can be split into two legal nodes (if there’s room to push one element up into the parent). These properties make it possible to delete and insert new values into a B-tree and adjust the tree to preserve the B-tree properties.
- The root node
- The root node’s number of children has the same upper limit as internal nodes, but has no lower limit.
- For example, when there are fewer than L−1 elements in the entire tree, the root will be the only node in the tree with no children at all.
- Leaf nodes
- Leaf nodes have the same restriction on the number of elements, but have no children, and no child pointers.
B-trees keep values in every node in the tree, and may use the same structure for all nodes. However, since leaf nodes never have children, the B-trees benefit from improved performance if they use a specialized structure.
Let h be the height of the classic B-tree. Let n > 0 be the number of entries in the tree. d be the minimum number of children an internal (non-root) node can have. For an ordinary B-tree, .
可以直接当结论记忆，推导过程在《Introduction to Algorithms》中有详细论述。
因为B+树是B树的改进，所以可以通过两颗树的区别来学习B+树，以下来自Differences between B trees and B+ trees
- Advantages of B+ trees:
- Because B+ trees don’t have data associated with interior nodes, more keys can fit on a page of memory. Therefore, it will require fewer cache misses in order to access data that is on a leaf node.
- The leaf nodes of B+ trees are linked, so doing a full scan of all objects in a tree requires just one linear pass through all the leaf nodes.
- A B tree, on the other hand, would require a traversal of every level in the tree. This full-tree traversal will likely involve more cache misses than the linear traversal of B+ leaves.
- Advantage of B trees:
- Because B trees contain data with each key, frequently accessed nodes can lie closer to the root, and therefore can be accessed more quickly.