Multi balanced tree --B-Tree (B tree)

  Keywords: M-order, B trees, split, merge

  Preface:

    Prior to explain in detail, there is little to emphasize again the next: B- tree, is the B-tree. Because the original English name of the B-tree is B-tree, and many people like the domestic B-tree translated B- tree, in fact, this is a very bad translation, it is easy to misunderstand. As people might think B- tree is a tree, while the B-tree is a kind of tree. And in fact, B-tree refers to the B-tree. (B <=> B- Tree Tree). Quoted from: https://www.cnblogs.com/yfzhou/p/10290006.html

1, B tree defines:

  For the B-tree, we generally described as M (M> 2) Order tree B (M Order herein refers to the maximum number of subtrees of all nodes in the tree). For the B-tree, it must meet the following properties:
   Nature :
  (1) the nature of the node:
    • All leaf nodes are at the same level;
    • Each node consists of a number and composition of the recording pointer . Wherein each node record number 1 smaller than the number of pointers, the pointer recorded every other.
  (2) the nature of the pointer:
    • Each pointer to a child node;
    • Each node has a pointer up to M;
    • Non-leaf nodes of the root least two branches;
    • Leaf node contains a minimum of two records and a null pointer, the pointers are leaf nodes NULL;
    • Non-root non-leaf node has at least ceil ( M / 2 ) branches; (ceil ( M / 2 )> =. 3) (Analysis: When this value is equal to 2, then it becomes a binary tree; When this value is equal to 1, becomes the list, so this value must be greater than or equal to 3)
    • For any node: ceil (M / 2) <= d <= m. (Can be derived from the point) (d: indicates the degree of each node)
  (3) the nature of the record:
    • Each recorded by the [key, Data] composition, while the size of each record by the key sit right non descending;
    • For any node in a record, all key values ​​recorded in the child node pointer which points to the left are smaller than the current record, is greater than the right pointer of the current record;
    • For any node: ceil (M / 2) -1 <= k_count <= m-1. (K_count: indicates the number of records for each node)

A B-tree as follows:

2, the characteristics of B-tree summary:

(1) Each node stores a [key, data]
(2) any one keyword appears once and only in one node;
(3) it is possible to search for the end of the non-leaf node;
(4) due to the limitations M / 2, and when the insertion node, if the node is full, the node needs to be split into two nodes each accounted M / 2; and delete nodes need to be two less than M sibling / 2 combined; (insert, delete operation to be solved the issue) ;
(5) B trees, like red-black trees are self-balancing trees.

3, B tree search:

(1) Search Process :

① starts from the root node, the nodes of the keyword in order to proceed equivalence determination;

② If the hit is finished, the current record is the corresponding keyword you want to find;

③ If there is no hit, the process proceeds to the son node belongs to the scope of the query key, the operation ① is repeated until the pointer is null corresponding to the son, or has been a leaf node. At this point then the tree does not exist in this keyword.

(2) search algorithm pseudocode on B-Tree as follows:

1. BTree_Search(node, key) {
2.     if(node == null) return null;
3.     foreach(node.key)
4.     {
5.         if(node.key[i] == key) return node.data[i];
6.         if(node.key[i] > key) return BTree_Search(point[i]->node);
7.     }
8.     return BTree_Search(point[i+1]->node);
9. }
10. data = BTree_Search(root, my_key);

4、B树的插入、删除:

前言 修复方式之分裂、合并、转移:
由于插入、删除数据记录会破坏B-Tree的性质,因此在插入删除时,需要对树进行一个分裂、合并、转移等操作以保持B-Tree性质。
①分裂:拆分当前节点的中间节记录到上层,与上层的节点的记录进行合并操作;
②合并:将记录与上层节点的记录进行合并;
③转移:需要不断将当前记录与其的相邻下级子节点中的记录进行交换,直到当前记录处于叶子节点中。(如果操作的节点记录不在叶子节点,那么就需要进行转移操作)
 
(1)插入:B树的插入总是在叶子节点上
如果插入新的数据记录到某个节点之后,节点的记录个数大于等于M,那么需要进行分裂操作。
2)删除:
①如果节点不在叶子节点上,那么需要把它替换到叶子节点上。然后对叶子节点执行删除操作;
②如果删除当前节点的某个记录之后,节点的记录个数小于ceil(M/2),那么需要进行合并操作;
③如果合并之后,上级节点记录个数小于等于ceil(M/2),那么此时首先判断当前节点的兄弟节点的记录个数是否在小于ceil(m/2),如果不是,那么取兄弟节点的一个记录与上级节点进行合并。否则,只能从上级节点的上级节点拉取一个记录下来进行合并。
 
注意:
插入、删除的示例详见https://www.cnblogs.com/yfzhou/p/10290006.html
②体情况具体分析。遇到连锁反应(eg:分裂之后有需要分裂),就继续处理呗。

5、B树的节点的存储:

  虽然B-Tree中不同节点存放的key和指针可能数量不一致,但是每个节点的域和上限是一致的,所以在实现中B-Tree往往对每个节点申请同等大小的空间。
 
 
引用: 

Guess you like

Origin www.cnblogs.com/axing-articles/p/11442726.html