Red-black tree and B tree, B+ tree

1. Red-black tree
1. Characteristics of red-black tree
(1) Each node is either black or red.
(2) The root node is black.
(3) Each leaf node (NIL) is black. [Note: The leaf node here refers to the leaf node that is empty (NIL or NULL)! ]
(4) If a node is red, its child nodes must be black.
(5) All paths from a node to the descendants of the node contain the same number of black nodes. [Here refers to the path to the leaf node]
The height of the red-black tree containing n internal nodes is O(log(n)).
As shown in the figure:


2. Red-black tree usage scenarios Red-black trees are used in
java, TreeSet and HashMap of JDK1.8.

But the question is, why use red- black trees? The insertion and deletion of red-black trees must satisfy the above five characteristics, and perform very complicated operations.
Reason: The
red-black tree is a balanced tree, and its complicated definitions and rules are to ensure the balance of the tree . If the tree does not guarantee its balance, it is as follows: Obviously this becomes a linked list.

The biggest purpose of ensuring balance is to reduce the height of the tree, because the search performance of the tree depends on the height of the tree. So the lower the height of the tree, the higher the search efficiency! This is why there are binary trees, search binary trees, etc., the purpose of various trees.

2.
The characteristics of B-tree 1, B-tree A B-tree of
order m meets the conditions:
(1) Each node has at most m subtrees
(2) Except for the root node, each other branch node has at least [m/2 ] A subtree
(3) The root node has at least two subtrees (unless the B-tree contains only one node)
(4) All the leaf nodes are on the same layer, and the leaf nodes of the B-tree can be regarded as a kind of external nodes and do not contain any information.
(5) A non-leaf node with j children has exactly j-1 key codes, and the key codes are arranged in increasing order.
B-tree is also called balanced multi-path search tree . As shown in the figure:


2. B-tree usage scenarios
B-trees are mostly used for indexing file systems.

So the question is: Why use B-trees? Isn't the red-black tree just fine?
Reason:
Compared with binary trees and red-black trees, B-trees have more subtrees, which means more paths. More subtrees indicate lower tree height and higher search efficiency. Of course, if there are too many paths, it may become An ordered array too . So of course it is impossible to make the number of paths infinite.

Back to the topic: Because the file system and database are generally stored on the computer hard disk, if the amount of data is too large, it may not be able to be loaded into the memory at one time . (A tree can't be loaded all at once, how to find it, right?) But B-trees can be stored in multiple ways. It is precisely because of this advantage of the B-tree that the content of only one node can be loaded into the memory at a time when the file is searched. The red-black tree finds very blocks in the memory, but if it is in the database and file system, the B-tree is obviously better

3. B+ tree
B+ tree is a variant of B tree, which has higher query efficiency than B tree.
1. The characteristics of the B+ tree
(1) The intermediate node of k subtrees contains k elements (k-1 elements in the B tree), and each element does not store data, it is only used for indexing, and all data
is stored in Leaf node.
(2) All leaf nodes contain the information of all the elements, and pointers to records containing these elements, and the leaf nodes themselves
are linked in order of the size of the key from small to large.

(3) All intermediate node elements exist in child nodes at the same time, and are the largest (or smallest) element among the child node elements.


2. The usage scenario of
B+ tree B+ tree is transformed on the basis of B tree. Its data is all in the leaf nodes, and pointers are added between the leaf nodes to form a linked list.
B+ trees are mostly used for indexes in databases.

So why is the B+ tree used for indexing in the database?
Reason:
Because select in the database is often not just querying one record, but often querying multiple records . For example: the last 10 items sorted by id. If there are more than one, the B-tree needs to be traversed in order, which may require cross-layer access. In the B+ tree, because all data is in the leaf nodes, there is no need to cross-layer, and because there is a linked list structure, only the head and the end need to be found, and all the data can be extracted through the linked list.

 

Guess you like

Origin blog.csdn.net/qq_27828675/article/details/115369726