B, B-, B +, and B *

Source: Internet
Author: User

B, B-, B +, and B *

 

B tree

That is, the binary search tree:

1. all non-leaf nodes have at most two sons (Left and Right); 2. store a keyword on all nodes. 3. the left pointer of a non-leaf node points to the subtree smaller than its keyword, And the right Pointer Points to the subtree larger than its keyword;

For example:

B-tree search starts from the root node. If the query keyword is the same as the node keyword, it hits;
Otherwise, if the query keyword is smaller than the node keyword, it enters the left son; if it is larger than the node keyword, it enters the right son;
If the pointer of the Left or Right son is null, the corresponding keyword cannot be found in the report;

If the number of left and right Subtrees of all non-leaf nodes of Tree B is almost (balanced), the search performance of Tree B approaches binary search;
However, it has the advantage of Binary Search over continuous memory space: Changing the B-tree structure (insertion and deletion of nodes) does not require moving large segments of memory data, or even constant overhead;

However, after multiple inserts and deletions, Tree B may have different structures:

The right side is also a B-tree, but its search performance is linear. The same keyword set may lead to different tree indexes. Therefore, to use Tree B, we should also consider keeping the structure of the Left graph as much as possible, and avoiding the structure of the right graph, that is, the so-called "balance" problem;

The B-tree actually used is based on the original B-tree with a balanced algorithm, that is, the "balanced binary tree ";The key to balancing Binary Trees is how to maintain even distribution of B-Tree nodes.The balance algorithm is a strategy for inserting and deleting nodes in Tree B;

B-tree

Is a multi-path search tree (not binary ):

1) each node x has the following attributes:-node x has n keywords-the keywords themselves are not sorted in descending order-there is a Boolean value indicating whether the node is a leaf node 2) each internal node x also contains n + 1 pointer to the child. 3) each leaf node has the same depth. 4) each node contains an upper and lower bound Number of keywords, using the minimum degree t> = 2 of Tree B represents-each node except the root node must have at least T-1 keywords, so, each internal node has at least t children-each node contains a maximum of 2 T-1 keywords. Therefore, an internal node has a maximum of 2 t children. T = 2 is the simplest. Each internal node has 2, 3, and 4 children. It is a 2-3-4 tree.

For example, (M = 3 ):


B-tree search, starting from the root node, performs a binary search for the keyword (ordered) sequence in the node. If

The hit ends. Otherwise, the child node in the scope of the query keyword is entered. Repeat until the corresponding son pointer is

Null, or it is already a leaf node;

B-Tree features:

1. the set of keywords is distributed in the entire tree; 2. any keyword appears only in one node; 3. the search may end at a non-leaf node; 4. its search performance is equivalent to performing a binary search in the full set of keywords; 5. automatic hierarchical control;
B + tree

B +-tree: a B-tree deformation tree generated as required by the file system.

The similarities and differences between an m-Level B + tree and a m-Level B tree are as follows:

The node with n subtree contains n keywords;All leaf nodes contain information about all keywords., AndPointer to a record containing these keywords, AndThe leaf node itself is connected in a small and large order based on the keyword size.. (The leaf node of Tree B does not include all the information to be searched.) All non-terminal nodes can be regarded as the index part,The node contains only the maximum (or minimum) keywords in its subtree root node.. (The non-final node of Tree B also contains valid information to be searched)

The non-terminal node contains only the pointer of the smallest keyword in the Child root node.

Why is B +-tree more suitable for the file index and database index of the operating system in actual applications than B-tree? B +-tree disks have lower read/write costs

The internal node of B +-tree does not point to the specific information of the keyword. Therefore, the internal node is smaller than the B-Tree node. If you store all the keywords of the same internal node in the same disk, the more keywords the disk can hold. The more keywords you need to search for In-memory reading at one time. IO reads and writes are reduced.

For example, assume that a disk block contains 16 bytes, while a keyword is 2 bytes, and a keyword is 2 bytes. An internal node of a 9-Level B-tree (a node can have up to eight keywords) requires two disks. While the internal node of the B + tree only needs one disk. When the internal node needs to be read into the memory, the B-tree has one more disk block query time than the B + tree (the disk is the time when the disk is rotated ).
2. the query efficiency of B +-tree is more stable.

Because the non-endpoint is not the final point to the file content node, it is only the index of the keyword in the leaf node. Therefore, any keyword search must follow a path from the root node to the leaf node. The path length of all keyword queries is the same, resulting in the query efficiency of each data.
3. The main reasons for database index adoption of B + tree are:

While improving disk I/O performance, Tree BThe problem of inefficient element traversal is not solved.. To solve this problem, the B + tree came into being. The B + tree can traverse the entire tree as long as it traverses the leaf nodes. In addition, range-based queries in the database are very frequent, while B-tree does not support such operations (or the efficiency is too low)

B*-tree

B *-tree is a variant of B +-tree. On the basis of B + tree (all leaf nodes contain information about all keywords, and pointers to records containing these keywords ),B * Add a pointer to the brothers at non-root and non-leaf nodes in the tree.B * indicates that the number of non-leaf node keywords is at least (2/3) * M, that is, the minimum block usage is 2/3 (instead of 1/2 of B + tree ). A simple example is provided, as shown in:

<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxwPjxjb2RlIGNsYXNzPQ = "hljs haml"> Split of B + tree: When a node is full, allocate a new node, copy 1/2 of the data from the original node to the new node, and add a pointer to the new node in the parent node; the split of the B + tree only affects the original node and the parent node, but does not affect the sibling node. Therefore, it does not need to point to the sibling node.

B * tree split: When a node is full, if its next sibling node is not full, move part of the data to the sibling node, and then insert a keyword into the original node, finally, modify the keywords of the sibling node in the parent node (because the keyword range of the sibling node has changed). If the sibling node is full, add a new node between the original and sibling nodes, copy 1/3 of the data to the new node, and add a pointer to the new node at the parent node.

Therefore, the probability of B * tree allocating new nodes is lower than that of B + tree, and the space usage is higher;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.