B-Tree motivation and structure

Source: Internet
Author: User

Ps. We follow from perceptual to rational cognitive order to gradually explore the mystery of B-Tree, which is often said value here with key(key code) refers to, because it is possible to save a string, said value is not appropriate.

(Multi-MAP WARNING!!!) recommended to watch under Wi-Fi)

Although the search tree we have seen so far is two forks, there is another common finding tree that differs from this, called B-tree , also known as B-tree. Here isthe basic information of B-Tree

Physically, Each node of the B-tree may contain more than one branch, but later it will see that it is logically still equivalent to the two-fork lookup tree previously described, so we still put it in the search tree category. What is the motive for designing and implementing B-trees, which already has so many species ? The B- tree was also the primary function, which was to bridge the huge difference in access speed between different storage levels-that is, to achieve efficient IO. Let's go through the year before, when Bill Gates 's sentence was once a laughingstock to many people.

Because he had asserted at that time that the basic memory capacity of 640KB, DOS, was sufficient to meet the needs of any practical application. Although this is a bit arbitrary, but after learning the B-tree, we will certainly think that this sentence is actually true truth, because we have a variety of metaphysical optimization method 2333.

In a sense, the memory we are able to use in our calculations is getting smaller, not as much as our intuition. This may sound like a paradox, but the reason is that the amount of information that needs to be handled is increasing: the growth rate of the system's storage capacity << The scale of the application problem. Take a look at a set of statistics

The amount of digital information that we have in our human population has been growing at an alarming rate in the past half-century, such as the total number of years up to Zettabyte--1 and the 0 after reaching The end of the year. We know that China's population is roughly more than 1 billion, which is about $10^{9}$, and everyone needs a TB -scale hard drive (if the hard drive has more of this space than 233), note that What we're talking about here is just the hard drive, which is external storage. And if you think about memory, that's a lot more stressful.

Take a closer look at some of the numbers and compare the typical database sizes and memory sizes in different eras:

In just 20 , memory size jumped 1000 times times , but data size jumped million and shuffling. The pressure on the memory capacity is actually even greater, and the pressure is increased by at least 100 times times . Today, the typical datasets are mostly terabytes, whether in the fields of biomolecular, medicine, physics, nuclear power or meteorology. Give me a few examples .

All in all, although the absolute capacity of memory is indeed increasing with the development of technology, the capacity of memory is actually getting smaller in comparison to the actual application requirements. Then why not make the memory bigger? The reason is, do you have the money to dial? we have to make a choice between capacity and access speed, the composition of the principle, the larger the capacity of memory is slower, in turn, in order to make access faster, you have to make the necessary sacrifices in capacity. In the face of this internal contradiction, we can still make a difference, using the contradiction analysis method, we think of a wonderful method--cache! To do this we need to know more about the performance of different levels of memory:

This consists of two facts

    1. Memory with different capacity and type, the difference in access speed is very wide.

In the case of disk, memory, two-level storage, how much difference do they have in access speed? As far as traditional rotary disks are concerned, the speed of access is roughlyMsLevel. And the typical memory, roughly, is theNsmn, with one second as the benchmark -- The former is $10^{-3}$ < Span lang= "ZH-CN" >51s1 day cautionary" in the image of the said:

Therefore, in the design and implementation of the algorithm, in order to avoid a external memory access, we would rather access memory 10 times, hundreds or thousands, million times. This is why storage systems are typically organized hierarchically. As the levels go deeper, the capacity of the memory becomes larger, but in turn, access is getting lower. The efficient operation of such a hierarchical structure lies in the strategy of placing the most commonly used data at a higher level as much as possible, because although its storage capacity is limited but the speed is highest, the infrequently used data is adaptively transferred to a larger, but slower, level.

    1. Read and write 1 b from disk, almost as fast as read and write 1KB

Typical storage systems do mostly support read or write operations in a batch-based manner. Specifically, whether we need to output data from memory to external memory, or if we need to read data from external memory to memory, the data involved are accounted for and organized in pages. For example, in C 's stdio.h, There is this code:

Where the setvbuf interface allows to set the size of the page buffer, buffering the operating mode and so on. Therefore, in the algorithm involving frequent and large numbers of data access, it is necessary to make full use of this feature, that is to say, we should gradually get used to batch type of access. Read or write at once or do nothing . In terms of marginal costs, such organizations and access can be optimized as much as possible. Then our protagonist B- tree In the meantime can play a role?

let's explore theinterior of the B- tree.

B-Tree is also used to store a set of key codes of the entry, its characteristics are very distinct, first of all, each node may not have only two fork, you can have more forks. Second, the depth of all the underlying nodes is exactly the same, and in this sense it is an ideal and balanced search tree. Finally, and most importantly, the overall feature: The B-Tree appears wider, shorter, and can be dynamically changed relative to the regular two-fork lookup tree. The designer of B- tree defines it as a balanced multi-path (multi-way) search tree, which is essentially equivalent to the previous two-way search tree, Because each internal (internal) node can be thought of as a result of the proper merging of several two-way nodes

For instance, look at this.

Do not look at the box, this is a BST, looked at the box, the father and son two generations into a node, the tree has become this:

after 2 generations of merging, each node will have 3 key codes and 4 branches. By the Way, after 3 generations of merging, each node contains 7 key codes and 8 branches. In general, if a merge is performed every d generation, then each node will have a $2^{d}$ branch and a corresponding 1 -unit key code. Since this kind of multi-path search tree and the two-way search tree does not have the essential difference, it also invented a ghost Ah, this is the irrigation paper?

Not also, the problem is that we are usually hierarchically hierarchical organization of the storage system, if the use of B-tree can be targeted at different levels of communication, greatly reducing the number of IO access, thus greatly improving the computational efficiency. Is it not enough to be familiar with the AVL tree?

Let's do a primary math, there's a $10^{9}$,1GData set for each record. If they are organized into a treeAvlTree, the height is roughly30Layer. In other words, in the worst case, a single lookup requires deep30layer, and each layer we need to execute onceIoOperation, and each time can only read a value, which is not worth the candle. ThatBWhere's the tree?BThe merged nodes in the tree contain more than one key code at a time, so theBEach drop in the tree can be read into a group of internal nodes instead of a single key code, which translates the features of external memory bulk access into real benefits. The scale of an internal node depends on the size of the data buffer page, usually in several casesKb256,$2^{8}$1g record b tree height does not exceed 4io4 times, which is a big improvement. Maybe some people have questions: isn't it all a constant level? This is true in terms of asymptotic meaning, but when each unit of this constant is equivalent to 105 06 o'clock, we have to make a fuss, because our memory and time are limited. Just like 1 seconds and 1 days can be seen as constants, but for a limited life, But there are essential differences, 4 year to read a bachelor can accept 30

With the above perceptual knowledge, we have a rational insight into what the B-tree is . Each b-tree has its own order , which is its intrinsic property. M-Order B-Tree is a tree with the following structural characteristics :

    1. Root limit:0 sons or sons number between 2 and M
    2. Except for tree roots , the number of sons of all non-leaf nodes is between $\left\lceil \frac{m}{2} \right\rceil$ and M
    3. All the leaves are at the same depth , and the outer nodes are at the same depth.
    4. Each internal node has no more than M-1 key codes

In other words , m This indicator specifies theupper and lower bounds of the B-tree internal nodes and branches : M-order B-trees have a maximum of m branches per node, root, and other nodes have at least $\left\lceil \frac{m}{2} \right\rceil$ Branch , this tree is also called ($\left\lceil \frac{m}{2} \right\rceil$, M)-tree. Nodes have a maximum of M-1 values , and the other nodes have at least the actual number of branches -1 values (byWikipedia). for the 4-order B-tree, it can also be called (2,4) tree, interestingly, (2,4) tree in the B-tree has a very unique role and status, Later we will see the 2,4 tree and the red and black trees have a puzzled origin. This write B-tree analysis, the next write concrete implementation, and then the next article on the red and black trees.

The red part is the real leaf node, which can be called "External node" in many literatures, but in the B-tree It is two completely different concepts . In addition, the height of the B-tree is also factored into the external node, unlike the usual BST. It is also important to note that thequestion of the B- tree representation is due to the fact that his particular nature has led to a number of pointers being set aside in many cases, specifically, if a full b When the tree is drawn, it is necessary to draw 2 pointers for each of the key code's left and Right descendants, as follows

But the paper is not that big, so to be compact, we simplify the pointers to a particle, and it turns out that:

And the external nodes are all on the same level, no differences, just ignore them, we only focus on the different points. This can be said:

or this:

  so you can save space to express huge amounts of data and

Related Article

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.