[Turn] a variety of binary tree


Data in the database is typically placed inside the disk, when the disk access is necessary to access data,
the physical access procedure: disk rotation, the magnetic arm moves two processes. After the disk rotates to the prescribed position, movement of the magnetic arm began accessing data.

Then the time (speed) access to data is mainly consumed in which part of it? Mainly targeting process consumption.
Therefore: in view of improving the rate of data access, in fact, reduce the number of disk seeks (I / O operation).

For example. To sequential search .

 
 

Find 5 when traversing from start to finish, you need to locate a total of five times. Do not have to say, apparently this order is to find the least efficient.

 

In order to improve efficiency, to binary tree .
Binary specification I will not say,

 

 

A total of six the number, no matter which number search, most will locate three times.
Ah, since binary tree so convenient that we all use binary good. Amount, in fact, a chart that case, can be considered a binary tree, it considered a case. If you can not guarantee the stability of efficiency, and that this structure or not.
(Here record a knowledge)
the first order, in sequence, after traversal. A little like saying, here's the first that is the root node.

In order to enhance stability to a balanced binary tree .
Balanced binary difference with equilibration balance factor to determine whether, and rotates binary tree. Balance factors: difference in height left and right subtrees. Balanced binary tree in the balance factor can not be more than one, or rotation.
Heaved a sigh of relief, such a stable, right?
Stability is stable, but in order to stabilize the binary tree, at the expense of some of the more important things - time.
When rotating binary tree deletion of data resulting from new, very time-consuming!
The purpose of all of the operations are designed to save time and improve efficiency. Such operations forest for the trees. But not did not place merit.
Usage scenarios: New Removal less data, search data can be multi-application scenarios.

On the basis of the stability, and then optimize it, reducing the number of rotations about it, to red-black tree .
Red-black tree (Java in TreeMap) Features:
1, each node is either red or black.
2, the root node must be black.
3, red node can not be continuous (ie, the red node of children and the father can not be red).
4, for each node, any path from that point to null (the trailing end of the tree), and contain the same number of black nodes.

 

 

The key logic red-black tree rotation is: to ensure that the height difference of the left and right subtrees of any node that does not exceed the lower of the two in doubles . Example: Node A, the height of the left subtree X, the height of the right subtree Y, XY <= min (X , Y);
so that when XY> min (X, Y) , and rotating.

Red-black tree has stabilized in such a way to meet it? Not satisfied, but also more stable. To B-tree .
B-tree learning time I have seen such a definition.
Definition 1

1.根结点至少有两个子女。
2.每个中间节点都包含k-1个元素和k个孩子,其中 ceil(m/2) ≤ k ≤ m 3.每一个叶子节点都包含k-1个元素,其中 ceil(m/2) ≤ k ≤ m 4.所有的叶子结点都位于同一层。 5.每个节点中的元素从小到大排列,节点当中k-1个元素正好是k个孩子包含的元素的值域划分 6.每个结点的结构为:(n,A0,K1,A1,K2,A2,… ,Kn,An) 其中,Ki(1≤i≤n)为关键字,且Ki<Ki+1(1≤i≤n-1)。 Ai(0≤i≤n)为指向子树根结点的指针。且Ai所指子树所有结点中的关键字均小于Ki+1。 n为结点中关键字的个数,满足ceil(m/2)-1≤n≤m-1。

Definition 2

定义任意非叶子结点最多只有M个儿子,且M>2;
根结点的儿子数为[2, M]; 除根结点以外的非叶子结点的儿子数为[M/2, M],向上取整; 非叶子结点的关键字个数=儿子数-1; 所有叶子结点位于同一层; k个关键字把节点拆成k+1段,分别指向k+1个儿子,同时满足查找树的大小关系。

前提:M阶B树
三种结点:根结点,中间结点,叶子结点
孩子:指一个结点下的子结点
关键字:结点中的值

1、根结点孩子数量:[2,M]
2、中间结点孩子数量:[ceil(M/2), M]
3、根结点和中间结点的关键字数量:他们的孩子数量 -1

4、一个结点当中:指向孩子指针和关键字的位置关系是:(指针1) 关键字A (指针2) 关键字B (指针3) 每个关键字的值 > 左指针 指向的孩子树里结点中的关键字 每个关键字的值 < 右指针 指向的孩子树里结点中的关键字 5、叶子结点位于同一层 6、结点中的关键字从小到大排列 7、结点中关键字不重复
 
 

After the pictures and definitions are resorted to, B-tree for red-black tree is very obvious advantages, the most obvious is a B-tree node to store more than one keyword. It will be located on disk size comparison key point moved to settle in the operation .

 

B-tree is very possible, but not all are friendly to the scene. To B + tree .
Let us talk about what the scene B unfriendly tree, the scope of the query : for example, to find the image above E-> O all the letters between the two letters. That is, the logical sequence of the B-tree to find E, the sequence continues to find O, and then out letters.
Then B + tree for this scenario is very simple. B + tree-come, first talk about basic things, so much will know why look with a B + tree is simpler than the range of the B-tree.

1、根节点和中间结点 的 孩子数量 = 关键字数量
2、根节点和中间结点 的 关键字 是 关键字对应子树中所有关键字的最大值,同时也存在于子树中
3、根节点和中间结点 只做索引,不包含数据指针以及数据

4、叶子结点包含所有数据,并按照从小到大顺序排列
5、叶子结点用指针连在一起

Therefore, the scope of the query : for example, to find a number between 3-8, the B + tree approach directly on the leaf node ordering traversal to the list.
Furthermore, B + tree to find more stable than B-tree, because every time to find the leaf node.
Why every time to a B + tree leaf node? B-tree where every time?
B represents a key tree index key, and at the same time it is present in the content key points to the actual storage location in memory. If traversed to a certain keyword, then go to the storage location based on real data pointer.
B + tree non-leaf nodes represent the key index key, stores the actual data or pointers to leaf nodes of the real data position.



Guess you like

Origin www.cnblogs.com/ShiningArmor/p/11993175.html