B-tree (B-tree) insertion and deletion operations

1. Definition

A B-tree is a balanced multipartite tree, usually we say a B-tree of order m, which must meet the following conditions:
(1) Each node has at most m child nodes;
(2) Except for the root node and leaf node , each other node has at least one ceil(m/2)child node;
(3) The root node has at least two child nodes; (the only exception is that the root node is a leaf node)
(4) All leaf nodes are in the same layer;
(5) A non-root node with k child nodes contains exactly k-1 keys, and the keys are arranged in increasing order.

ceil stands for round up

2. Find

The search of the B-tree is very simple. It is an extension of the binary sorting tree. The binary sorting tree is a two-way search, and the B-tree is a multi-way search, because the keywords in the nodes of the B-tree are ordered. When searching within a point, in addition to sequential search, you can also use halved search to improve efficiency. The specific search steps of the B-tree are as follows (assuming the search key is key):

(1) First, compare the key with the keyword in the root node. If the key is equal to k[i] (k[] is the keyword array in the node), the search is successful;

3. Insert

Take a B-tree of order 3 as an example:

(1) If the number of keywords of the node does not reach 2, then insert it directly;
(2) If the number of keywords of the node has reached 2, then obviously it cannot be satisfied according to the nature of the B-tree , which needs to be split

The rule of splitting is that the node is divided into two halves, and the keywords in the middle are promoted and added to the parent node, but there may be a situation where the parent node is also full, so you have to backtrack upwards, even to correct The root node is split, then the whole tree is added one layer.

Example 1 ——————————————

write picture description here


Example 2 ——————————————-

write picture description here

write picture description here

write picture description here


4. Delete

First of all, it needs to be clear: deleting non-leaf nodes will inevitably lead to not satisfying the B-tree properties

Then it can be handled like this: the deleted keyword is the i-th keyword key[i] in the node, then the smallest keyword Y can be found from the subtree pointed to by the pointer son[i], instead of key[i] , then delete Y from the leaf node. Therefore, the problem of deleting the keyword k in the non-leaf node becomes the problem of deleting the keyword in the leaf node,

Then the deletion operation of the B-tree becomes the problem of deleting the keywords in the leaf nodes.

(1) If the number of keywords in the node where the deleted keyword Ki is located is not less than ceil(m/2), then it is only necessary to delete Ki and the corresponding pointer Ai from the node, and the rest of the tree remains unchanged.

write picture description here

(2) If the number of keywords in the node where the deleted keyword Ki is located is equal to ceil(m/2)-1, it needs to be adjusted.
write picture description here

(3) The number of keywords in the node where the deleted keyword Ki is located and its adjacent sibling nodes is equal to ceil(m/2)-1, assuming that the node has a right sibling, and the address of its right sibling node is equal to ceil(m/2)-1. Pointed to by its parent node pointer Ai. Then after the keyword is deleted, the remaining keywords and pointers of the node where it is located, together with the keyword Ki in the parent node, are merged into the sibling node pointed to by Ai (if there is no right sibling, it is merged into the left sibling. node). If the number of keywords in the parent node is therefore less than ceil(m/2)-1, and so on.

write picture description here

write picture description here

write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325995996&siteId=291194637