Binary search tree, red-black tree and balanced tree

binary search tree

Also known as binary search tree , the English abbreviation is BST .

Features:

  1. If its left subtree is not empty, the values ​​of all nodes on the left subtree are less than the value of its root node;
  2. If its right subtree is not empty, the values ​​of all nodes on the right subtree are greater than the value of its root node;
  3. Its left and right subtrees are also binary search trees

As shown in the figure is a binary search tree:

Advantage:

  1. Orderliness : If the binary tree is traversed in "in-order", an increasing sequence of key values ​​of all nodes will be generated. For the binary search tree shown in the figure, the results of in-order traversal are: 3, 12, 24, 37, 45, 53, 61, 78, 90, 100;
  2. Efficiency : The efficiency of insertion and search, the time complexity of the algorithm is O(h) , h represents the height of the tree, which cannot be achieved by other data structures. For example, although the ordered linear table is ordered, the time complexity of the insertion algorithm is O(n); although the time complexity of the heap insertion algorithm is O(log2n), the heap does not have order.

shortcoming:

Imbalance : The binary search tree is unbalanced, even in extreme cases - single chain, as shown in the figure, at this time h=n, the binary search tree loses its efficiency advantage. If you want customer service at this time This kind of imbalance, the traditional optimization method is: balanced tree , red-black tree

insert image description here
algorithm:

  • Insertion algorithm:
    • Starting from the root node, when the key value is larger, go to the left, and when the key value is smaller, go to the right, until the end, which is the insertion point
  • Delete algorithm:
    • If the deleted node has only one child node, then directly connect the child node to the parent node
      Please add a picture description

    • If the deleted node has more than one child, replace it with the smallest value in the right subtree. Finding the minimum value is very simple, just go to the left and go to the end
      Please add a picture description

balanced tree

The full name is self-balancing binary search tree, and the English abbreviation is AVL .

AVL tree is a binary search tree with additional balance conditions . The balance conditions are established to ensure that the depth of the entire tree is O(logN).

Balance condition: Intuitively, the best balance condition is that the left and right subtrees of each node have the same height, but this is too harsh, and it is difficult for us to insert new elements to maintain such a balance condition. The AVL tree is the next best thing, requiring that the height difference between the left and right subtrees of any node is at most 1 , which is a weaker condition, but it can also ensure a balanced state of logarithmic depth .

Self-balancing algorithm:
occurrence condition: when the element is inserted and the insertion is successful

The position of the new element is nothing more than the following four:

  1. The insertion point is in the left subtree of X's left child node - left left
  2. The insertion point is in the right subtree of the left child node of X ---- left and right
  3. The insertion point is in the left subtree of the right child node of X ---- right left
  4. The insertion point is in the right subtree of X's right child node ---- right right

Among them, X represents the deepest one among the nodes whose balance state is broken. Since the node has at most two child nodes, and the balance is broken, it means that the height difference between the left and right nodes of X is 2
Please add a picture description

  • Cases 1 and 4 can be considered as lateral insertion, and can be adjusted to balance with a single rotation operation


As shown above, the left side is before rotation, and the right side is after rotation

In order to adjust the balance state, we hope to increase A by one level and decrease C by one level, that is, we will see that k1 increases upwards and k2 decreases downwards .
According to the characteristics of the binary search tree, we can know that k2>k1, so k2 must become the right child node of k1 in the new tree .
According to the characteristics of the binary search tree, until the B subtree is between k1 and k2, then the B subtree in the new tree must fall on the left side of K2.

  • Cases 2 and 3 can be regarded as medial insertion, and can be adjusted to balance by using double rotation operations.
    Please add a picture description
    As shown in the above picture, the left side is before rotation, and the right side is after rotation

For the internal test insertion, it is absolutely impossible to simply perform a single rotation, because it is still unbalanced after the rotation, so you may as well draw a picture and try it yourself.
For the internal test insertion, the only possibility is to use k2 as the new root node, which makes (according to the rules of the binary search tree) k1 must become the left child node of k2, and k3 must become the right child node of k2.

Double rotation is not a new algorithm, it is simply to perform two single rotations.
The specific process of double rotation:

  1. K2 and k1 perform a single rotation, and k1 can appear as the left child node of k2
  2. K2 and k3 perform a single rotation, and k3 can appear as the right child node of k2

Please add a picture description

red black tree

The English abbreviation is RB-tree .

The rules of red-black tree:

  1. Each node is either red or black
  2. root node is black
  3. If a node is red, its children must be black
  4. In any path from any node to NULL (the end of the tree), the number of black nodes must be the same

According to the rules, we can analyze the following inevitable situations of execution:

  1. According to rule 4, the element we insert must be red
  2. After the element finds the insertion point and inserts it, if the parent node of the insertion node is red, it does not comply with rule 3 and must be changed
  3. For NULL elements, we regard them as black, so we need to worry about violating rule 3. Because there will be a NULL element at the end of each path to the end of the tree, so the same number of black nodes in rule 4 does not affect

As shown in the figure below, insert four elements 3, 8, 35, and 75 into the red-black tree, because the newly added nodes must be red. It can be seen that these four elements do not conform to the rules of the red-black tree. One analysis.

Please add a picture description
Before the analysis, let's clarify the naming of each node:
X: new node
P: the parent node of the new node
G: the parent node of the new node's parent node, that is, the grandparent node
S: the new node's grandparent node is another node except the parent node Child node, that is, the uncle node
GG: the parent node of the grandparent node of the new node, that is, the great-grandfather node

  1. State 1: S is black and inserted outside (corresponding to inserted element 3)

    Do a single rotation on P and G, and change the color of P and G

Please add a picture description
2. State 2: S is black and inserted inside (corresponding to inserted element 8)

  • Do a single rotation on P, X, and change the color of G, X
  • Do a single rotation on X, G without changing the color
    Please add a picture description
  1. State 3: S is red and inserted outside (corresponding to inserted element 75)
  • Do a single rotation on P, G and change the color of X
  • If GG is black, everything is done, if GG is red, there is some trouble, we look at state 4
    Please add a picture description
  1. State 4: S is red and inserted outside (corresponding to inserted element 35)
  • Perform corresponding operations according to state 3
  • If GG is red, we need to continue to operate according to the situation until the father and son are not both red. We
    Please add a picture description
    can figure it out without analysis. If you keep developing to the upper structure, it is very time-consuming, so we can implement a top-down program.
    Top-down program logic:
    When a new element X is inserted, there will be a process of searching for the insertion point from the root node downwards. During this process, if two child nodes of a certain node N are found to be red, then X Change it to red, and change the two child nodes to black.
    Please add a picture description

Guess you like

Origin blog.csdn.net/weixin_44081533/article/details/115016947