Analytical self-balancing binary tree: red-black tree

Analytical self-balancing binary tree: red-black tree

Red-black tree concept

**What is a red-black tree? **Red-black tree is a self-balancing binary tree (AVL). Just like its name, the nodes that make up the tree are either red or black
**What is it like? **The red-black tree is on the ordinary binary tree, and a color is added to each node as the attribute of the node. This is a property of the red-black tree
**What is the role of the red-black tree? **The typical use is for associative arrays. The data structures map and set in c++ are implemented based on red-black trees. A key corresponds to a value. The corresponding value that can be found through the key is used to store ordered Data, its time complexity is O(lgn), its efficiency is very high

Properties of red-black trees

Nature 1:

节点不是红色就是黑色,这是红黑树的特征,要不叫红黑树

Nature 2:

根节点必须为黑色,不能为红色

Nature 3:

每个叶子节点(指的是NIL或者空节点的叶子节点)是黑色的

Nature 4:

如果一个节点是红色的,那该节点的子节点必须是黑色的

Nature 5:

从根节点到叶子节点的所以路径上的黑色节点的数量必须相同

Basic operation of red-black tree

1. Add

Add, that is, insert a new node into the red-black tree. The red-black tree before insertion is of course a balanced binary tree, which is ordered, so after insertion, the new red-black tree must also become a balanced binary tree.

The first step of adding: set the color of the new node to red

Why should the new node be set to red? instead of black? This is because of one of the properties of the red-black tree: from the root node to the leaf nodes in the tree, the number of black nodes on the path must be the same. If it is black, it will violate this property after being inserted into the tree, so Newly inserted nodes must be red

Add the second step: Make the current binary tree into a balanced binary tree through left-handed or right-handed operations and coloring operations

After the new node is inserted, first check whether it is balanced or not, and turn into a balanced binary tree through left-handed or right-handed; left-
handed: the right node is used as the current root node, and the root node is the original right node, which is the current root The left node of the node, the left node on the original right node is the right node of the current left node, as shown in the figure:
left-handed
Right rotation: and left rotation is the opposite, that is, the left node is used as the current root node, and the root node is used as the original left node That is, the right node of the current root node, the right node on the original left node is used as the left node of the current right node, as shown in the figure: If the
Right-handed
height difference between the left and right subtrees of any node in the tree is greater than 1, it means that the tree is not a balanced binary tree , you need to adjust the left and right rotations to make it a balanced binary tree.
The next step is shading: this score considers several cases:

  1. When the parent node of the inserted node is black, the five properties are not violated, so there is no need for coloring;
  2. If the root node is empty, just set the color of the inserted node to black, because the inserted node is the root node;
  3. If the parent node of the inserted node is red,
    there are two situations.
    1. When the sibling node of the parent node of the node to be inserted is red, for example:
    insert image description here
    at this time, the parent node must be changed to black, and then rotate right Operation, and then left-handed operation to get a new red-black tree, as shown in the figure below: the
    insert image description here
    inserted node is on the right, the steps are the same, but the direction of rotation should be changed to first left-handed, then right-handed.
    2 When the sibling nodes of the parent node of the node to be inserted are black, for example:
    insert image description here
    first change the color of the parent node to black, change the parent node of the parent node to red, and then perform right rotation.
    If the inserted node is on the right, just change from right-handed to left-handed.

Guess you like

Origin blog.csdn.net/qq_44858592/article/details/108160260