Red-black tree: rotate, insert, delete operations; the difference with the balanced binary search tree

1. The difference between red-black tree and balanced binary search tree:
balanced binary tree (AVL):

  • It is an empty tree or the absolute value of the height difference between its left and right subtrees does not exceed 1. (Fully balanced), And the left and right subtrees are both a balanced binary tree.
  • The time complexity is both the best case and the worst case O(lgn).
  • To ensure absolute balance, after inserting and deletingNeed multiple rotations to maintain balance, Thus very time-consuming.

Red-black tree:

  • The red-black tree gave up the pursuit of complete balance,Pursue roughly balance
  • The time complexity is also the best case and worst case O(lgn).
  • Pursue roughly balance so,No need for a lot of rotating operations

to sum up: The AVL tree is suitable for the case where the number of insertions and deletions is relatively small, but the search is large; if the number of insertions and deletions is large, the red-black tree is suitable.

Reference: What is the difference between a red-black tree and a balanced binary tree?
The difference between red-black tree and AVL tree (balanced binary tree)

2. Red-black tree nature:
Binary search tree satisfying red and black nature: The
Insert picture description here
red-black tree guarantees that no path to a leaf node will be twice as long as other paths, so it isApproximate balance, As shown in Figure (a).
To facilitate the code,Use sentinel to represent NULL, indicating the parent node of the leaf node and the root node, As shown in Figure (b).
Insert picture description here
Usually only internal nodes are displayed:
Insert picture description here
3. Rotation operation: used to maintain the red and black nature.
Left-hand and right-hand: After rotation, the splicing of nodes ensures that the nature of the binary search tree is easy to understand.
Left-handed (right-handed): The left (right) child who becomes his right (left) child.
Insert picture description here
Pseudo code:
Insert picture description here
4. Insertion:
Steps:
a. Insert the leaf node according to the binary search tree method;
b. Color it to red;
c. Call the red-black property adjustment function to turn the tree into a red-black tree through rotation and recoloring.
Insert picture description here
Line 17 of the code: red and black property adjustment function:
three types of situations:
**case 1: the uncle node y of z is red: ** adjust the color circularly to satisfy property 4, until the parent node is black.
Insert picture description here
Case 2: tert node y z black and z is a right child: by L becomes case 3 .
**Case 3: The uncle y of z is black and z is a left child: **The red and black properties are adjusted by turning right and recoloring.
Insert picture description here
Overall code:
Insert picture description here
Insert picture description here
5. Deletion is
too complicated, so let's learn it when you use it in your work. Understand the principle in the interview, know how to use it, and know where to study.

to sum up:

1. The red-black tree pursues roughly balance, and the AVL tree pursues absolute balance.
2. On the basis of the binary search tree, the red-black tree satisfies the nature through left-right rotation and color adjustment.
3. Understand the nature of red-black trees.

Guess you like

Origin blog.csdn.net/qq_33726635/article/details/105878932