1. About B-tree
Although most of the search trees are binary trees, there is a popular serach tree taht is not binary. This tree is known as a B-tree.
Although most of the search trees are binary trees, there are not binary tree search trees. This is a B-tree. In short, Tree B is a multi-channel balanced search tree. It is suitable for organizing dynamic search tables on direct storage devices such as disks.
A B-tree of order m Is a tree with the following structural properties:
(1) theroot is either a leaf or has between 2 and m children.
(2) All nodeleaf nodes (except t the root) have between [m/2] And m children.
(3) All leaves are at the same depth.
(4) Each node shoshould contain such data fields
(P0, K0, P1, K1,... Pi, ki... pm, km)
[K0, K1,... km] is the increased key.
[P0, P1,... pm] is the pointer which is point to its related child nodes.
A B tree of the degree of M (M> = 3) has the following properties:
(1) The root node is either a leaf node or a child node of 2 to M.
(2) All non-leaf nodes have m/2 to M child nodes.
(3) All leaf nodes have the same height.
(4) Each node should contain the following data domains:
(P0, K0, P1, K1,... Pi, ki... pm, km)
[K0, K1,... km] is an ascending sequence of keywords.
[P0, P1,... pm] is the pointer field pointing to its child node, which is initially empty.
All data are stored at the leaves. contained in each interios node are pointers P1, P2 ,... PM to the children, and values K1, K2 ,... K (S-1), representing the smallest key found in the Subtrees P2, P3 ,..., PM, respectively. of course, some of the these pointers might be null, and the corresponding Ki wowould then be undefined. for every node, all the keys in subtree P1 are smaller than the keys in subtree P2, and so on. the leaves contail all the actula data, whichare either the keys themselves or pointers to recores containing the keys. we will assume the former tokeep our examples simpe. there are various definitions of B-trees that change this structure in mostly minor ways, but this definition is one ot the popular forms. we will also insist that the number of keys in a leaf is also[M/2] and M.
All data is stored on leaf nodes. Each node contains pointers to its child nodes P0, P1, p2... PM, and K0, K1, K2 ,... KM equivalent. These values are the corresponding subtree P1, p2... the minimum value of P (S-1. Of course, these node pointer PI may also be null, and the corresponding Ki is also null. For each node, all the key values in the Child number P1 must be smaller than the key value in the Child tree P2, And the other child trees are also.
Is a typical B-tree
The tree as following dimo-showed is an example of a B-tree of order 4
A B-tree of order 4 is more popularly known as a 2-3-4 tree, and a B-tree of Order 3 is know as a 2-3 tree. we will describe the operation of B-trees by using the special case of 2-3 trees. our starting point is the 2-3 tree follows.
A 4-Level B tree can also be called a 2-3-4 tree, but it is usually called a 4-Level B tree. A tree with the order of 3 is usually called a 2-3 tree. The following uses 2-3 trees as an example.
we have drawn interior nodes (nonleaves), which contain the two pieces of data for each node. A dash line as a second piece of information in an interior node indicates that the node has only tow children. leaves are drawn in boxes, which contain the keys. the keys in the leaves are ordered. to perform a find. we start at the root and branch in one of (at most) three directions ctions, depending on Relation of the key we are looking for to the two (possibly one) values stored at the node
we have drawn an internal node, each of which contains two pieces of data, inside the node -- indicates that the node has only two children. The box contains the leaf node, which contains the key values. The key values in the leaf node are arranged in order. For search, we start from the root node, and then select the corresponding decision tree based on the relationship between the two values stored in the node and the key value.
To performInsertOn a previusly unseen key, X, we follow the path as though we were finding a find. When we get to a leaf node, we have found the correct place to put X.Thus, to insert a node with key 18, we can just add it to a leaf without causing any violations of the 2-3 tree Properties. The result is shown in the following figure.
In order to perform an insert operation without the key value X, we first perform the search operation, and then find the corresponding location. After finding a leaf node, we can find the correct position to insert X. For example, if we want to insert a key 18, we just need to insert it into the node without damaging the attributes of 2-3 trees.
Unfortunately, since a leaf can hold only two or three keys, this might not always be possible, if we now try to insert1Into the tree, we find that the node where it belongs is already full. Placing our new key into this node wocould give it a fourth element, which is not allowed.This is can be solved by making two nodes of two keys each and adjusting the information in the parent.
Unfortunately, a leaf node can have only two or three key values. However, if we want to insert key value 1, this possibility will be broken because the key value of the node to be inserted is full. It is obviously not allowed to insert it as 4th key values. Then, we only need to adjust it in the parent node.
Unfortanately, this idea does not always work, as can be seen by an attempt to insert 19 into the current tree. If we make two nodes of two keys each, we obtain the following tree.
This tree has an internal node with four children, but we only allow three per node. The solution is simple,We merely split this node into two nodes with two children. of course, this node might be one of three children itself, and thus splitting it wocould create a probrem for its parent (which wocould now have four children ), but we can keep on splitting nodes on the way up to the root until we either get to the root or find a node with only two children.