Btree and B + tree
[TOC]
reference resources:Detailed explanation of Btree and B + tree
preface
B + tree is a kind of storage structure, which is often used in database indexing.
Prepare knowledge
Morder tree: the maximum bifurcation of the tree is m, that is, the maximum number of child nodes is m;
Root node: node without parent node;
Leaf node: node without child node;
Internal node: a node that is not a root or leaf node;
Binary search tree: the value in the left subtree is greater than the value of the root nodeSmall, the value in the right subtree is greater than the value of the root nodelarge；
Balanced binary tree: special case of binary search tree, left and right subtreeheightThe same;
1、 Btree
B number is equilibriummanyFork tree, a Btree of order m, has the following characteristics:
 The internal node must have at least ceil (M / 2) child nodes;
 When the root node is not a leaf node, it must have at least two child nodes, that is, order 2;
 Nodes of order m contain M1 data；
 All leaf nodes have the same height;
2、 B + tree
B + tree is a variant of B tree. Some changes have been made to its rules. The changes are as follows:
 The leaf node consists of an ordered array and a pointer to the right leaf node;

Non leaf nodes consist of an ordered array, but array elements consist of aIndex valueIt consists of a pointer;
 Pointer: points to a leaf node;
 Index value: the smallest index value in the leaf node pointed to;
 The non leaf node is a tool node, which is used to quickly find the specified leaf node. Only the leaf node stores the real data (a row of data);
 leafNodesSimilar to an ordered linked list;
 The node of order m contains M data;
2.1 why is B + tree suitable for database?
 B + tree is convenient for range query, which is the most important.
You just need to find the leftmost range. After finding it, you can traverse the leaf node to the right until you meet the end of the right range. In this way, you can screen out all the data in the range.
B tree’s scope search uses middle order traversal, while B + tree uses traversal on linked list;
 The cost of disk reading and writing of B + tree is lower.
The internal nodes of the B + tree do not have pointers to the specific information of keywords. Therefore, its internal nodes are smaller than Btree. If all the keywords of the same internal node are stored in the same disk, the more keywords the disk can hold. The more keywords that need to be searched are read into memory at one time. Relatively speaking, the number of IO reads and writes is reduced;
 The query efficiency of B + tree is more stable
Because the non endpoint is not the node that finally points to the file content, but the index of keywords in the leaf node. So any keyword search must take a path from root node to leaf node. The path length of all keyword queries is the same, resulting in the same query efficiency of each data;