Balanced binary tree, AVL tree and red-black tree

Balanced binary tree, AVL tree and red-black tree

balanced binary tree

       To know AVL trees and red-black trees, we must first understand what is a balanced binary tree, because both AVL trees and red-black trees are self-balancing binary trees.

What is a balanced binary tree:

        The depth of the left and right subtrees of each node in a binary tree cannot exceed 1.

There are four types of balance:

       LL type: insert elements on the left child of the left subtree; solution: (left-handed) as shown below:

RR type: insert elements on the right child of the right subtree; the solution (right rotation) is as follows:

   RL type: Insert an element on the left child of the right subtree; the solution (first right-handed and then left-handed) is as shown below:

    LR type: Insert an element on the right child of the left subtree; (first left-handed and then right-handed)

AVL tree

What is an AVL tree:

       The AVL tree is a binary search tree with a balanced condition. It was also first invented from a balanced binary search tree . In the AVL tree, the maximum height difference between the two subtrees corresponding to any node is 1, so it is also called For a height balanced tree , it must satisfy the balance condition.

limitation:

     Because the AVL tree is a height-balanced tree, the balance of the AVL tree will be destroyed when deleting or inserting. As long as the balance condition is not met, the balance must be maintained by rotation, and the overhead of rotation is very large, so AVL Trees are not suitable for situations where there are many insertions and deletions.

application:

     Widely present in the Windows NT kernel;

red black tree

What is a red-black tree:

  A binary search tree, but a storage bit is added to each node to indicate the color of the node, which can be red or black ( either red or black ). By restricting the way each node is colored on any path from the root to the leaf, the red-black tree ensures that no path will be twice as long as the other path . Therefore, the red-black tree is a weakly balanced binary tree (because it is weak balance, it can be seen that in the case of the same node, the height of the AVL tree is lower than that of the red-black tree) , compared with the strict AVL tree, its number of rotations is less, so there are more operations for searching, inserting, and deleting In the case of , we use a red-black tree .

nature:

  1.  Each node is either red or black
  2.  the root node is black;
  3.  Each leaf node (the leaf node is the NULL pointer or NULL node at the end of the tree) is black;
  4.  As shown in the figure, if a node is red, then its two sons are black;
  5. For any node, each path to the NULL pointer of the leaf point tree contains the same number of black nodes;
  6. Each path contains the same black nodes;

How does the black mangrove ensure that the number of rotations is small:

The red-black tree does not pursue "complete balance", that is, it does not require nodes like AVL |balFact| <= 1. It only requires partial balance, but it proposes to add colors to the nodes. Red and black are exchanged for the number of rotations when adding and deleting nodes with non-strict balance. Lower and any imbalance will be resolved within three spins.

Applications:

  1. Widely used in C++'s STL, maps are implemented with red-black trees;
  2. The process scheduling of Linux uses red-black tree to manage the process control block. The virtual memory space of the process is stored in a red-black tree. Each virtual memory space corresponds to a node of the red-black tree, and the left pointer points to the adjacent Virtual memory space, the right pointer points to the adjacent high-address virtual memory space;
  3.  IO multiplexing epoll adopts red-black tree organization to manage sockfd to support fast addition, deletion, modification and query;
  4.  In Nginx, the red-black tree is used to manage the timer, because the red-black tree is ordered, and the timer with the smallest distance from the current one can be quickly obtained;
  5.  Implementation of Java's TreeMap;

Comparison of AVL tree and red-black tree:

  1.  Because the height of the AVL tree will be lower than that of the black-red tree in the case of the same node, the time spent by the AVL tree will be relatively less when doing the search.
  2.  When deleting and inserting, the AVL tree is highly balanced, while the red-black tree is only weakly balanced, so the number of rotations of the black-red tree will be much smaller than that of the AVL tree.
  3. The red-black tree restores balance faster than the AVL tree in the case of imbalance, so the red-black tree has good stability and complete functions, and its performance is also very good, with strong comprehensive strength.

Summarize:

    AVL tree: high balance, long unbalance recovery time, so poor stability, easy to find and not easy to delete and insert.

   Red-black tree: The balance is low, and the imbalance recovery is faster, so the stability is high, but the tree length is relatively high, and it is more convenient to search, delete, and insert.

 

 

Reference: https://blog.csdn.net/u010899985/article/details/80981053

 

 

 

 

 

 

 

Balanced binary tree, AVL tree and red-black tree

Guess you like

Origin blog.csdn.net/Wu_0526/article/details/107973520