Differences and application scenarios between red-black trees and B+ trees

red-black tree

A red-black tree is a binary search tree where each node has a color attribute, either red or black. In addition to the general requirements enforced by binary search trees, we add the following additional requirements for any valid red-black tree:

1. Nodes are red or black

2. The root node is black.

3 Each leaf node (NIL node, empty node) is black.

4 Both children of each red node are black. (There cannot be two consecutive red nodes on all paths from each leaf to the root)

5. All paths from any node to each of its leaves contain the same number of black nodes.

        Comparison of red-black tree and avl (binary balanced tree)

 

         1. If inserting a node causes the unbalance of the tree, both AVL and RB-Tree (red-black tree) only need at most 2 rotation operations, that is, both are O(1); When unbalanced, in the worst case, AVL needs to maintain the balance of all nodes on the path from the deleted node to the root, so the magnitude of rotation is O(logN), while RB-Tree only needs at most 3 times (because No strict balancing is required, the longest possible path from the root to the leaf is no more than twice as long as the shortest possible path) Rotating and modifying the color of the node, only requires O(1) complexity.

          2. Secondly, the structure of AVL is more balanced than that of RB-Tree. Inserting and deleting nodes is more likely to cause unbalance of the Tree. Therefore, when a large amount of data needs to be inserted or deleted, AVL needs to be rebalanced more frequently. Therefore, RB-Tree is more efficient in scenarios that require a lot of insertion and deletion of nodes. Naturally, since AVL is highly balanced, AVL's search is more efficient.

 

       Practical application of red-black tree :

           The implementation of IO multiplexing epoll uses the red-black tree to organize and manage sockfd to support rapid addition, deletion, modification, and checking.
           In ngnix, the red-black tree is used to manage the timer, because the red-black tree is ordered, and the current distance can be quickly obtained. The smallest timer.
           Implementation of TreeMap in java, hashmap of jdk1.8.

 

     B+ tree

            A B+ tree is a tree data structure, which is an n-ary sorted tree. Each node usually has multiple children. A B+ tree contains root nodes, internal nodes and leaf nodes. The root node may be a leaf node or a node containing two or more child nodes.

        (  ps: An example of a 3-order B-tree refers to a maximum of 2 keywords per node, 3 children)

       The B+ tree is a deformed tree of the B tree. The difference between it and the B tree is:

  • A node with k child nodes must have k keys;
  • Non-leaf nodes only have an index function , and the information related to records is stored in leaf nodes.
  • All leaf nodes of the tree form an ordered linked list, and all records can be traversed in the order of key code sorting, which is convenient for interval search and traversal .
  • The advantages of B+ trees are:

    • Since the B+ tree does not contain data information on internal nodes, more keys can be stored in memory pages . The data is stored more closely and has better spatial locality. Therefore, accessing data associated with leaf nodes also has a better cache hit rate.
    • The leaf nodes of the B+ tree are all linked, so the convenience of the entire tree only requires a linear traversal of the leaf nodes. And because the data is arranged in order and connected, it is convenient for interval search and search. The B-tree requires recursive traversal of each layer. Adjacent elements may not be adjacent in memory, so cache hits are not as good as B+ trees.

    But the B-tree also has advantages. The advantage is that since each node of the B-tree contains a key and a value, the frequently accessed elements may be closer to the root node, so the access is faster. The following is the difference between B-tree and B+ tree:



     

  •  

    Application scenarios of b+ tree:

     B/B+ tree is a balanced multi-way search tree designed for disks or other storage devices (compared to binary, B-tree has multiple branches per inner node), compared with red-black tree, in the same In the case of nodes, the height of a B/B+ tree is much smaller than the height of the red-black tree ( mentioned in the performance analysis of the B/B+ tree below ). The operation time on the B/B+ tree is usually determined by accessing the disk. It consists of two parts: time and CPU calculation time, and the speed of the CPU is very fast, so the operation efficiency of the B-tree depends on the number of times the disk is accessed. When the total number of keywords is the same, the smaller the height of the B-tree is, the less disk I/O is. The less time spent. The structure of a binary search tree is not suitable for a database because its search efficiency is related to the number of layers. The lower the data, the more comparisons are required. For the database, every time it enters a layer, it is necessary to read data from the hard disk, which is very fatal, because the reading time of the hard disk is much longer than the data processing time, and the fewer times the database reads the hard disk, the better. This data structure is very beneficial to reduce the number of times the hard disk is read. Assuming that a node can hold 100 values, then a 3-level B-tree can hold 1 million data, and if it is replaced by a binary search tree, it will require 20 levels! Assuming the OS reads one node at a time, and the root node remains in memory, then a B-tree looking for a target value in 1 million pieces of data requires only two hard disk reads.
    

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326079743&siteId=291194637