Red-black tree, insert delete operation

Source: Internet
Author: User

Binary sort Tree

A self-balancing two-fork sorting tree (binary search tree)
The process of generating a two-fork sort tree is very easily unbalanced, and the worst case scenario is one-sided (only right/Zuozi), which results in a much lower retrieval efficiency for the two-fork tree (O (n)).
In order to maintain the balance of the binary tree, there are various algorithms, such as: AVL,SBT, stretching tree, treap, red-black tree and so on.

Red and black Trees

Red and black trees need to meet 5 properties:
-node is not red or black
-The root node is black
-All NULL nodes are referred to as a leaf node, and the color is considered black.
-All Red node's child nodes are black, no adjacent two red nodes can appear on one path
-Contains the same number of black nodes on all paths from any node to its leaf node
Like what:

Derived properties: The longest path on the tree cannot be greater than twice times the shortest path.
Explanation: Because the 1th node in the tree is not red or black, because the 4th tree does not allow the existence of two consecutive red nodes, then for a node to its leaf node of the longest path must be red and black staggered, then the shortest path must be a pure black node and 5th contains the same number of black nodes on all paths from either node to its leaf node, so that the number of black nodes on the longest path is equal to the number of black nodes on the shortest path! and the 2nd root node is black, the 3rd leaf node is black, then know: Longest path <=2* Shortest path
Note: The red black tree is not a balanced binary tree, the difference:

1, red and black trees give up the pursuit of complete balance, the pursuit of general balance, and the balance of the time complexity of the binary tree is not a small difference in the case, to ensure that the maximum of three times each insert can be rotated to achieve a balance, the implementation is more simple.
2, balanced binary tree pursuit of absolute balance, the conditions are more stringent, to achieve a more troublesome, each time after inserting a new node need to rotate the number of unpredictable.

The Balanced binary tree is also known as the AVL tree (unlike the AVL algorithm) and has the following properties: It is a
The absolute value of the height difference of the empty tree or its left and right two subtrees is not more than 1, and the left and the two sub-trees are a balanced binary tree.
Construction and adjustment methods the common algorithms for balancing binary trees are red, black, AVL, treap and so on.
The formula for the node of the minimum binary balance tree is as follows F (n) =f (n-1) +f (n-2) +1.
This is similar to a recursive sequence, you can refer to the Fibonacci sequence, 1 is the root node, F (n-1) is the number of nodes Zuozi, F (n-2) is the number of nodes of the right subtree.

Red and black Tree basic operation

Left-hand, right-handed, coloring

Insert

First, in order to ensure that the root node to the leaf node the same number of black nodes, each inserted is a red node .
If you have two red nodes adjacent, you need to adjust:

Scenario 1:
The tree is empty, the position of the root node is inserted directly, the color of the nodes is changed to black.

Scenario 2:
The parent node of the insertion point is black, so it does not break any nature and is inserted directly.

The following is a discussion of the insertion point parent node is red, a total of three, the insertion point is n:
Scenario 3:

Change color, insert g recursively upward

Scenario 4:

Case 5 (turn into case 4):

Delete

We delete a node and actually delete the leaf node (the substitution point).
First, the delete point and the replacement point are exchanged, and then the replacement points are removed.
The substitution point is the last or the next node in the sequential traversal of the deleted point, so the substitution point must be the leftmost or most right child of a subtrees tree.
Therefore, the substitution point must have at least one child node that is a leaf node.
In five cases, the deletion is N:

Scenario 1:

Deleting n At this time requires further recursive deletion (P is the parent element root, delete n operation) because the number of black nodes per path is the same as the broken loop.

Scenario 2:

Scenario 3:

Scenario 4:

Scenario 5: Change to Scenario 4

Red-black tree, insert delete operation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.