Still don’t understand B-tree and B+-tree? Then I suggest you click here to learn!

B tree

Before introducing the B+ tree, let's briefly introduce the B tree. These two data structures have similarities and differences. Finally, we will compare the differences between the two data structures.

It's the golden recruitment period of 9 and 10 gold, are you all ready? I collected the interview questions of some major manufacturers and the latest information of this year (2020). The following are some screenshots of the information (all the information has been integrated into a document, and the pdf is compressed and packaged).
If you have a friend in need, you can click here to get information, code: qf

Insert picture description here

B-tree concept

B-tree is also called B-tree, it is a multi-way balanced search tree. I think everyone is familiar with binary trees. In fact, the B-tree and the B+ tree mentioned later are also transformed from the simplest binary tree. There is nothing mysterious about it. Let’s take a look at the definition of B-tree.

  • Each node has at most m-1 keywords (key-value pairs that can be stored).
  • The root node can have at least one keyword.
  • The non-root node has at least m/2 keywords.
  • The keywords in each node are arranged in ascending order, all keywords in the left subtree of each keyword are smaller than it, and all keywords in the right subtree are larger than it.
  • All leaf nodes are located in the same layer, or the length from the root node to each leaf node is the same.
  • Each node stores index and data, that is, the corresponding key and value.

Therefore, the range of the number of keywords for the root node: 1 <= k <= m-1, and the range of the number of keywords for non-root nodes: m/2 <= k <= m-1.
In addition, we need to pay attention to a concept. When describing a B-tree, we need to specify its order. The order indicates the maximum number of child nodes of a node. Generally, the letter m is used to indicate the order.
Let's take another example to illustrate the above concept. For example, here is a 5th-order B-tree, the range of the number of root nodes: 1 <= k <= 4, and the range of the number of non-root nodes: 2 <= k <= 4.
Below, we explain the insertion process of the B-tree through an example of insertion, and then explain the process of deleting keywords.

B-tree insertion

When inserting, we need to remember a rule: judge whether the number of key of the current node is less than or equal to m-1, if it is satisfied, just insert it directly, if it is not satisfied, divide the key in the middle of the node into left and right Two parts, the middle node can be placed in the parent node.

Example: In a 5-level B-tree, the node has at most 4 keys and at least 2 keys (note: the following nodes use one node to represent key and value).

When inserting 18, 70, 50, 40
Insert picture description here
into 22 and
Insert picture description here
inserting 22, it is found that the key of this node is greater than 4, so it needs to be split. The rules of split have already been mentioned above. After splitting, it is as follows.
Insert picture description here
Then insert 23
Insert picture description here
, 25 , 39 split to get the following.
Insert picture description here

I won’t introduce the more advanced insertion process. I believe you already know how to perform the insertion operation with this example.

B-tree delete operation
B-tree delete operation is relatively more complicated than insert operation, but you know that you can easily grasp several situations in mind.

Now there is an initial state of a B-tree like the following, and then delete it.
Insert picture description here
Delete 15. In this case, the element of the leaf node is deleted. If the number of nodes is still greater than m/2 after deletion, just delete it directly.
Insert picture description here

Next, we delete 22. The rule in this case: 22 is a non-leaf node. For the deletion of a non-leaf node, we need to overwrite the key to be deleted with the subsequent key (element), and then delete the sub-branch where the subsequent key is located. Successor key. For deleting 22, the subsequent element 24 needs to be moved to the node where the deleted 22 is located.
Insert picture description here
At this time, it is found that the node where 26 is located has only one element, which is less than 2 (m/2). This node does not meet the requirements. The rule at this time (borrowing elements from sibling nodes): If the leaf node is deleted, if the number of elements after deleting the element Less than (m/2), and the element of its sibling node is greater than (m/2), that is to say, the element of the sibling node is more than the minimum value m/2, the element of the parent node will be moved to this node first, Then move the element of the sibling node to the parent node. This meets the requirements.

We can see the operation process more clearly.
Insert picture description here
Then delete 28 and delete the leaf nodes. After deletion, the requirements are not met. Therefore, we need to consider borrowing elements from sibling nodes. However, there are not many sibling nodes (2), so what should I do? If this happens, first, move the element of the parent node to the node first, and then merge the keys in the current node and its sibling nodes to form a new node.
Insert picture description here
After moving, merge with sibling nodes.
Insert picture description here
Only the above cases can be deleted, and you can delete them according to different situations.

The above introduction, I believe that you already have a certain understanding of the B tree, the next part, we will continue to explain the B+ tree, I believe that the comparison of the B+ tree will be more clear.

B+ tree

B+ tree overview

The B+ tree is actually very similar to the B tree. Let’s first look at the similarities:

  • At least one element at the root node

-Non-root node element range: m/2 <= k <= m-1

difference:

-B+ tree has two types of nodes: internal nodes (also called index nodes) and leaf nodes. Internal nodes are non-leaf nodes. Internal nodes do not store data, only indexes, and data is stored in leaf nodes.
-The keys in the internal node are arranged in ascending order. For a key in the internal node, all the keys in the left tree are less than it, and the keys in the right subtree are greater than or equal to it. The records in the leaf nodes are also arranged according to the size of the key.
-Each leaf node stores pointers to adjacent leaf nodes, and the leaf nodes themselves are linked in order of the size of the keyword from small to large.
The parent node stores the index of the first element of the right child.

Let's look at an example of a B+ tree and feel it!
Insert picture description here
2.2 Insert operation

The insertion operation is very simple, only need to remember one trick: when the number of node elements is greater than m-1, the middle element is split into two parts, the middle element is split to the parent node as index storage, but the middle element itself Still split the right part.

The following takes the insertion process of a 5th-order B+ tree as an example. The nodes of the 5th-order B+ tree have at least 2 elements and at most 4 elements.

Insert 5, 10, 15, 20 and
Insert picture description here
insert 25. At this time, the number of elements is greater than 4, split and
Insert picture description here
then insert 26, 30, and continue to split.
Insert picture description here
With these examples, I believe that there is no problem with the insertion operation. Let's look at the delete operation.
The delete operation
is simpler than the B-tree operation for the delete operation, because the leaf node has a pointer, when borrowing elements from the sibling node, it does not need to go through the parent node, but can be moved directly through the sibling node (the premise is that the sibling node If the element of the sibling node is greater than m/2), then update the index of the parent node; if the element of the sibling node is not greater than m/2 (the sibling node has no extra elements), merge the current node and the sibling node, and delete the parent node key, let's take a look at specific examples.

Initial state

Insert picture description here
Delete 10. After deleting, it does not meet the requirements. It is found that the left sibling node has redundant elements, so I borrow the element. Finally, modify the parent node index to
Insert picture description here
delete element 5. It is found that the requirements are not met, and there are no redundant elements on the left and right sibling nodes. Therefore, you can choose to merge with the sibling node, and finally modify the parent node index. It is
Insert picture description here
found that the parent node index does not meet the conditions, so you need to do the same operation as the previous step. In
Insert picture description here
this way, the deletion of the B+ tree is completed. Is it right? , It feels very simple!

03 Summary of B tree and B+ tree

B+ tree has some advantages over B tree, which can be summarized as the following points.

A single node stores more elements, which makes the number of query IOs less, so it makes it more suitable as the underlying data structure of the database MySQL.
All queries must find leaf nodes, and the query performance is stable, while in B-trees, each node can find data, so it is unstable.
All leaf nodes form an ordered linked list, which is easier to find.

End of sentence

This is the end of this article about B-tree and B+-tree sharing. In addition, I have collected more than 20 years of company interview knowledge points, as well as various Java core knowledge points for free to share with everyone. If you want information, please click The code here is qf.
Insert picture description here

Guess you like

Origin blog.csdn.net/SpringBoot_/article/details/108835773