[soft test] data structure - tree structure - balanced binary search tree (red-black tree)

1. What is a red-black tree?

Red-Black Tree is a binary search tree with self-balancing properties.

Second, the characteristics of red-black tree

2.1 Color attribute of red-black tree

A red-black tree gets its name from its property that a red-black tree marks each node as either red or black.
Each node of the red-black tree has an additional attribute, that is, the color attribute.
Each node of the red-black tree is either red or black.

Each leaf node (NIL node, empty node) of a red-black tree is black.
The root node of a red-black tree must be black.
If a node is red, both of its children are black (i.e. there are no two adjacent red nodes)
For each node, on simple paths from that node to all its descendant leaf nodes, black nodes the same amount.
All paths from any node to each of its leaf nodes contain the same number of black nodes.
insert image description here

2.2 Balance

The red-black tree can automatically perform balance adjustments in operations such as insertion and deletion to ensure the balance of the tree.
In order to restore the balance of the tree, the red-black tree adopts a series of normalization operations, including color adjustment and rotation operations.
Red-black trees maintain tree balance by using color and normalization operations.
The red-black tree can maintain the balance of the tree during operations such as insertion and deletion.

2.3 Time complexity: O(log n)

In any case, the height of the red-black tree will never exceed O(log n).
For operations such as searching, inserting, and deleting in a red-black tree, its time complexity is O(log n).
But in the worst case, it may degenerate into a linked list structure, and the time complexity is O(n). Therefore, in practical applications, it is necessary to optimize the red-black tree, such as using techniques such as "color flipping" to reduce the occurrence of the worst case.

2.4 Better performance

A self-balancing red-black tree is an efficient data structure, especially when the ordering of a large number of elements needs to be maintained.
Red-black trees have better performance and are suitable for application scenarios that require frequent operations such as search, insertion, and deletion.

3. Red-black tree insertion and deletion operations

The insertion and deletion operations of a red-black tree are similar to those of a normal binary search tree, but color and balance issues need to be considered additionally.

3.1 Insertion operation of red-black tree

During operations such as insertion and deletion, the red-black tree can be automatically rotated and color adjusted to ensure that it is always a relatively balanced tree.

The insertion operation of the red-black tree is relatively simple. During the insertion process of the red-black tree
[normal insertion] first insert a new node into the tree according to the insertion method of the binary search tree,
[color adjustment] and then dye the node red according to the rules or black to maintain the properties of a red-black tree.
[Rotation] If the nature of the red-black tree is destroyed after inserting a new node, some rotation operations are required to restore the balance of the tree.

3.2 Red-black tree deletion operation

The deletion operation of the red-black tree is a little more complicated,

First, delete nodes according to the deletion method of the binary search tree,
and then perform color adjustment and rotation to ensure the properties of the red-black tree;

Three cases need to be dealt with:

3.2.1 The node to be deleted has no child nodes

Just delete the node directly.

3.2.2 The node to be deleted has a child node

Delete the node and connect its children to its parent.

3.2.3 The node to be deleted has two child nodes

Find the node's successor node (that is, the minimum value in its right subtree) or predecessor node (that is, the maximum value in its left subtree), and replace the node to be deleted with this node. Then some adjustments are made to the replaced nodes to keep the tree balanced.

Guess you like

Origin blog.csdn.net/wstever/article/details/129434768